简体   繁体   中英

Why doesn't the regular expression below work?

I'm using data annotation for validation but code below does not work:

    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Only Numbers allowed!")]
    [Required(ErrorMessage = "Client Id Required")]
    [Display(Name="Client ID")]
    public int ClientId
    {
       ...}

Try this instead:

[RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]

Answering your question "Why it's not working?", because The RegexValidator isn't called when the field is empty, you must use the RequiredFieldValidator in combination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM