[英]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.
但是,即使我只键入1-9个字符,也不会将类添加到文本区域。
In HTML5, pattern might be interpreted as a regex, but here it's just a string. 在HTML5中,模式可能会解释为正则表达式,但是这里只是一个字符串。 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}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.