用JS和JQuery實現(xiàn)的效果是一樣的。
站在用戶的角度思考問題,與客戶深入溝通,找到高淳網(wǎng)站設(shè)計與高淳網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋高淳地區(qū)。
HTML代碼
CSS樣式
body { text-align: center; padding: 0; margin: 0; } fieldset { width: 800px; } table tr td ~ td { text-align: left; width: 600px; }
JS代碼
//驗證用戶名 function check_userName() { var userName = document.getElementById("name").value; var regName = /[a-zA-Z]\w{4,16}/ if (userName == "" || userName.trim() == "") { document.getElementById("err_name").innerHTML = "請輸入用戶名"; return false; } else if (!regName.test(userName)) { document.getElementById("err_name").innerHTML = "由英文字母和數(shù)字組成的4-16位字符,以字母開頭"; return false; } else { document.getElementById("err_name").innerHTML = "ok!!!"; return true; } } //驗證昵稱 function check_nickName() { var nickName = document.getElementById("nick").value; var regName = /[\u4e00-\u9fa5]{2,6}/ if (nickName == "" || nickName.trim() == "") { document.getElementById("err_nick").innerHTML = "請輸入昵稱"; return false; } else if (!regName.test(nickName)) { document.getElementById("err_nick").innerHTML = "由2-6個漢字組成"; return false; } else { document.getElementById("err_nick").innerHTML = "ok!!!"; return true; } } //驗證郵箱 function check_email() { var email = document.getElementById("email").value; var regEmail = /^\w+@\w+((\.\w+)+)$/; if (email == "" || email.trim() == "") { document.getElementById("err_email").innerHTML = "請輸入郵箱"; return false; } else if (!regEmail.test(email)) { document.getElementById("err_email").innerHTML = "郵箱賬號@域名。如good@tom.com、whj@sina.com.cn"; return false; } else { document.getElementById("err_email").innerHTML = "ok!!!"; return true; } } //驗證密碼 function check_pwd() { var pwd = document.getElementById("pwd").value; var regPwd = /^\w{4,10}$/; if (pwd == "" || pwd.trim() == "") { document.getElementById("err_pwd").innerHTML = "請輸入密碼"; return false; } else if (!regPwd.test(pwd)) { document.getElementById("err_pwd").innerHTML = "格式錯誤"; return false; } else { document.getElementById("err_pwd").innerHTML = "ok!!!"; return true; } } //確認(rèn)密碼 function check_pwd2() { var pwd = document.getElementById("pwd").value; var pwd2 = document.getElementById("pwd2").value; if (pwd2 == "" || pwd2.trim() == "") { document.getElementById("err_pwd2").innerHTML = "請輸入密碼"; return false; } else if (!pwd2.equals(pwd)) { document.getElementById("err_pwd2").innerHTML = "兩次輸入密碼不一致"; return false; } else { document.getElementById("err_pwd2").innerHTML = "ok!!!"; return true; } } //驗證手機號 function check_phone() { var phone = document.getElementById("phone").value; var regPhone = /[13,15,18]\d{9}/; if (phone == "" || phone.trim() == "") { document.getElementById("err_phone").innerHTML = "請輸入手機號"; return false; } else if (!regPhone.test(phone)) { document.getElementById("err_phone").innerHTML = "手機號由11位數(shù)字組成,且以13,15,18開頭"; return false; } else { document.getElementById("err_phone").innerHTML = "ok!!!"; return true; } } //驗證時間 function check_date() { var birthday = document.getElementById("birthday").value; var regDate = /[13,15,18]\d{9}/; if (birthday == "" || birthday.trim() == "") { document.getElementById("err_date").innerHTML = "請輸入日期"; return false; } else if (!regDate.test(birthday)) { document.getElementById("err_date").innerHTML = "出生日期在1990-2009之間"; return false; } else { document.getElementById("err_date").innerHTML = "ok!!!"; return true; } }
Jquery代碼
$(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 = "用戶名不能為空"; } else if (!regName.test(userName)) { errMsg = "由英文字母和數(shù)字組成的4-16位字符,以字母開頭"; } else { errMsg = "OK!"; } $(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 = "昵稱不能為空"; } else if (!regName.test(nickName)) { errMsg = "由2-6個漢字組成"; } else { errMsg = "OK!"; } $(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 = "郵箱不能為空"; } else if (!regEmail.test(email)) { errMsg = "郵箱賬號@域名。如good@tom.com、whj@sina.com.cn"; } else { errMsg = "OK!"; } $(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 = "密碼不能為空"; } else if (!regPwd.test(pwd)) { errMsg = "格式錯誤"; } else { errMsg = "OK!"; } $(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 = "兩次輸入密碼不一致"; } else { errMsg = "OK!"; } $(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>" } else if (!regPhone.test(phone)) { errMsg = " 格式錯誤 < /span>" } else { errMsg = " 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 = "請輸入生日"; } else if (!regDate.test(birthday)) { errMsg = "格式錯誤"; } else { errMsg = "OK!"; } $(this).parent().append(errMsg); } }); }); });
總結(jié)
以上所述是小編給大家介紹的JS+jQuery實現(xiàn)注冊信息的驗證功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!