struts表单取值验证问题

在网上看了挺多的什么表单提交取值验证的问题,在一般的jsp页面内很简单就是document.(表单的名称).(表单内字段的名字).value即可,如:

 

<%@ page language="java" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>您好,欢迎您注册</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 --> 
<script>
function checkreg()
  {
            if (document.form.id.value=="")
   {
    alert("您好,请输入您的账名!");
    form.id.focus();
    return false;
   }
            if (document.form.id.value.length<6 || document.form.id.value.length>15)
   {
    alert("温馨提示:账号长度限制在6至15位!");
    form.id.focus();
    return false;
   }
            if (document.form.name.value=="")
   {
    alert("您好,请输入您的姓名!");
    form.name.focus();
    return false;
   }
             if (document.form.name.value.length<2 || document.form.name.value.length>5)
   {
    alert("温馨提示:姓名长度应该在2至5位!");
    form.name.focus();
    return false;
   }
            if (document.form.pswd.value=="")
   {
    alert("请输入密码!");
    form.pswd.focus();
    return false;
   }
      
            if (document.form.pswd.value.length<6|| document.form.pswd.value.length>15)
   {
    alert("温馨提示:密码长度限制在6至15位!");
    form.pswd.focus();
    return false;
   }
            if ((document.form.pswd.value)!=(document.form.repswd.value))
   {
    alert("温馨提示:二次密码输入不一致!");
    form.repswd.focus();
    return false;
   }
            if (document.form.age.value=="")
   {
    alert("请输入您的年龄!");
    form.age.focus();
    return false;
   }
            if (document.form.type.value=="")
   {
    alert("请选在你的身份!");
    form.type.focus();
    return false;
   }
            if (document.form.address.value=="")
   {
    alert("请输入您的有效地址!");
    form.address.focus();
    return false;
   }
   return true;
  }
</script>

 


</head>
  <body >
    <form action="" name="form" οnsubmit="return checkreg()" method="post">
    设置账号:<input type="text" name="id"   style="width:180px;height:20px"   οnclick="this.value='';" onblur = "this.value = this.value =='' ? : this.value  " οnkeyup="value=value.replace(/[/W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))" οnkeydοwn="if(event.keyCode==13)event.keyCode=9"/><font size="2px"> *只允许输入字母和数字</font>
   <br/><br/>
    您的姓名:<input type="text" name="name"  style="width:180px;height:20px"  οnclick="this.value='';" onblur = "this.value = this.value =='' ?  : this.value  "  οnkeyup="value=value.replace(/[ -~]/g,'')" οnkeydοwn="if(event.keyCode==13)event.keyCode=9"/><font size="2px"> *请您输入中文</font>
   <br/><br/>
    设置密码:<input type="password" name="pswd"  style="width:180px;height:20px" οnclick="this.value='';" onblur = "this.value = this.value =='' ?  : this.value  " /><font size="2px"> *请设置您的安全密码</font>
    <br/><br/>
    密码确认:<input type="password" name="repswd"  style="width:180px;height:20px" οnclick="this.value='';" onblur = "this.value = this.value =='' ?  : this.value  " /><font size="2px"> *请您再次输入密码以确认</font>
   <br/><br/>
   您的性别:<input type="radio" name="sex" value="1" checked/>男<input type="radio" name="sex" value="0"/>女
   <br/><br/>
   您的年龄:<input type="text" name="age" style="width:180px;height:20px"  οnkeyup="value=value.replace(/[^/d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"/><font size="2px"> *请输入您的年龄,只能是数字</font>
   <br/><br/>
   您的身份:<select name="type" style="width:180px;height:20px"/>
       <option>
    --请选择--
    <option value="0">
     学生
    <option value="1">
     老师
    <option value="2">
     演员
    <option value="3">
     公务员
    <option value="4">
     运动员
    <option value="5">
     其他
    </select>
   <br/><br/>
   现居地址:<input type="text" name="address" style="width:180px;height:20px" οnclick="this.value='';" onblur = "this.value = this.value =='' ? : this.value  "  οnkeyup="value=value.replace(/[ -~]/g,'')" οnkeydοwn="if(event.keyCode==13)event.keyCode=9"/><font size="2px"> *请输入您的有效地址,只能是中文</font>
