javascript表單驗(yàn)證大全
被 JavaScript 驗(yàn)證的這些典型的表單數(shù)據(jù)有以下幾種:
1.用戶(hù)是否已填寫(xiě)表單中的必填項(xiàng)目?
2.用戶(hù)輸入的郵件地址是否合法?
3.用戶(hù)是否已輸入合法的日期?
4.用戶(hù)是否在數(shù)據(jù)域 (numeric field) 中輸入了文本?
下面是用戶(hù)名和密碼驗(yàn)證代碼:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>無(wú)標(biāo)題文檔</title> <script> function validateForm() { var username = document.forms["myForm"]["username"].value; var password = document.forms["myForm"]["password"].value; alert(username+" "+password); } </script> </head> <body> <form name="myForm" action="" onSubmit=" return validateForm()" method="post"> 用戶(hù)名:<input type="text" name="username"/><br/> 密碼:<input type="password" name="password"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
必填(或必選)項(xiàng)目驗(yàn)證:
下面的函數(shù)用來(lái)檢查用戶(hù)是否已填寫(xiě)表單中的必填(或必選)項(xiàng)目。假如必填或必選項(xiàng)為空,那么警告框會(huì)彈出,并且函數(shù)的返回值為 false,否則函數(shù)的返回值則為 true(意味著數(shù)據(jù)沒(méi)有問(wèn)題):
function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") {alert(alerttxt);return false} else {return true} } }
下面是連同 HTML 表單的代碼:
<html> <head> <script type="text/javascript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") {alert(alerttxt);return false} else {return true} } } function validate_form(thisform) { with (thisform) { if (validate_required(email,"Email must be filled out!")==false) {email.focus();return false} } } </script> </head> <body> <form action="submitpage.htm" onsubmit="return validate_form(this)" method="post"> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body> </html>
E-mail 驗(yàn)證
下面的函數(shù)檢查輸入的數(shù)據(jù)是否符合電子郵件地址的基本語(yǔ)法。
意思就是說(shuō),輸入的數(shù)據(jù)必須包含 @ 符號(hào)和點(diǎn)號(hào)(.)。同時(shí),@ 不可以是郵件地址的首字符,并且 @ 之后需有至少一個(gè)點(diǎn)號(hào):
function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) {alert(alerttxt);return false} else {return true} } }
下面是連同 HTML 表單的完整代碼:
<html> <head> <script type="text/javascript"> function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) {alert(alerttxt);return false} else {return true} } } function validate_form(thisform) { with (thisform) { if (validate_email(email,"Not a valid e-mail address!")==false) {email.focus();return false} } } </script> </head> <body> <form action="submitpage.htm"onsubmit="return validate_form(this);" method="post"> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body> </html>
以上代碼是用戶(hù)名、密碼、必填、必選項(xiàng)和email以及連同表單的代碼,希望對(duì)大家學(xué)習(xí)javascript表單驗(yàn)證有所幫助。
相關(guān)文章
微信小程序自定義彈窗滾動(dòng)與頁(yè)面滾動(dòng)沖突的解決方法
這篇文章主要為大家詳細(xì)介紹了微信小程序自定義彈窗滾動(dòng)與頁(yè)面滾動(dòng)沖突的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07使用svg實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了如何使用svg實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07JavaScript函數(shù)之call、apply以及bind方法案例詳解
這篇文章主要介紹了JavaScript函數(shù)之call、apply以及bind方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08前端開(kāi)發(fā)不得不知的10個(gè)最佳ES6特性
ES6已經(jīng)不再是JavaScript最新的標(biāo)準(zhǔn),但是它已經(jīng)廣泛用于編程實(shí)踐中。下面通過(guò)本文給大家分享前端開(kāi)發(fā)不得不知的10個(gè)最佳ES6特性,感興趣的朋友參考下吧2017-08-08相冊(cè)展示PhotoSwipe.js插件實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了相冊(cè)展示PhotoSwipe.js插件實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08CSS+JS實(shí)現(xiàn)點(diǎn)擊文字彈出定時(shí)自動(dòng)關(guān)閉DIV層菜單的方法
這篇文章主要介紹了CSS+JS實(shí)現(xiàn)點(diǎn)擊文字彈出定時(shí)自動(dòng)關(guān)閉DIV層菜單的方法,設(shè)計(jì)javascript操作菜單的彈出與關(guān)閉的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05js+html5操作sqlite數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了js+html5操作sqlite數(shù)據(jù)庫(kù)的方法,以完整實(shí)例形式分析了JavaScript封裝的html5操作SQLite數(shù)據(jù)庫(kù)類(lèi),并分析了具體使用技巧,需要的朋友可以參考下2016-02-02