对于具有正则表达式模式的输入字段:
<input type="text" maxlength="4" inputmode='numeric' pattern="\d*">
我想使用 javascript 来验证输入,通过以某种方式解析pattern
属性(不起作用):
$('input').on('keypress', function(e){
var regex = this.pattern,
valid = eval(regex).test( e.charCode );
if( !valid )
return false;
});
我知道模式中的正则表达式与JS使用的不同,但我需要某种方式来转换任何正则表达式,以此方法可以工作,允许只键入模式允许的字符。