I have a form that has placeholders for input fields. It uses html5 placeholder attribute and javascript placeholder fallback. I use unobtrusive validation like so:
[DisplayName("")]
[Required(ErrorMessage = "Please enter a name")]
[StringLength(maximumLength: 20, MinimumLength = 1, ErrorMessage = "max length is 20 chars.")]
public string UserName { get; set; }
[DisplayName("")]
[Required(ErrorMessage = "Please enter a comment.")]
[StringLength(maximumLength: 350, MinimumLength = 1, ErrorMessage = "max length is 350 chars.")]
public string CommentText { get; set; }
The problem is that when using IE the placeholders are counted as characters in the input fields so there is no validation and the form can be submitted when nothing is entered.
Is there some way I can add a condition to the UserName annotations that restricts a specified string. For example "Enter username here.."
I would like to avoid writing a custom property validator if this is possible.
Thanks in advance.
EDIT. Solved using regular expressions:
[RegularExpression("^((?!My_Placeholder_Text).|\n)*$", ErrorMessage = "Please enter text")]