js實(shí)現(xiàn)表單校驗(yàn)功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)表單校驗(yàn)功能的具體代碼,供大家參考,具體內(nèi)容如下
1、所用到的三個(gè)事件:
onfocus(焦點(diǎn)聚焦事件)、onblur(焦點(diǎn)離開(kāi)事件)、onkeyup(按鍵抬起的事件)
2、利用事件觸發(fā)函數(shù),函數(shù)中執(zhí)行校驗(yàn)的信息。
3、利用checkform判斷表單中的內(nèi)容是否規(guī)范,如果規(guī)范submit按鈕可以提交表單信息。
簡(jiǎn)單效果:
form中的代碼:
<form action="demo.html" onsubmit="return checkForm()"> <div> <div class="text"> <p>用戶名</p> <input id="value" onfocus="shoeTips('hint','用戶名長(zhǎng)度不能小于六')" onblur="hint_hide()" onkeyup="hint()" type="text" Name="Userame" placeholder="用戶名" /> <span id="hint"></span> </div> <div class="text"> <p>密碼</p> <input id="pass_value" onfocus="shoeTips('pass_hint','密碼長(zhǎng)度不能小于六')" onblur="pass_hide()" onkeyup="checkPass()" type="password" name="password" placeholder="密碼" /> <span id="pass_hint"></span> </div> <div class="text"> <p>確認(rèn)密碼</p> <input id="passpass_value" onfocus="shoeTips('passpass_hint','兩次密碼要一致')" onblur="passpass_hide()" onkeyup="checkPassPass()" type="password" name="password" placeholder="確認(rèn)密碼" /> <span id="passpass_hint"></span> </div> <div class="text"> <p>郵箱</p> <input id="email" onfocus="shoeTips('email_hint','郵箱格式要正確')" onblur="emailHide()" onkeyup="emailCheck()" type="email" name="email" placeholder="郵箱" /> <span id="email_hint"></span> </div> <div class="text"> <p>手機(jī)號(hào)</p> <input id="phone" type="text" onfocus="shoeTips('phone_hint','格式為十一位數(shù)字的手機(jī)號(hào)')" onblur="phoneHide()" onkeyup="phoneCheck()" Name="Phone" placeholder="手機(jī)號(hào)"> <span id="phone_hint"></span> </div> <div class="submit"> <input type="submit" value="提交" /> </div> </div> </form>
js中的:
function shoeTips(spanId, tips) { var span = document.getElementById(spanId); span.innerHTML = tips; } /** * 校驗(yàn)用戶名 */ function hint() { var value = document.getElementById("value").value; var hint = document.getElementById("hint"); if(value.length < 6) { hint.innerHTML = "用戶名太短"; return false; } else { hint.innerHTML = "用戶名合格"; return true; } } function hint_hide() { var hint = document.getElementById("hint"); hint.innerHTML = ""; } /** * 校驗(yàn)密碼 */ function checkPass() { var value = document.getElementById("pass_value").value; var hint = document.getElementById("pass_hint"); if(value.length < 6) { hint.innerHTML = "密碼太短"; return false; } else { hint.innerHTML = "密碼格式合格"; return true; } } function pass_hide() { var hint = document.getElementById("pass_hint"); hint.innerHTML = ""; } /*** * 確認(rèn)密碼的校驗(yàn) */ function checkPassPass() { var papavalue = document.getElementById("passpass_value").value; var value = document.getElementById("pass_value").value; var papahint = document.getElementById("passpass_hint"); if(papavalue != value) { papahint.innerHTML = "兩次密碼不一致"; return false; } else { papahint.innerHTML = ""; return true; } } function passpass_hide() { var papahint = document.getElementById("passpass_hint"); papahint.innerHTML = ""; } /** * 校驗(yàn)郵箱 */ function checkEmail(strEmail) { var emailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; if ( emailReg.test(strEmail) ) { return true; } else { // alert("您輸入的Email地址格式不正確!"); return false; } }; function emailCheck() { var emailValue = document.getElementById("email").value; var email_hint = document.getElementById("email_hint"); var flag = checkEmail(emailValue); if(flag) { email_hint.innerHTML = "郵箱格式正確"; return true; } else { email_hint.innerHTML = "郵箱格式錯(cuò)誤"; return false; } } function emailHide() { var email_hint = document.getElementById("email_hint"); email_hint.innerHTML = ""; } /** * 校驗(yàn)手機(jī)號(hào) */ function checkMobile( strMobile ) { //13588888888 var regu = /^[1][345678][0-9]{9}$/; var re = new RegExp(regu); if (re.test(strMobile)) { return true; } else { return false; } }; function phoneCheck() { var phone = document.getElementById("phone").value; var phone_hint = document.getElementById("phone_hint"); var flag = checkMobile(phone); if(flag) { phone_hint.innerHTML = "手機(jī)號(hào)格式正確"; return true; } else { phone_hint.innerHTML = "手機(jī)號(hào)格式錯(cuò)誤"; return false; } } function phoneHide() { var phone_hint = document.getElementById("phone_hint"); phone_hint.innerHTML = ""; } function checkForm() { var flag = emailCheck() && checkPass() && checkPassPass() && hint() && phoneCheck(); return flag; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js前端設(shè)計(jì)模式優(yōu)化50%表單校驗(yàn)代碼示例
- 從表單校驗(yàn)看JavaScript策略模式的使用詳解
- Vue.js + Nuxt.js 項(xiàng)目中使用 Vee-validate 表單校驗(yàn)
- JavaScript 完成注冊(cè)頁(yè)面表單校驗(yàn)的實(shí)例
- 使用JavaScript進(jìn)行表單校驗(yàn)功能
- Angularjs使用指令做表單校驗(yàn)的方法
- AngularJs表單校驗(yàn)功能實(shí)例代碼
- AngularJS入門教程之表單校驗(yàn)用法示例
- Vue.js 表單校驗(yàn)插件
- JS實(shí)現(xiàn)注冊(cè)界面表單校驗(yàn)
相關(guān)文章
js實(shí)現(xiàn)網(wǎng)頁(yè)圖片延時(shí)加載 提升網(wǎng)頁(yè)打開(kāi)速度
這篇文章主要為大家介紹了js實(shí)現(xiàn)網(wǎng)頁(yè)圖片延時(shí)加載,提升網(wǎng)頁(yè)打開(kāi)速度,感興趣的小伙伴們可以參考一下2016-01-01js仿蘋(píng)果iwatch外觀的計(jì)時(shí)器代碼分享
這篇文章主要介紹了JS+CSS3實(shí)現(xiàn)的類似于蘋(píng)果iwatch計(jì)時(shí)器特效,很實(shí)用的代碼,推薦給大家,有需要的小伙伴可以參考下。2015-08-08使用Object.defineProperty實(shí)現(xiàn)簡(jiǎn)單的js雙向綁定
這篇文章主要介紹了使用Object.defineProperty實(shí)現(xiàn)簡(jiǎn)單的js雙向綁定的相關(guān)資料,需要的朋友可以參考下2016-04-04通過(guò)seajs實(shí)現(xiàn)JavaScript的模塊開(kāi)發(fā)及按模塊加載
seajs實(shí)現(xiàn)了JavaScript 的 模塊開(kāi)發(fā)及按模塊加載。用來(lái)解決繁瑣的js命名沖突,文件依賴等問(wèn)題,其主要目的是令JavaScript開(kāi)發(fā)模塊化并可以輕松愉悅進(jìn)行加載。下面我們來(lái)一起深入學(xué)習(xí)下吧2019-06-06微信小程序如何修改radio和checkbox的默認(rèn)樣式和圖標(biāo)
這篇文章主要介紹了微信小程序修改radio和checkbox的默認(rèn)樣式和圖標(biāo),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07js實(shí)現(xiàn)九宮格圖片半透明漸顯特效的方法
這篇文章主要介紹了js實(shí)現(xiàn)九宮格圖片半透明漸顯特效的方法,涉及js操作css特效的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02