Search code examples
asp.net-core-mvcunobtrusive-validation

jQuery Validation Unobtrusive allowing empty strings at browser


I have this on my model...

[Required(ErrorMessage = "Passwords must be at least eight characters.")]

If I submit no string, then the browser won't let me submit the form.

If I submit an empty string, ' ', it posts back and returns with a validation error.

How do get the client validation to disallow empty strings?

It looks as though it fails validation on the server because an empty string is trimmed to zero length.

Strange inconsistency between the client and the server.


Solution

  • Try to use RegularExpression to disallow spaces in your model property value

    [Required(ErrorMessage = "Passwords must be at least eight characters.")]
    [RegularExpression("^[A-Za-z0-9]$", ErrorMessage = "Field can not contain only spaces")]
    public string somestring {get;set;}