$(function () { var errMsg; $.each($("input"), function (i, val) { $(val).blur(function () { if ($(val).attr("name") == "userName") { $(".nameMsg").remove(); var userName = val.value; var regName = /[a-zA-Z]\w{4,16}/ if (userName == "" || userName.trim() == "") { errMsg = "<span>用户名不能为空</span>"; } else if (!regName.test(userName)) { errMsg = "<span>由英文字母和数字组成的4-16位字符,以字母开头</span>"; } else { errMsg = "<span>OK!</span>"; } $(this).parent().append(errMsg); } else if ($(val).attr("name") == "nickName") { $(".nickMsg").remove(); var nickName = val.value; var regName = /[\u4e00-\u9fa5]{2,6}/ if (nickName == "" || nickName.trim() == "") { errMsg = "<span>昵称不能为空</span>"; } else if (!regName.test(nickName)) { errMsg = "<span>由2-6个汉字组成</span>"; } else { errMsg = "<span>OK!</span>"; } $(this).parent().append(errMsg); } else if ($(val).attr("name") == "email") { $(".emailMsg").remove(); var email = val.value; var regEmail = /^\w+@\w+((\.\w+)+)$/; if (email == "" || email.trim() == "") { errMsg = "<span>邮箱不能为空</span>"; } else if (!regEmail.test(email)) { errMsg = "<span>邮箱账号@域名。如good@tom.com、whj@sina.com.cn</span>"; } else { errMsg = "<span>OK!</span>"; } $(this).parent().append(errMsg); } else if ($(val).attr("name") == "pwd") { $(".pwdMsg").remove(); var pwd = val.value; var regPwd = /^\w{4,10}$/; if (pwd == "" || pwd.trim() == "") { errMsg = "<span>密码不能为空</span>"; } else if (!regPwd.test(pwd)) { errMsg = "<span>格式错误</span>"; } else { errMsg = "<span>OK!</span>"; } $(this).parent().append(errMsg); } else if ($(val).attr("name") == "pwd2") { $(".pwd2Msg").remove(); var pwd2 = val.value; var pwd = $("input")[3].value; if (pwd2 == "" || pwd2.trim() == "" || !pwd2.equals(pwd)) { errMsg = "<span>两次输入密码不一致</span>"; } else { errMsg = "<span>OK!</span>"; } $(this).parent().append(errMsg); } else if ($(val).attr("name") == "phone") { $(".phoneMsg").remove(); var phone = val.value; var regPhone = /[13,15,18]\d{9}/; if (phone == "" || phone.trim() == "") { errMsg = "<span class = 'phoneMsg' style = 'color:red;' > 手机号不能为空 < /span>" } else if (!regPhone.test(phone)) { errMsg = "<span class = 'phoneMsg' style = 'color:red;' > 格式错误 < /span>" } else { errMsg = "<span class = 'phoneMsg' style = 'color:red;' > OK! < /span>" } $(this).parent().append(errMsg); } else if ($(val).attr("name") == "date") { $(".dateMsg").remove(); var birthday = val.value; var regDate = /(199\d|200\d)[-/](0\d|1[0-2])[-/](0\d|[1-2]\d|30|31)/; if (birthday == "" || birthday.trim() == "") { errMsg = "<span>请输入生日</span>"; } else if (!regDate.test(birthday)) { errMsg = "<span>格式错误</span>"; } else { errMsg = "<span>OK!</span>"; } $(this).parent().append(errMsg); } }); }); });
总结
以上所述是小编给大家介绍的JS+jQuery实现注册信息的验证功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章: