문제

I have a viewmodel that works fine EXCEPT if the person enters nothing and hits submit, then i get a db error because the column doesn't except nulls.

I am doing

 if (ModelState.IsValid)

How can I make this Email field use a RegularExpression AND a required attribute at the same time?

public class RegistrationViewModel
{
    [RegularExpression(@"^......$", ErrorMessageType = ..., ErrorMessageResourceName = ...]
    public string Email {get;set;}

    ...
}
도움이 되었습니까?

해결책

Just use the RequiredAttribute.

[Required(AllowEmptyStrings = false)]

Now, if there isn't a value it will fail and if there is one the RegularExpressionAttribute will validate it.

다른 팁

You can add many validation attributes like this :

[Required(ErrorMessage = "Email address is mandatory")]
[RegularExpression(@"^......$", ErrorMessageType = ..., ErrorMessageResourceName = ...]
public string Email {get;set;}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top