表單驗證

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 表單驗證 (http://www.webasp.net/article/9/8280.htm)
-- 作者:未知
-- 發佈日期: 2004-03-15
<script language="javascript">
<!--

function bbs(theForm)
{
if (theForm.user.value == "")
{
alert("請輸入用戶名!");
theForm.user.focus();
return (false);
}
if (checktext(theForm.user.value))
{
alert("請您輸入有效用戶名!");
theForm.user.select();
theForm.user.focus();
return (false);
}


if (theForm.passwd.value == "")
{
alert("請輸入密碼!");
theForm.passwd.focus();
return (false);
}
if (checktext(theForm.passwd.value))
{
alert("請您輸入有效密碼!");
theForm.passwd.select();
theForm.passwd.focus();
return (false);
}




var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var checkStr = theForm.user.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}


if (!allValid)
{
alert("您輸入的用戶名不能含有字符 < / > ' ! =等!");
theForm.user.focus();
return (false);
}
if (checktext(theForm.user.value))
{
alert("您輸入的用戶名不能含有字符 < / > ' ! =等!");
theForm.user.select();
theForm.user.focus();
return (false);
}


var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var checkStr = theForm.passwd.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}


if (!allValid)
{
alert("您輸入的密碼不能含有字符 < / > ' ! =等!");
theForm.passwd.focus();
return (false);
}
if (checktext(theForm.passwd.value))
{
alert("您輸入的密碼不能含有字符 < / > ' ! =等!");
theForm.passwd.select();
theForm.passwd.focus();
return (false);
}

}

function bbs1(theForm)
{if (theForm.keyword.value == "")
{
alert("請輸入關鍵字!");
theForm.keyword.focus();
return (false);
}
if (checktext(theForm.keyword.value))
{
alert("請輸入關鍵字!");
theForm.keyword.select();
theForm.keyword.focus();
return (false);
}
}

function bbs3(theForm)
{

if (theForm.passwd.value == "" || theForm.passwd1.value == "")
{
alert("請輸入密碼!");
theForm.passwd.focus();
return (false);
}

if (theForm.passwd.value.length <4 || theForm.passwd.value.length>7)
{
alert("請輸入密碼必須在4-6位!");
theForm.passwd.focus();
return (false);
}
if(theForm.passwd.value != theForm.passwd1.value)
{
alert("兩次密碼輸入不一致")
theForm.passwd.select();
theForm.passwd.focus();
return (false);
}
if(theForm.psda.value == "")
{alert("輸入密碼提示問題!")
theForm.psda.select();
theForm.psda.focus();
return (false);
}

if(theForm.psdq.value == "")
{alert("輸入密碼提示答案!")
theForm.psdq.select();
theForm.psdq.focus();
return (false);
}
if (theForm.email.value == "")
{
alert("請您輸入您的\"E-Mail\"聯繫方式!");
theForm.email.focus();
return (false);
}

var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
var checkStr = theForm.email.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}

if (theForm.email.value.length < 6)
{
allValid = false;
}

if (!allValid)
{
alert("您輸入的 \"電子郵件地址\" 無效!");
theForm.email.focus();
return (false);
}

address=theForm.email.value;
if(address.length>0)
{
i=address.indexOf("@");
if(i==-1)
{
window.alert("對不起!您輸入的電子郵件地址是錯誤的!")
theForm.email.focus();
return false
}
ii=address.indexOf(".")
if(ii==-1)
{
window.alert("對不起!您輸入的電子郵件地址是錯誤的!")
theForm.email.focus();
return false
}

}
if (checktext(theForm.email.value))
{
alert("請您輸入有效的\"E-Mail\"!");
theForm.email.select();
theForm.email.focus();
return (false);
}

}

function bbs4(theForm)
{
if (theForm.name.value == "")
{
alert("請輸入用戶名!");
theForm.name.focus();
theForm.name.select();
return (false);
}

if (theForm.name.value.length >10)
{
alert("輸入的用戶名必須是10個英文字符或5個漢字!");
theForm.name.focus();
theForm.name.select();
return (false);
}
if (checktext(theForm.name.value))
{
alert("請您輸入有效用戶名!");
theForm.name.select();
theForm.name.focus();
return (false);
}
}
function XPGuestBook(theForm)
{
if (theForm.name.value == "")
{
alert("請告訴我您的名字!");
theForm.name.focus();
return (false);
}
if (checktext(theForm.name.value))
{
alert("請您輸入有效姓名!");
theForm.name.select();
theForm.name.focus();
return (false);
}


if (theForm.email.value == "")
{
alert("請您輸入您的\"E-Mail\"聯繫方式!");
theForm.email.focus();
return (false);
}

var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
var checkStr = theForm.email.value;
var allValid = true;
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}

if (theForm.email.value.length < 6)
{
allValid = false;
}

if (!allValid)
{
alert("您輸入的 \"電子郵件地址\" 無效!");
theForm.email.focus();
return (false);
}

address=theForm.email.value;
if(address.length>0)
{
i=address.indexOf("@");
if(i==-1)
{
window.alert("對不起!您輸入的電子郵件地址是錯誤的!")
theForm.email.focus();
return false
}
ii=address.indexOf(".")
if(ii==-1)
{
window.alert("對不起!您輸入的電子郵件地址是錯誤的!")
theForm.email.focus();
return false
}

}
if (checktext(theForm.email.value))
{
alert("請您輸入有效的\"E-Mail\"!");
theForm.email.select();
theForm.email.focus();
return (false);
}

if (theForm.subject.value == "")
{
alert("請註明本次留言的主題!");
theForm.subject.focus();
return (false);
}



}

function checktext(text)
{
allValid = true;

for (i = 0; i < text.length; i++)
{
if (text.charAt(i) != " ")
{
allValid = false;
break;
}
}

return allValid;
}
//-->
</script>

webasp.net