JS實現(xiàn)的簡單表單驗證功能完整實例
本文實例講述了JS實現(xiàn)的簡單表單驗證功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>www.dhdzp.com 表單驗證</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
color: #000000;
}
body {
background-color: #CCCCCC;
margin-left: 0px;
margin-top: 0px;
}
a {
font-size: 12px;
color: #666600;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #000099;
}
a:hover {
text-decoration: underline;
color: #6633FF;
}
a:active {
text-decoration: none;
color: #00FF00;
}
-->
</style>
<script type="text/javascript">
function checkusername()
{
var myform = document.getElementById("form1");
var username = myform.username.value.length;
if(username < 1||username>12)
{
errusername.innerHTML = "<font color='red'>用戶名不符合要求</font>";
return false;
}else{
errusername.innerHTML = "<font color='green'>用戶名符合要求</font>";
return true;
}
}
function checkage()
{
var myform = document.getElementById("form1");
var age = myform.age.value;
if(age != parseInt(age))
{
errage.innerHTML = "<font color='red'>年齡不符合要求</font>";
return false;
}else{
errage.innerHTML = "<font color='green'>年齡符合要求</font>";
return true;
}
}
function checkemail()
{
var myform = document.getElementById("form1");
var mail=/^[A-Za-z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
if(!mail.test(myform.email.value))
{
erremail.innerHTML = "<font color='red'>email不符合要求</font>";
return false;
}else{
erremail.innerHTML = "<font color='green'>email符合要求</font>";
return true;
}
}
function checkform()
{
checkusername();checkage();checkemail();
if(checkusername()&&checkage()&&checkemail())
{
return true;
}else{
return false;
}
}
</script>
</head>
<body alink="center">
<form id="form1" name="form1" method="post" action="ttt.jsp" onsubmit="return checkform()">
<table width="777" border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="3" ><div align="center">請輸入你的注冊信息</div></td>
</tr>
<tr>
<td width="250" ><div align="right" >請輸入你的用戶名:</div></td>
<td width="250"><div align="center">
<input type="text" name="username" onblur="checkusername()" />
</div></td>
<td><div id="errusername" align="center"></div></td>
</tr>
<tr>
<td width="250"><div align="right">請輸入你的年齡:</div></td>
<td width="250"><div align="center" >
<label>
<input type="text" name="age" onblur="checkage()"/>
</label>
</div></td>
<td><div align="center" id="errage"></div></td>
</tr>
<tr>
<td width="250"><div align="right" >請輸入你的EMAIL:</div></td>
<td width="250"><div align="center" >
<label>
<input type="text" name="email" onblur="checkemail()" />
</label>
</div></td>
<td><div align="center" id="erremail"></div></td>
</tr>
<tr>
<td><div align="right">
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</div></td>
<td><div align="center">
<label>
<input type="reset" name="Submit2" value="重置" />
</label>
</div></td>
<td><div align="center"></div></td>
</tr>
</table>
</form>
</body>
</html>
運行效果:

PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript正則表達式技巧大全》、《JavaScript表單(form)操作技巧大全》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。