<br/>
<br/>
个人简介:<TEXTAREA class=editbox2 οnkeydοwn=textCounter(this.form.memo,this.form.remLen,160); οnkeyup=textCounter(this.form.memo,this.form.remLen,160); name=memo rows=8 wrap=on cols=45></TEXTAREA>
共可输入160字,还剩<INPUT class=editbox1 readOnly maxLength=3 size=3 value=160 name=remLen>字。 <br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

 

然而在struts中,我发现表单不支持name访问,在网上找了半天也没一个真正的好例子,后来我查文档发现在Struts中,表单支持styleId的形式提取各个输入框内的值,于是我就整理了一下,具体例子如下:

 

 

<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html:html>
  <head>
    <title>吴秋华脚本验证整理</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">


 <!--本页面纯属研究struts中引用脚本-->
 <script type="text/javascript">
 function checkRegister(){
 if(document.getElementById("name").value==""){
  alert("吴秋华温馨提示:姓名不能为空!");
  document.getElementById("name").focus();
  return false;
 }
 if(document.getElementById("pswd").value==""){
  alert("吴秋华温馨提示:密码不能为空!");
  document.getElementById("pswd").focus();
  return false;
 }
 if(document.getElementById("age").value==""){
  alert("吴秋华温馨提示:年龄不能为空!");
  document.getElementById("age").focus();
  return false;
 }
 if(document.getElementById("sf").value=="0"){
  alert("吴秋华温馨提示:请选择你的身份!");
  document.getElementById("sf").focus();
  return false;
 }
 if(document.getElementById("favor").value==""){
  alert("吴秋华温馨提示:请选择爱好!");
  document.getElementById("favor").focus();
  return false;
 }
 if(document.getElementById("motto").value==""){
  alert("吴秋华温馨提示:请填写你的人生格言 !");
  document.getElementById("motto").focus();
  return false;
 }
 if(document.getElementById("jianjie").value==""){
  alert("吴秋华温馨提示:请填写个人简介!");
  document.getElementById("jianjie").focus();
  return false;
 }
 return true;
 }
 </script>
  </head>
  <body>
    <html:form action="/register.do" method="post" οnsubmit="return checkRegister()">
     姓名:<html:text property="name" styleId="name" style="width:150px;height:22px"></html:text><br><br>
     密码:<html:password property="pswd" styleId="pswd" style="width:150px;height:22px"></html:password><br><br>
     性别:<html:radio property="sex" styleId="sex" value="1">男</html:radio>
       <html:radio property="sex" styleId="sex" value="0">女</html:radio><br><br>
     年龄:<html:text property="age" styleId="age" style="width:150px;height:22px"></html:text><br><br>
     身份:<html:select property="sf" styleId="sf" style="width:150px;height:22px">
       <html:option value="0">--请选择--</html:option>
       <html:option value="学生">学生</html:option>
       <html:option value="老师">老师</html:option>
       <html:option value="工人">工人</html:option>
       <html:option value="艺人">艺人</html:option>
      </html:select><br><br>
     爱好:<html:checkbox property="favor" styleId="favor" value="上网">上网</html:checkbox>
     <html:checkbox property="favor" styleId="favor" value="看书">看书</html:checkbox>
     <html:checkbox property="favor" styleId="favor" value="编程">编程</html:checkbox><br>
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<html:checkbox property="favor" styleId="favor" value="交友">交友</html:checkbox>
     <html:checkbox property="favor" styleId="favor" value="爬山">爬山</html:checkbox>
     <html:checkbox property="favor" styleId="favor" value="其他">其他</html:checkbox><br><br>
     格言:<html:text property="motto" styleId="motto" style="width:150px;height:22px"></html:text><br><br> 
     简介:<html:textarea property="jianjie" styleId="jianjie" style="width:240px;height:100px"></html:textarea><br><br>
     <html:submit value="提交"></html:submit>
    </html:form>
</body>
</html:html>

温馨提示:当你保存后要打开这个页面看效果的时候,你的Action(即源代码我加黄色背景处)在struts配置中要有,不然会提示有错!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰河洗剑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值