JS验证中文姓名,手机号

验证文本框输入的是否是姓名

JS代码:

function checkChinese(str) { var re = /[^\u4e00-\u9fa5]/; if (re.test(str)) return false; return true; }; function checkChineseName(v) { if (v == '') return 1; if (v.length < 2) { return 2; } var name = v.replace(/·/g, ''); name = name.replace(/•/g, ''); if(checkChinese(name))  return 0; else return 2; };

 

 
验证填入的必须是手机号
 
JS代码:
function checkMobileStrict(v) { var yd = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '187', '188']; var lt = ['130', '131', '132', '155', '156', '185', '186']; var dx = ['133', '153', '180', '189']; var whole = []; whole = whole.concat(yd, lt, dx); if (v == '') return 1; if (v.length != 11) { return 2; } if (isNaN(v)) { return 2; } var phone_sect = v.substr(0, 3); var find = false; var i = 0; for (i = 0; (i < whole.length); i++) { if (phone_sect == whole[i]) { find = true; break; } } if (find) return 0; else return 2; }

 

 
调用jQuery方法:
 
JS代码:
jQuery().ready(function() { var firstNameErr = "请正确填写您的中文姓名。"; var phoneErr = "请正确填写您的手机号码。"; function checkTheFClass() { if (checkCommonField()) { $("form").submit(); return true;} function checkCommonField() { $("#Name").removeClass("error");   $("#Phone").removeClass("error"); if (checkChineseName($("#Name").val())) { alert(firstNameErr); $("#Name").addClass("error"); return false; }; if (checkMobileStrict($("#Phone").val())) { alert(phoneErr); $("#Phone").addClass("error"); return false; }; return true; }; $('#btn_Submit').click(function() { checkTheFClass(); return false; }); });

 

 
HTML代码:
<form name="aspnetForm" method="get" action="/wapLog.aspx" id="aspnetForm" >

<p>客户姓名:<input name="Ext1" type="text" id="Name" size="15" /></p>

<p>手机号码:<input name="Ext2" type="text" id="Phone" size="15" /></p>

<input type="submit" name="button" id="btn_Submit" value="提交" />

</form>

 

你可能感兴趣的:(js)