jquery表单验证
无聊的我今天竟然无聊到又去写表单验证了
顺便吐槽星期六前端群里的出来面基了,宅毛线,出来(。・∀・)ノ゙嗨
//表单验证基于Jquery beat1.0 兼容IE6 //QQ:1035417613 //Author:JounreyYao //Data:2015-07-02 ;(function($){ $.fn.extend({ "FormName":function(){ var name = $(this).val(); if(name==""){ $(this).siblings(".warning").show().siblings(".Notice").hide(); } else if(/^[\u4e00-\u9fa5]+$/i.test(name)){ $(this).siblings(".process").show().siblings(".Notice").hide(); }else{ $(this).siblings(".danger").show().siblings(".Notice").hide(); } }, "DataYear":function(){ var reg=/^\d{5}$/; var val = $(this).val(); if(val==""){ $(this).siblings(".warning").text("请输入年") $(this).siblings(".warning").show().siblings(".Notice").hide(); }else if(isNaN(val)){ $(this).siblings(".warning").text("请输入正确的日期,如2010年1月1日"); $(this).siblings(".warning").show().siblings(".Notice").hide(); } }, "DataMouth":function(){ var reg=/^\d{5}$/; var val = $(this).val(); if(val==""){ $(this).siblings(".warning").text("请输入月份"); $(this).siblings(".warning").show().siblings(".Notice").hide(); }else if(isNaN(val)){ $(this).siblings(".warning").text("请输入正确的月份,如2010年1月1日"); $(this).siblings(".warning").show().siblings(".Notice").hide(); }else if(val>12||val==0){ $(this).siblings(".warning").text("请输入正确的日期,如2010年1月1日"); $(this).siblings(".warning").show().siblings(".Notice").hide(); } }, "DataDay":function(){ var reg=/^\d{5}$/; var val = $(this).val(); if(val==""){ $(this).siblings(".warning").text("请输入日期"); $(this).siblings(".warning").show().siblings(".Notice").hide(); }else if(isNaN(val)){ $(this).siblings(".warning").text("请输入正确的日期,如2010年1月1日"); $(this).siblings(".warning").show().siblings(".Notice").hide(); }else if(val>31||val==0){ $(this).siblings(".warning").text("请输入正确的日期,如2010年1月1日"); $(this).siblings(".warning").show().siblings(".Notice").hide(); } }, "Unit":function(){ var val = $(this).val(); if(val==""){ $(this).siblings(".warning").text("请输入正确的格式"); $(this).siblings(".warning").show().siblings(".Notice").hide(); }else if(isNaN(val)){ $(this).siblings(".warning").text("请输入数字"); $(this).siblings(".warning").show().siblings(".Notice").hide(); }else{ $(this).siblings(".process").show().siblings(".Notice").hide(); } }, "Edition":function(){ $(this).children(".option").click(function(){ $(this).siblings(".selectList").toggle() }) }, "EditionSelect":function(){ $(this).click(function(){ var val = $(this).html(); $(this).parent(".selectList").siblings(".EditionOption").val(val); $(this).parent(".selectList").siblings(".option").html(val); $(this).parent(".selectList").hide() }) }, "NullText":function(){ var name = $(this).val(); if(name==""){ $(this).siblings(".warning").show().siblings(".Notice").hide(); } else{ $(this).siblings(".process").show().siblings(".Notice").hide(); } }, "NullHidden":function(){ var name = $(this).val(); if(name==""){ $(this).parent(".Edition").siblings(".warning").show().siblings(".Notice").hide(); } else{ $(this).parent(".Edition").siblings(".process").show().siblings(".Notice").hide(); } }, "EmallForm":function(){ var name = $(this).val(); if(name==""){ $(this).siblings(".warning").text("请输入邮箱"); $(this).siblings(".warning").show().siblings(".Notice").hide(); } else{ var pattern = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; if (!pattern.test(name)) { $(this).siblings(".warning").text("请输入正确的邮箱"); $(this).siblings(".warning").show().siblings(".Notice").hide(); } else{ $(this).siblings(".process").show().siblings(".Notice").hide(); } } }, "allForm":function(){ $(".sofeFormBox .sofeForm .name").FormName(); $(".sofeFormBox .sofeForm .username").FormName(); $(".sofeFormBox .sofeForm .ProductName").NullText(); $(".sofeFormBox .sofeForm .InputTel").NullText(); $(".sofeFormBox .sofeForm .ProductName").NullText(); $(".sofeFormBox .sofeForm .EmallInput").EmallForm(); $(".sofeFormBox .sofeForm .dataYear").DataYear(); $(".sofeFormBox .sofeForm .dataMouth").DataMouth(); $(".sofeFormBox .sofeForm .dataDay").DataDay(); $(".sofeFormBox .sofeForm .InputFormUnit").Unit(); $(".sofeFormBox .sofeForm .Edition").Edition(); $(".sofeFormBox .sofeForm .EditionOption").NullHidden(); } }) })(jQuery) $(function(){ $(".sofeFormBox .sofeForm .name").blur(function(){ $(this).FormName(); }) $(".sofeFormBox .sofeForm .username").blur(function(){ $(this).FormName(); }) $(".sofeFormBox .sofeForm .ProductName").blur(function(){ $(this).NullText(); }) $(".sofeFormBox .sofeForm .InputTel").blur(function(){ $(this).NullText(); }) $(".sofeFormBox .sofeForm .ProductName").blur(function(){ $(this).NullText(); }) $(".sofeFormBox .sofeForm .EmallInput").blur(function(){ $(this).EmallForm(); }) $(".sofeFormBox .sofeForm .dataYear").blur(function(){ $(this).DataYear(); }) $(".sofeFormBox .sofeForm .dataMouth").blur(function(){ $(this).DataMouth(); }) $(".sofeFormBox .sofeForm .dataDay").blur(function(){ $(this).DataDay(); }) $(".sofeFormBox .sofeForm .InputFormUnit").blur(function(){ $(this).Unit(); }) $(".sofeFormBox .sofeForm .Edition").blur(function(){ $(this).Edition(); }) $(".sofeFormBox .sofeForm .Edition .selectList .select").blur(function(){ $(this).EditionSelect(); }) $(".sofeFormBox .sofeForm .dataYear,.sofeFormBox .sofeForm .dataMouth,.sofeFormBox .sofeForm .dataDay").focus(function(){ $(this).siblings(".warning").hide(); }) $(".sofeForm .SubmitBtn").click(function(){ $(this).allForm(); }) }) function clsIDCard(CardNo) { this.Valid = false; this.ID15 = ''; this.ID18 = ''; this.Local = ''; if (CardNo != null) this.SetCardNo(CardNo); } // 设置身份证号码,15位或者18位 clsIDCard.prototype.SetCardNo = function(CardNo) { this.ID15 = ''; this.ID18 = ''; this.Local = ''; CardNo = CardNo.replace(" ", ""); var strCardNo; if (CardNo.length == 18) { pattern = /^\d{17}(\d|x|X)$/; if (pattern.exec(CardNo) == null) return; strCardNo = CardNo.toUpperCase(); } else { pattern = /^\d{15}$/; if (pattern.exec(CardNo) == null) return; strCardNo = CardNo.substr(0, 6) + '19' + CardNo.substr(6, 9) strCardNo += this.GetVCode(strCardNo); } this.Valid = this.CheckValid(strCardNo); } // 校验身份证有效性 clsIDCard.prototype.IsValid = function() { return this.Valid; } // 返回生日字符串,格式如下,1981-10-10 clsIDCard.prototype.GetBirthDate = function() { var BirthDate = ''; if (this.Valid) BirthDate = this.GetBirthYear() + '-' + this.GetBirthMonth() + '-' + this.GetBirthDay(); return BirthDate; } // 返回生日中的年,格式如下,1981 clsIDCard.prototype.GetBirthYear = function() { var BirthYear = ''; if (this.Valid) BirthYear = this.ID18.substr(6, 4); return BirthYear; } // 返回生日中的月,格式如下,10 clsIDCard.prototype.GetBirthMonth = function() { var BirthMonth = ''; if (this.Valid) BirthMonth = this.ID18.substr(10, 2); if (BirthMonth.charAt(0) == '0') BirthMonth = BirthMonth.charAt(1); return BirthMonth; } // 返回生日中的日,格式如下,10 clsIDCard.prototype.GetBirthDay = function() { var BirthDay = ''; if (this.Valid) BirthDay = this.ID18.substr(12, 2); return BirthDay; } // 返回性别,1:男,0:女 clsIDCard.prototype.GetSex = function() { var Sex = ''; if (this.Valid) Sex = this.ID18.charAt(16) % 2; return Sex; } // 返回15位身份证号码 clsIDCard.prototype.Get15 = function() { var ID15 = ''; if (this.Valid) ID15 = this.ID15; return ID15; } // 返回18位身份证号码 clsIDCard.prototype.Get18 = function() { var ID18 = ''; if (this.Valid) ID18 = this.ID18; return ID18; } // 返回所在省,例如:上海市、浙江省 clsIDCard.prototype.GetLocal = function() { var Local = ''; if (this.Valid) Local = this.Local; return Local; } clsIDCard.prototype.GetVCode = function(CardNo17) { var Wi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1); var Ai = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); var cardNoSum = 0; for (var i = 0; i < CardNo17.length; i++) cardNoSum += CardNo17.charAt(i) * Wi[i]; var seq = cardNoSum % 11; return Ai[seq]; } clsIDCard.prototype.CheckValid = function(CardNo18) { if (this.GetVCode(CardNo18.substr(0, 17)) != CardNo18.charAt(17)) return false; if (!this.IsDate(CardNo18.substr(6, 8))) return false; var aCity = { 11 : "北京", 12 : "天津", 13 : "河北", 14 : "山西", 15 : "内蒙古", 21 : "辽宁", 22 : "吉林", 23 : "黑龙江 ", 31 : "上海", 32 : "江苏", 33 : "浙江", 34 : "安徽", 35 : "福建", 36 : "江西", 37 : "山东", 41 : "河南", 42 : "湖北 ", 43 : "湖南", 44 : "广东", 45 : "广西", 46 : "海南", 50 : "重庆", 51 : "四川", 52 : "贵州", 53 : "云南", 54 : "西藏 ", 61 : "陕西", 62 : "甘肃", 63 : "青海", 64 : "宁夏", 65 : "新疆", 71 : "台湾", 81 : "香港", 82 : "澳门", 91 : "国外" }; if (aCity[parseInt(CardNo18.substr(0, 2))] == null) return false; this.ID18 = CardNo18; this.ID15 = CardNo18.substr(0, 6) + CardNo18.substr(8, 9); this.Local = aCity[parseInt(CardNo18.substr(0, 2))]; return true; } clsIDCard.prototype.IsDate = function(strDate) { var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/); if (r == null) return false; var d = new Date(r[1], r[2] - 1, r[3]); return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[2] && d .getDate() == r[3]); } $(function(){ $("#IdCard").blur(function(event){ var idCard = $(this).val(); var checkFlag = new clsIDCard(idCard); if( !checkFlag.IsValid() ){ $(this).siblings(".warning").text("请输入正确的身份证号"); $(this).siblings(".warning").show().siblings(".Notice").hide(); return false; }else{ $(this).siblings(".process").show().siblings(".Notice").hide() } }); });
我只想静静的撸会儿代码,但是总监大大又吐槽我了,你搞得那么复杂干嘛。。。
起因只是我想问他时间的年是要15还是2015
然后撸了个半成品。。。。
额,写上这一段的时候主要还是不明白,好多点反对是对我的随笔不满还是对我随笔质量的不满。。实际上我更多的把这个博客当成个人动态玩的。。。所以还是比较随意的。。。当然如果觉得写得有问题可以在下方评论写出改进建议。。。我会虚心接受,顺便可以知道自己的不足点。。。
分类:
JavaScript进阶篇
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库