当然我们可以使用javascript的函数在客户端或者服务器端验证用户输入的合法性。但是在客户端的验证,别人可以轻易的把你的html代码拷贝,并且然后来改变客户端的验证。那么最好的方法就是把认证完全的放到server端。下面是一个例子,包含两端的检测。
<%@ language=javascript %>
<html>
<script language=javascript>
<!--
function checkzip(zipcode){
var myZipReg=/^/d{6}$/
if(myZipReg.test(zipcode)==true){
return isright();
}
else {
return iswrong();
}
}
function isright(){
return true;
}
function iswrong(){
alert("something is wrong about the zipcode");
document.zipcodeForm.zipcodeText.focus();
}
//-->
</script>
<strong>input a zipcode of china</strong>
<form name=zipcodeForm action=fortest6.asp method=post οnsubmit="return checkzip(document.zipcodeForm.zipcodeText.value)">
<input name=zipcodeText type=text>
<br>
<input type=submit value=OK>
</form>
</html>
fortest6.asp
<%@ language=javascript%>
<%
function checkzip(zipcode){
var myZipReg=/^/d{6}$/
if(myZipReg.test(zipcode)==true){
return isright();
}
else {
return iswrong();
}
}
function isright(){
return true;
}
function iswrong(){
return false;
}
var zipcode=new String(Request.Form("zipcodeText"))
if(checkzip(zipcode)==true){
Response.write("<html>/r");
Response.write("the zipcode you provite ");
Response.write("<font color=/"red/">");
Response.write(zipcode+"</font> is right/r");
Response.write("</html>");
}
else {
Response.write("<html>/r");
Response.write("the zipcode you provite ");
Response.write("<font color=/"red/">");
Response.write(zipcode+"</font> is wrong/r");
Response.write("</html>")
}
%>
这个例子,是客户端和服务器端同时验证的例子,当然可以用下面的办法,只用服务器端验证,别人也无法知道你的代码了:))
<%@ language=javascript %>
<html>
<strong>input a zipcode of china</strong>
<form name=zipcodeForm action=fortest6.asp method=post>
<input name=zipcodeText type=text>
<br>
<input type=submit value=OK>
</form>
</html>
嘿嘿。