表单验证
简述
表单验证是在用户提交表单前,对输入内容做检查,判断是否符合规则;不合格就提示、阻止提交,避免脏数据传给后台。
表单验证的思路
问题
如何编写脚本验证表单?
分析
-
获取表单元素的值(String类型),然后进行判断
-
触发表单(FORM)的提交事件(onSubmit)
电子邮件验证
-
效果
-
源代码
| <SCRIPT LANGUAGE="JavaScript">
function checkEmail() {
var strEmail = document.myform.txtEmail.value;
if (strEmail.length == 0) {
alert("电子邮件不能为空!");
return false;
}
if (strEmail.indexOf("@", 0) == -1) {
alert("电子邮件格式不正确\n必须包含@符号!");
return false;
}
if (strEmail.indexOf(".", 0) == -1) {
alert("电子邮件格式不正确\n必须包含.符号!");
return false;
}
return true;
}
</SCRIPT>
<FORM name="myform" method="post" action="reg_success.htm" onSubmit="return checkEmail( )">
<INPUT name="txtEmail" type="text" id="txtEmail">
</FORM>
|
源代码
| <HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>使用字符串</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function checkEmail() {
var strEmail = document.myform.txtEmail.value;
if (strEmail.length == 0) {
alert("电子邮件不能为空!");
return false;
}
if (strEmail.indexOf("@", 0) == -1) {
alert("电子邮件格式不正确\n必须包含@符号!");
return false;
}
if (strEmail.indexOf(".", 0) == -1) {
alert("电子邮件格式不正确\n必须包含.符号!");
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<FORM name="myform" method="post" action="reg_success.htm" onSubmit="return checkEmail( )">
<P><IMG src="images/reg_back1.jpg" width="979" height="195"></P>
<P> </P>
<TABLE border="0" align="center">
<TR>
<TD>您的电子邮件 </TD>
<TD colspan="2"><INPUT name="txtEmail" type="text" id="txtEmail">
*必填</TD>
</TR>
<TR>
<TD colspan="3" align="center">
<P>
</P>
<P>
<INPUT name="clearButton" type="reset" id="clearButton" value=" 清 空 ">
<INPUT name="registerButton" type="submit" id="registerButton" value=" 注 册 ">
</P>
</TD>
</TR>
</TABLE>
<P> </P>
<P> </P>
<P> </P>
<P><IMG src="images/bottom.jpg" width="969" height="107"></P>
<P> </P>
</FORM>
</HTML>
|
用户名密码验证
-
效果
-
源代码
| <SCRIPT language="JavaScript">
//validate Name
function checkUserName() {
var fname = document.myform.txtUser.value;
if (fname.length != 0) {
for (i = 0; i < fname.length; i++) {
var ftext = fname.substring(i, i + 1);
if (ftext < 9 || ftext > 0) {
alert("名字中包含数字 \n" + "请删除名字中的数字和特殊字符");
return false
}
}
}
else {
alert("未输入用户名 \n" + "请输入用户名");
return false
}
return true;
}
function passCheck() {
var userpass = document.myform.txtPassword.value;
if (userpass == "") {
alert("未输入密码 \n" + "请输入密码");
return false;
}
// Check if password length is less than 6 charactor.
if (userpass.length < 6) {
alert("密码必须多于或等于 6 个字符。\n");
return false;
}
return true;
}
function validateform() {
if (checkUserName() && passCheck())
return true;
else
return false;
}
</SCRIPT>
|
源代码
| user_password.html |
|---|
| <HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>使用字符串</TITLE>
<SCRIPT language="JavaScript">
//validate Name
function checkUserName() {
var fname = document.myform.txtUser.value;
if (fname.length != 0) {
for (i = 0; i < fname.length; i++) {
var ftext = fname.substring(i, i + 1);
if (ftext < 9 || ftext > 0) {
alert("名字中包含数字 \n" + "请删除名字中的数字和特殊字符");
return false
}
}
}
else {
alert("未输入用户名 \n" + "请输入用户名");
return false
}
return true;
}
function passCheck() {
var userpass = document.myform.txtPassword.value;
if (userpass == "") {
alert("未输入密码 \n" + "请输入密码");
return false;
}
// Check if password length is less than 6 charactor.
if (userpass.length < 6) {
alert("密码必须多于或等于 6 个字符。\n");
return false;
}
return true;
}
function validateform() {
if (checkUserName() && passCheck())
return true;
else
return false;
}
</SCRIPT>
</HEAD>
<body>
<FORM name="myform" method="post" action="reg_success.htm" onSubmit="return validateform()">
<P><IMG src="images/reg_back1.jpg" width="979" height="195"></P>
<P> </P>
<TABLE border="0" align="center">
<TR>
<TD>用户名: </TD>
<TD colspan="2"><INPUT name="txtUser" type="text" id="txtUser">
*必填</TD>
</TR>
<TR>
<TD>密 码:</TD>
<TD colspan="2"><INPUT name="txtPassword" type="password" id="txtPassword">
*必填</TD>
</TR>
<TR>
<TD colspan="3" align="center">
<P>
</P>
<P>
<INPUT name="clearButton" type="reset" id="clearButton" value=" 清 空 ">
<INPUT name="registerButton" type="submit" id="registerButton" value=" 登 录 ">
</P>
</TD>
</TR>
</TABLE>
<P> </P>
<P> </P>
<P> </P>
<P><IMG src="images/bottom.jpg" width="969" height="107"></P>
<P> </P>
</FORM>
</body>
</HTML>
|
文本框
-
效果
-
源代码
| <SCRIPT LANGUAGE="JavaScript">
function clearText() {
if (document.myform.txtEmail.value == "请输入真实的电子邮箱,我们将发送激活密码") {
document.myform.txtEmail.value = "";
document.myform.txtEmail.style.color = "red";
}
}
</SCRIPT>
<INPUT name="txtEmail" type="text" class="textBorder" id="txtEmail"
value="请输入真实的电子邮箱,我们将发送激活密码" size="40"
onFocus="clearText( )" style="color: #666666;">
|
源代码
| email2.html |
|---|
| <HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>使用字符串</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function checkEmail() {
var strEmail = document.myform.txtEmail.value;
if (strEmail.length == 0) {
alert("电子邮件不能为空!");
return false;
}
if (strEmail.indexOf("@", 0) == -1) {
alert("电子邮件格式不正确\n必须包含@符号!");
document.myform.txtEmail.focus();
document.myform.txtEmail.select();
return false;
}
if (strEmail.indexOf(".", 0) == -1) {
alert("电子邮件格式不正确\n必须包含.符号!");
document.myform.txtEmail.focus();
document.myform.txtEmail.select();
return false;
}
return true;
}
function clearText() {
if (document.myform.txtEmail.value == "请输入真实的电子邮箱,我们将发送激活密码") {
document.myform.txtEmail.value = "";
document.myform.txtEmail.style.color = "red";
}
}
</SCRIPT>
<STYLE type="text/css">
<!--
.textBorder {
border: 1px solid;
font-size: 15px;
}
-->
</STYLE>
</HEAD>
<body>
<FORM name="myform" method="post" action="reg_success.htm" onSubmit="return checkEmail( )">
<P><IMG src="images/reg_back1.jpg" width="979" height="195"></P>
<P> </P>
<TABLE width="559" border="0" align="center">
<TR>
<TD>登录名:</TD>
<TD colspan="2"><INPUT name="txtUserName" type="text" class="textBorder" id="txtUserName" size="40"></TD>
</TR>
<TR>
<TD>您的电子邮件: </TD>
<TD colspan="2"><INPUT name="txtEmail" type="text" class="textBorder" id="txtEmail" value="请输入真实的电子邮箱,我们将发送激活密码"
size="40" onFocus="clearText( )" style="color: #666666;">
*必填</TD>
</TR>
<TR>
<TD colspan="3" align="center">
<P>
</P>
<P>
<INPUT name="clearButton" type="reset" id="clearButton" value=" 清 空 ">
<INPUT name="registerButton" type="submit" id="registerButton" value=" 注 册 ">
</P>
</TD>
</TR>
</TABLE>
<P> </P>
<P> </P>
<P> </P>
<P><IMG src="images/bottom.jpg" width="969" height="107"></P>
<P> </P>
</FORM>
</body>
</HTML>
|