表单验证正则表达式和 DOM: 在表单验证中实现正则表达式的最佳方法是什么?
我目前正在这样做,但是当我验证它与值不匹配时:
if(this.value.length > 0 && this.value === /^[a-zA-Z]+$/ );
提前谢谢您!
最佳答案
您需要使用test()
方法。
var patt = /^[a-zA-Z]+$/; //define your RegEx
if(this.value.length > 0 && patt.test(this.value)){ //check if this.value is longer than one character AND matches your RegEx
//do something
};
关于javascript - 表单验证正则表达式和 DOM : What would be the best way to implement regex in a form validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048896/