Frage

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;}

    ...
}
War es hilfreich?

Lösung

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.

Andere Tipps

You can add many validation attributes like this :

[Required(ErrorMessage = "Email address is mandatory")]
[RegularExpression(@"^......$", ErrorMessageType = ..., ErrorMessageResourceName = ...]
public string Email {get;set;}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top