jquery验证手机号码和固定电话号码

时间:2022-03-22 04:30:43
<pre name="code" class="javascript"> //验证手机号码或者电话号码
function checkContactNumber() {
$("#error").css("display", "none");
var mobile = $.trim($("#ContactNumber").val());
var isMobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
var isPhone = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;;
var error = "<label id=\"error\" class=\"validate_input_error\">请正确填写电话号码,例如:13511111111或010-11111111</label>";
//如果为1开头则验证手机号码
if (mobile.substring(0, 1) == 1) {
if (!isMobile.exec(mobile) && mobile.length != 11) {
$("#ContactNumber").after(error);
$("#ContactNumber").focus();
return false;
}
}
//如果为0开头则验证固定电话号码
else if (mobile.substring(0, 1) == 0) {
if (!isPhone.test(mobile)) {
$("#ContactNumber").after(error);
$("#ContactNumber").focus();
return false;
}
}
//否则全部不通过
else {
$("#ContactNumber").after(error);
$("#ContactNumber").focus();
return false;
}
return true;
}