RegExp无法在javascript中工作 [英] RegExp not working in javascript
本文介绍了RegExp无法在javascript中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除正则表达式外,其他所有功能都正常运行,我尝试了每种解决方案,但每次其他块都执行时,即使输入有效也是如此.
Everything is working fine except regular expression i have tried every solution but every time else block executes even when input is valid.
我想使用regExp来验证有效的电话号码
I want To use regExp to validate valid Phone Number
var inputValidation = function () {
this.validation = function (para1) {
var input = $(document).find('[data-input-validation = ' + para1 + ']');
input.each(function () {
$(this).keyup(function(){
var value = $(this).val();
var validate = new inputValidation();
validate.validater('phoneNumber',value);
});
});
};
this.validater = function(param1,param2) {
switch (param1) {
case 'phoneNumber':
var mobileNumber = new RegExp('/[0-9-()+]{3,20}/');
if(mobileNumber.test(param2)){
alert('valid');
}
else {
alert('false');
}
break;
}
}
};
var validate = new inputValidation();
validate.validation('search');
推荐答案
您正在混合使用 string 正则表达式符号和文字正则表达式符号.仅选择以下一项:
You are mixing both the string regex notation and the literal regex notation. Choose only one of:
new RegExp('ab+c');
new RegExp(/ab+c/);
在您的情况下,可能是这样的:
in your case maybe something like this instead:
var mobileNumber = new RegExp(/[0-9-()+]{3,20}/);
这篇关于RegExp无法在javascript中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文