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

这篇博客探讨了在ASP中如何实现客户端和服务器端同时验证用户输入的合法性,以确保安全性。通过示例代码展示了一个JavaScript函数用于客户端验证邮政编码格式,而服务器端同样进行验证。强调了仅依赖客户端验证的不足,并提供了仅使用服务器端验证的替代方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

嘿嘿。
 

Google
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值