JQuery.validate⾃定义校验分享数组中英⽂⽂件后缀等等//项⽬中统⼀使⽤的校验,分享给⼤家
jQuery.validator.addMethod("mobilePhone",function(value,element){
return this.optional(element) || /^(13|15|18)\d{9}$/.test(value);
},"请输⼊正确的⼿机号码!");
jQuery.validator.addMethod("phone",function(value,element){
return this.optional(element) || /^(\d{8}|\d{7})$/.test(value);
},"请输⼊正确的电话号码!");
//验证电话号码,包括验证国内区号,国际区号,分机号
jQuery.validator.addMethod("telephone",function(value,element){
return this.optional(element) || /^(\d{3,4}-)?\d{6,8}(-\d{3,5})?$/.test(value);
},"请输⼊正确的电话号码");
jQuery.validator.addMethod("fax",function(value,element){
return this.optional(element) || /^(\d{8}|\d{7})$/.test(value);
},"请输⼊正确的传真号!");
to用法jQuery.validator.addMethod("regionNum",function(value,element){
return this.optional(element) || /^0\d{2,3}$/.test(value);
},"请输⼊正确的区号!");
jQuery.validator.addMethod("checkEmail",function(value,element){
return true;
},"请输⼊正确的邮箱!");
jQuery.validator.addMethod("carNum1",function(value,element){
return this.optional(element) || /^[\u4e00-\u9fa5]([A-Za-z]{1})[\u0000-\u00FF]{5}$/.test(value);
},"请输⼊正确的车牌号码!");
jQuery.validator.addMethod("carNum",function(value,element){
return this.optional(element) || /^[\u4e00-\u9fa5\da-zA-Z\-\_]+$/.test(value);
},"不能输⼊特殊字符!");
jQuery.validator.addMethod("normalwords",function(value,element){
return this.optional(element) || /^\w+$/.test(value);
},"匹配由数字、26个英⽂字母或者下划线组成的字符串");
jQuery.validator.addMethod("upca",function(value,element){
return this.optional(element) || /^[A-Z]+$/.test(value);
},"匹配由26个英⽂字母的⼤写组成的字符串");
jQuery.validator.addMethod("lowca",function(value,element){
return this.optional(element) || /^[a-z]+$/.test(value);
},"匹配由26个英⽂字母的⼩写写组成的字符串");
//验证百分⽐
jQuery.validator.addMethod("checkPercent", function(value, element) {
groovevar reg = /^-?\d+%$/;
return this.optional(element) || st(value);
}, "\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u767e\u5206\u6bd4!");
//验证英⽂
强调句练习
jQuery.validator.addMethod("checkENG", function(value, element) {
var reg = /^[a-zA-Z](\s*[a-zA-Z])*$/;
return this.optional(element) || st(value);
}, "\u8bf7\u8f93\u5165\u82f1\u6587!");
//验证中英⽂数字
jQuery.validator.addMethod("checkENGCHNNUM", function(value, element) {
var reg = /^[0-9a-zA-Z\u4E00-\u9FA5]+$/;
return this.optional(element) || st(value);
}, "请输⼊正确的字符、数字或字母!");
//验证中英⽂ add by ruijie.chen
jQuery.validator.addMethod("checkENGCHN", function(value, element) { var reg = /^[a-zA-Z\u4E00-\u9FA5]+$/;
return this.optional(element) || st(value);
}, "请输⼊正确的中⽂或英⽂!");
//验证英⽂字母和数字
jQuery.validator.addMethod("checkENGNUM", function(value, element) { var reg = /^[0-9a-zA-Z]+$/;
return this.optional(element) || st(value);
},"请输⼊正确的英⽂字母或数字!");
//只允许汉字、英⽂字母、数字及下划线
jQuery.validator.addMethod("checkTSNUM", function(value, element) { var reg = /^[\u0391-\uFFE5\w]+$/;
return this.optional(element) || st(value);
},"只允许汉字、英⽂字母、数字及下划线");
//验证中⽂
jQuery.validator.addMethod("checkCHN", function(value, element) {
var reg = /^[\u4e00-\u9fa5]+$/;
return this.optional(element) || st(value);
}, "\u8bf7\u8f93\u5165\u4e2d\u6587!");
//验证不能有中⽂
jQuery.validator.addMethod("checkHasCHN", function(value, element) { var reg = /[\u4E00-\u9FA5]/g;
st(value)){
return fal;
}el{
return true;
}
}, "不能包含中⽂字符");
//验证不能为数字
jQuery.validator.addMethod("checkNotNum", function(value, element) { var reg = /(^[^0-9]+$)/;
return this.optional(element) || st(value);
}, "\u4e0d\u80fd\u586b\u5199\u6570\u5b57!");
//验证⼤于0的数字
jQuery.validator.addMethod("checkNum", function(value, element) {
var reg = /^[1-9]+?[0-9]*$/;
return this.optional(element) || st(value);
}, "\u8bf7\u8f93\u5165\u5927\u4e8e\u96f6\u7684\u6574\u6570");
//检验是否全为数字
jQuery.validator.addMethod("isNum", function(value, element) {
var numtype = "0123456789";
for (i = 0; i < value.length; i++) { //检查是否有不在 0123456789之內的字 if (numtype.indexOf(value.charAt(i)) < 0) {
return fal;
}
}
return true;
}, "请输⼊数字!");
//检验投保单
jQuery.validator.addMethod("isNum_policy", function(value, element) { var numtype = "0123456789";
for (i = 0; i < value.length; i++) { //检查是否有不在 0123456789之內的字 if (numtype.indexOf(value.charAt(i)) < 0) {
return fal;
}
}
return true;
}, "请输⼊正确的投保单号!");
//校验24⼩时时间如: 00:00
jQuery.validator.addMethod("checkTime", function(value, element) {
var intt = value.indexOf(":");
var befor = value.substring(0, intt);
var after = value.substring(intt + 1, intt + 3);
if (befor >= 24 || befor.length != 2 || after >= 60 || after.length != 2) {
return fal;
}
return true;
}, "请输⼊正确的时间格式,如:00:00!");
//校验Double
jQuery.validator.addMethod("checkDouble", function(value, element) {
//var reg1 = /^\d+$/;
var reg = /^(([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9]))$/; //update by ruijie.chen 2010-03-01
return this.optional(element) || st(value);
}, "请输⼊Double型数字");
//校验邮政编码
jQuery.validator.addMethod("checkPost", function(value, element) {
var reg = /^[0-9]\d{5}$/;
return this.optional(element) || st(value);
}, "\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u90ae\u653f\u7f16\u7801!");
//校验包含中⽂字符的字符串的长度,中⽂每个字符算2个字符长度,英⽂为1个字符长度
jQuery.validator.addMethod("maxlengthC", function(value, element, param) {
return this.optional(element) || place(/[^\x00-\xff]/g,"**").length <= param;
}, $.validator.format("允许最⼤字符为 {0},汉字为2个字符!"));
//校验⽇期格式:yyyyMM
jQuery.validator.addMethod("date_yyyyMM", function(value, element) {
return this.optional(element) || /^\d{4}(0[1-9])|(1[0-2])$/.test(value);
}, $.validator.format("请输⼊正确的⽇期格式,如:201003"));
//校验⽇期格式:yyyy-MM-dd
jQuery.validator.addMethod("date_yyyy-MM-dd", function(value, element) {
return this.optional(element) || /^\d{4}-\d{2}-\d{2}$/.test(value);
}, $.validator.format("请输⼊正确的⽇期格式,如:2010-03-09"));
//校验JAVA包名格式: 由⼩写英⽂单词和点组成,单词最多50个字母(系统⼀般⽀持200)
jQuery.validator.addMethod("checkPackageName", function(value, element) {
acceptabilityreturn this.optional(element) || /^[a-z]{1,50}(\.[a-z]{1,50})*$/.test(value);
}, $.validator.format("请输⼊正确的包名格式,如:wtouch.dictionary"));
//校验JAVA类名由英⽂字母和数字组成,必须以字母开头,不能使⽤JAVA保留字(保留字均由⼩写英⽂组成)
jQuery.validator.addMethod("checkClassName", function(value, element) {
return this.optional(element) || /^[a-zA-Z]([a-zA-Z0-9])*$/.test(value);
}, $.validator.format("请输⼊正确的类名格式,如:HelloWorld"));
//校验⽇期格式:yyyy-MM-dd
jQuery.validator.addMethod("dateDate", function(value, element) {
return this.optional(element) || /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30 }, $.validator.format("请输⼊正确的⽇期格式,如:2010-01-20"));
//校验⼤于0,的⾦额数字,最多保留2位⼩数点若必须设置2位⼩数点则:^((\d{1,3}(,\d{3})+?|\d+)(\.\d{2})?|(\.\d{2}))$
jQuery.validator.addMethod("checkMeny", function(value, element) {
return this.optional(element) || /^((\d{1,3}(,\d{3})+?|\d+)(\.\d{1}|\.\d{2})?|(\.\d{1}|\.\d{2}))$/.test(value);
advent}, "请输⼊正确的⾦额,最多保留⼆位⼩数");
//校验⼤于0,的⾦额数字,最多保留2位⼩数点若必须设置2位⼩数点则:^((\d{1,3}(,\d{3})+?|\d+)(\.\d{2})?|(\.\d{2}))$
jQuery.validator.addMethod("checkPolicyMeny", function(value, element) {
return this.optional(element) || /^\d{1,6}([\.]\d{1,4})?$/.test(value);
}, "请输⼊正确的⾦额,最⼤值为999999.9999。");
//校验⼤于0,的⾦额数字,最多保留2位⼩数点若必须设置2位⼩数点则:^((\d{1,3}(,\d{3})+?|\d+)(\.\d{2})?|(\.\d{2}))$
jQuery.validator.addMethod("checkTrainingMeny", function(value, element) {
return this.optional(element) || /^\d{1,6}([\.]\d{1,4})?$/.test(value);
}, "请输⼊正确的⾦额");
//培训和招募校验使⽤:校验⼤于0,number(10,2)的数字,最多保留2位⼩数点
jQuery.validator.addMethod("checkTenTwoNumber", function(value, element) {
return this.optional(element) || /^\d{1,8}([\.]\d{1,2})?$/.test(value);
}, "请输⼊0~99999999.99內的⾦额");
//校验⼤于0,⽤户⾝⾼,最多3位整数保留1位⼩数点
jQuery.validator.addMethod("checkHeight", function(value, element) {
return this.optional(element) || /^\d{1,3}([\.]\d{1})?$/.test(value);
}, "请输⼊正确的⾝⾼,最多保留⼀位⼩数");
//校验⼤于0,⽤户⾝⾼,最多3位整数保留2位⼩数点
jQuery.validator.addMethod("checkWeight", function(value, element) {
return this.optional(element) || /^\d{1,3}([\.]\d{1,2})?$/.test(value);
}, "请输⼊正确的体重,最多保留⼆位⼩数");
//校验百分⽐1-100之间整数+最多保留2为⼩数点不加%号不⽀持=0.00 -->
//加百分号^([1-9][0-9]?(\.[0-9]{1,2})?)%$|^(0\.[1-9][0-9]?)%$|^(0\.[0-9][1-9])%$|^100(\.0|\.00)?%$
jQuery.validator.addMethod("checkPercent", function(value, element) {
return this.optional(element) || /^([1-9][0-9]?(\.[0-9]{1,2})?)$|^(0\.[1-9][0-9]?)$|^(0\.[0-9][1-9])$|^100(\.0|\.00)?$/.test(value); }, "请输⼊正确的百分⽐值,0.01-100");英语集训营
//校验年龄1-120
jQuery.validator.addMethod("checkAge", function(value, element) {
return this.optional(element) || /^(?:[1-9][0-9]?|1[01][0-9]|120)$/.test(value);
}, "请输⼊正确的年龄");
jQuery.validator.addMethod("checkProFee", function(value, element) {
上海职场英语培训排名return this.optional(element) || /^(0\.([0-9]){1,4}|1)$/.test(value);
}, "请输⼊正确的⾦额,数值⼤⼩为0到1,最多4位⼩数");
//判断出⽣⽇期⼩于等于今天
jQuery.validator.addMethod("checkBirthday",function(value,element){
var now = new Date();
var date = value+' 23:59:59';
if(Date.par(value)-Date.par(now)>0){
return fal
teambuilding}
美容学校那里好
return true;
}, "出⽣⽇期不能⼤于今天");
//检验⽂件上传类型
$(document).ready(function() {
$('input[type="file"]').change(function(){
checkEXT($(this));
});
//校验样式效果,⽂本框获取焦点,隐藏相应错误信息
$("form").find("input").each(function(){
$(this).click(function(){
var _this=$(this);
if(_this.hasClass("error")){
_veClass("error");
var labelAR = _this.parent().find("label[class='error']");
}
});
});
});
function checkEXT(element){
var file = element;
var ext = file.val().substr(file.val().lastIndexOf(".")).toLowerCa();
if(ext ==".exe" || ext==".bat" || ext==".com" || ext==".pif" || ext==".scr"){ alert("不能上传后缀名为"+ext+"的⽂件");
file.after(file.clone());
$('input[type="file"]').change(function(){
checkEXT($(this));
});
}takecareofmyeyes
}