ASP学习笔记(5)--客户端和服务器端同时验证输入合法性

来源:互联网 发布:知乎 唯一预测特朗普 编辑:程序博客网 时间:2024/05/01 03:33

当然我们可以使用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 onsubmit="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>

嘿嘿。
 

Google  
原创粉丝点击