jQuery表单验证-电话号码 [英] Jquery form validation - telephone number
本文介绍了jQuery表单验证-电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表单上设置了jQuery验证,该验证当前测试电话号码字段是否为空且为数字,但我希望它能够处理用户在移动/区域后放置空格的情况代码.
I have setup jQuery validation on form, The validation currently tests that the telephone number field isn't empty and is a number, but I'd like it to be able to handle a user placing a space after the mobile/area code.
任何人都可以建议我需要做些什么来允许这样做吗?
Can anyone advise what i'd need to do to allow this?
这是我当前的代码-
if((phone.length == 0) || (names == 'Please Enter Your Contact Number (no space)')){
var error = true;
$('.error').fadeIn(500);
$('.pStar').fadeIn(500);
}else{
var value = $('#phone').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var intRegex = /^\d+$/;
if(!intRegex.test(value)) {
var error = true;
$('.error').fadeIn(500);
$('.pStar').fadeIn(500);
} else{
$('.pStar').fadeOut(500);
}
}
推荐答案
I would highly recommend using jQuery validate rather than re-inventing the wheel
使用jQuery快速Google搜索英国号码验证了这一点
a quick google search for uk numbers with jquery validate turned this up
看起来像这样
jQuery.validator.addMethod('phoneUK', function(phone_number, element) {
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})$/);
}, 'Please specify a valid phone number'
);
这篇关于jQuery表单验证-电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文