javascript表單驗(yàn)證大全
被 JavaScript 驗(yàn)證的這些典型的表單數(shù)據(jù)有以下幾種:
1.用戶是否已填寫表單中的必填項(xiàng)目?
2.用戶輸入的郵件地址是否合法?
3.用戶是否已輸入合法的日期?
4.用戶是否在數(shù)據(jù)域 (numeric field) 中輸入了文本?
下面是用戶名和密碼驗(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">
用戶名:<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)檢查用戶是否已填寫表單中的必填(或必選)項(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>
以上代碼是用戶名、密碼、必填、必選項(xiàng)和email以及連同表單的代碼,希望對(duì)大家學(xué)習(xí)javascript表單驗(yàn)證有所幫助。
相關(guān)文章
解決webpack dev-server不能匹配post請(qǐng)求的問(wèn)題
這篇文章主要介紹了解決webpack不能匹配post請(qǐng)求的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
JavaScript日期時(shí)間與時(shí)間戳的轉(zhuǎn)換函數(shù)分享
這篇文章主要介紹了JavaScript日期時(shí)間與時(shí)間戳的轉(zhuǎn)換函數(shù)分享,本文給出兩個(gè)函數(shù)實(shí)現(xiàn)日期時(shí)間和時(shí)間戳間的轉(zhuǎn)換,需要的朋友可以參考下2015-01-01
JS實(shí)現(xiàn)玩轉(zhuǎn)風(fēng)車
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)玩轉(zhuǎn)風(fēng)車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
JavaScript中window.showModalDialog()用法詳解
這篇文章主要介紹了JavaScript中window.showModalDialog()用法詳解,需要的朋友可以參考下2014-12-12
淺析JavaScript的幾種Math函數(shù),random(),ceil(),round(),floor()
本文主要對(duì)JavaScript的幾種Math函數(shù),random(),ceil(),round(),floor()的作用進(jìn)行簡(jiǎn)要解析,具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
Underscore之Array_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Underscore之Array的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
基于JS實(shí)現(xiàn)翻書效果的頁(yè)面切換樣式
在項(xiàng)目開發(fā)中經(jīng)常遇到翻書的頁(yè)面切換效果,基于js代碼怎么實(shí)現(xiàn)的呢?今天小編給大家分享基于JS實(shí)現(xiàn)翻書效果的頁(yè)面切換樣式,需要的朋友參考下吧2017-02-02

