Leo Jiang
Leo Jiang

Reputation: 26115

Why doesn't this jQuery HTML5 fallback work?

  $('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

Answers (1)

numbers1311407
numbers1311407

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

Related Questions