国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

詳解JavaScript表單驗(yàn)證(E-mail 驗(yàn)證)

2024-05-06 16:30:08
字體:
供稿:網(wǎng)友
這篇文章主要為大家詳細(xì)介紹了JavaScript表單驗(yàn)證,重點(diǎn)介紹了E-mail驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
 

本文為大家分享了JavaScript表單驗(yàn)證,被 JavaScript 驗(yàn)證的這些典型的表單數(shù)據(jù)有:
用戶是否已填寫表單中的必填項(xiàng)目?
用戶輸入的郵件地址是否合法?
用戶是否已輸入合法的日期?
用戶是否在數(shù)據(jù)域 (numeric field) 中輸入了文本?
必填(或必選)項(xiàng)目
下面的函數(shù)用來檢查用戶是否已填寫表單中的必填(或必選)項(xiàng)目。假如必填或必選項(xiàng)為空,那么警告框會(huì)彈出,并且函數(shù)的返回值為 false,否則函數(shù)的返回值則為 true(意味著數(shù)據(jù)沒有問題):

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ù)是否符合電子郵件地址的基本語法。
意思就是說,輸入的數(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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)javascript表單驗(yàn)證有所幫助。



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 稻城县| 凌源市| 丰镇市| 延安市| 凤阳县| 宜兰市| 镇康县| 临江市| 博罗县| 天镇县| 滕州市| 上栗县| 阿城市| 荆门市| 沙坪坝区| 民和| 赤壁市| 淅川县| 颍上县| 黎平县| 屏南县| 深水埗区| 陆川县| 贡山| 卫辉市| 西林县| 新龙县| 资阳市| 玉树县| 义乌市| 台安县| 措勤县| 连江县| 广饶县| 丰台区| 青铜峡市| 萝北县| 滁州市| 瑞昌市| 保康县| 合江县|