Reputation: 26115
$('form').find('input[pattern],textarea[pattern]').each(function(){
if(!$(this).val().match($(this).prop('pattern'))){
$(this).addClass('error');
}
});
<textarea name=... required pattern=^.{10,255}$></textarea>
Basically, if the value doesn't match the pattern, a class (error) is added. However, the class isn't being added to the textarea even if I only type 1-9 characters.
Upvotes: 1
Views: 251
Reputation: 34072
In HTML5, pattern might be interpreted as a regex, but here it's just a string. You'd need to make a regex out of it.
new RegExp($(this).attr('pattern'))
Also, you might want to throw some quotes around the attribute:
pattern="^.{10,255}"
Upvotes: 1