js 格式验证总结
1.身份证号验证
var Common = { //身份证号验证 IsIdCardNo: function (IdCard) { var reg = /^\d{15}(\d{2}[0-9X])?$/i; if (!reg.test(IdCard)) { return false; } if (IdCard.length == 15) { var n = new Date(); var y = n.getFullYear(); if (parseInt("19" + IdCard.substr(6, 2)) < 1900 || parseInt("19" + IdCard.substr(6, 2)) > y) { return false; } var birth = "19" + IdCard.substr(6, 2) + "-" + IdCard.substr(8, 2) + "-" + IdCard.substr(10, 2); if (!Common.IsDate(birth)) { return false; } } if (IdCard.length == 18) { var n = new Date(); var y = n.getFullYear(); if (parseInt(IdCard.substr(6, 4)) < 1900 || parseInt(IdCard.substr(6, 4)) > y) { return false; } var birth = IdCard.substr(6, 4) + "-" + IdCard.substr(10, 2) + "-" + IdCard.substr(12, 2); if (!Common.IsDate(birth)) { return false; } iW = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); iSum = 0; for (i = 0; i < 17; i++) { iC = IdCard.charAt(i); iVal = parseInt(iC); iSum += iVal * iW[i]; } iJYM = iSum % 11; if (iJYM == 0) sJYM = "1"; else if (iJYM == 1) sJYM = "0"; else if (iJYM == 2) sJYM = "x"; else if (iJYM == 3) sJYM = "9"; else if (iJYM == 4) sJYM = "8"; else if (iJYM == 5) sJYM = "7"; else if (iJYM == 6) sJYM = "6"; else if (iJYM == 7) sJYM = "5"; else if (iJYM == 8) sJYM = "4"; else if (iJYM == 9) sJYM = "3"; else if (iJYM == 10) sJYM = "2"; var cCheck = IdCard.charAt(17).toLowerCase(); if (cCheck != sJYM) { return false; } } return true; }, //日期验证 IsDate: function (strDate) { var strSeparator = "-"; //日期分隔符 var strDateArray; var intYear; var intMonth; var intDay; var boolLeapYear; strDateArray = strDate.split(strSeparator); if (strDateArray.length != 3) return false; intYear = parseInt(strDateArray[0], 10); intMonth = parseInt(strDateArray[1], 10); intDay = parseInt(strDateArray[2], 10); if (isNaN(intYear) || isNaN(intMonth) || isNaN(intDay)) return false; if (intMonth > 12 || intMonth < 1) return false; if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) return false; if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1)) return false; if (intMonth == 2) { if (intDay < 1) return false; boolLeapYear = false; if ((intYear % 100) == 0) { if ((intYear % 400) == 0) boolLeapYear = true; } else { if ((intYear % 4) == 0) boolLeapYear = true; } if (boolLeapYear) { if (intDay > 29) return false; } else { if (intDay > 28) return false; } } return true; } };
2.QQ
Common.Valid = { //验证qq号码 QQNum: function (str) { var reg = /^\s*\d{4,12}\s*$/g; return reg.test(str); }, //空或有效格式验证器 EmptyOrValid: function (validFunc) { var func = validFunc; if ($.type(validFunc) == "string") func = this[$.trim(validFunc)]; return function (str) { return $.trim(str) === "" || func(str); } } };
3.手机、电话、传真验证
$(function () { String.prototype.isMobile = function () { return (/^(?:13\d|15[012356789]|18[0256789]|147)-?\d{5}(\d{3}|\*{3})$/.test($.trim(this))); } String.prototype.isTel = function () { //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(1-4位)" return (/^(([0\+]\d{2,3}-)?(0\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,4})?$/.test($.trim(this))); } String.prototype.isTelMobile = function () { //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(1-4位)" return (/^(([0\+]\d{2,3}-)?(0\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,4})?|(?:13\d|15[012356789]|18[0256789]|147)-?\d{5}(\d{3}|\*{3}))?$/.test($.trim(this))); } });
使用示例:
if ($.trim($("#Phone").val()) != "" || $.trim($("#Fax").val()) != "") { if (!$.trim($("#Phone").val()).isTel()) { Common.ShowInfo("请输入正确的电话号码!"); return; } if (!$.trim($("#Fax").val()).isTel()) { Common.ShowInfo("请输入正确的传真号!"); return; } } if ($.trim($("#MobilePhone").val()) != "") { if (!$.trim($("#MobilePhone").val()).isMobile()) { Common.ShowInfo("请输入正确的手机号码!"); return; } }
$(function () {
});
4. jquery 常用验证
$(function () { // jQuery.validator增加IP验证 jQuery.validator.addMethod("code", function (value, element) { return this.optional(element) || (/^[a-zA-Z0-9]+$/.test(value)); }, "请输入英文、数字"); //jQuery.validator增加手机验证
jQuery.validator.addMethod("isMobile", function (value, element) {
var pattern = /^(?:13\d|15[012356789]|18[0256789]|147)-?\d{5}(\d{3}|\*{3})$/;
return this.optional(element) || (pattern.test(value));
}, "请输入英文、数字");
var v = $('#frmInput').validate({ rules: {
Name:{ required: false, code: true },//
Email: { required: false, email: true },
mobile:{ required: false, isMobile: true } }, messages: {
Name: { required: "请输入用户名!", email: "请输入正确的用户名!" },
Email: { required: "请输入邮箱!", email: "请输入正确的邮箱地址!" },
mobile:{ required: false, isMobile: "输入正确的手机号!"} } }); });
使用示例:
<script type="text/javascript">
$(function () {
//以上的验证代码
});
var Input = {}; Input.Save = function (e) { var frm = $(e).parents("form").first(); if (!$(frm).valid()) return; $.post($(frm).attr("action"), $(frm).serialize(), function (data) { if (data.IsOK) { alert(data.Description); } else alert(data.Description); }); }
</script>
<form name="frmInput" id="frmInput" method=post action="@Url.Action(@ViewContext.RouteData.Values["Action"].ToString())" >
</form>
5.多行文本框的长度进行验证
<script type="text/javascript"> //对textarea 长度进行验证 jQuery.fn.checkLength = function (parameters) { defaults = { min: 1, max: 120 };//设置默认值 jQuery.extend(defaults, parameters); // 当前textarea 的值 var taValue = $(this).val(); var len = taValue.length; if (len >= defaults.max) { return false; } else if (len <= defaults.min) { return false; } else { return true; } } var Input = {}; Input.Save = function (e) {
if (!$("#Content").checkLength({ min: 1, max: 9999 })) {
alert("内容不能为空!");
return;
} }; </script >
5. 密码验证(不能包含空格)
String.prototype.hasSpace = function () {
return (/(^\s+)|(\s+$)|(^\S*\s*\S$)/.test($.trim(this)));
};
if($("#Password").val().hasSpace()){//判断
//
}