struts的输入验证服务器端与客户端

在服务器端,主要重写validate()方法

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validate;
import com.opensymphony.xwork2.ActionSupport;


public class RegistAction1 extends ActionSupport{
    private String userName;
    private String userPassword;
    private int userAge;
    private String userTelephone;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    public int getUserAge() {
        return userAge;
    }
    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }
    public String getUserTelephone() {
        return userTelephone;
    }
    public void setUserTelephone(String userTelephone) {
        this.userTelephone = userTelephone;
    } 
    public void validate(){  
        if(userName==null ||userName.length()<6 || userName.length()>16){
            addFieldError("userName","用户姓名的长度不符合要求,6-16位!"); 
}
        if(userPassword.length()>16||userPassword.length()<6){
            addFieldError("userPassword","密码长度不符合要求,6-16位!");
        }
        if(userAge>130||userAge<1){
            addFieldError("userAge","年龄不符合要求,1-130岁");
        }
        if(userTelephone.length()!=8){
            addFieldError("userTelephone","电话号码不符合要求,8位");
        }  
    }
    public String execute(){
        return SUCCESS;
    }
}

在客户端我们可以使用javascript与ajax等

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>用户注册页面</title>
        <!--检验输入表单数据的函数-->
        <script language="JavaScript">
            function trim(str)
            {
                //使用正则式去掉字符的前后空格
                return str.replace(/^\s*/,"").replace(/\s*$/,""); 
            }
            function check(form)
            {
                //定义错误标志字符串
                var errorStr="";
                //提取表单的4个数据
                var userName=trim(form.userName.value);
                var userPassword=trim(form.userPassword.value);
                var userAge=trim(form.userAge.value);
                var userTelephone=trim(form.userTelephone.value);
                var pattern = /^\d{8}$/;
                //判断用户名是否为空
                if(userName==null||userName=="")
                {
                    errorStr="用户名不能为空!";
                }
                else if(userPassword.length>16||userPassword.length<6)
                {
                    errorStr="密码长度必须在6-16之间";
                }
                else if(userAge>130||userAge<0)
                {
                    errorStr="年龄必须在0-130之间";
                }
                else if(!pattern.test(userTelephone))
                {
                    errorStr="电话号码为8位阿拉伯数字组成!";
                }
                if(errorStr=="")
                {
                    return true;
                 }
                 else
                {
                    alert(errorStr);
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <center>
            请输入注册信息...
            <hr>
            <s:form action="register.action" method="post" onSubmit="return check(this);">
                <table border="1">
                    <tr>
                        <td>
                            <s:textfield name="userName" label="姓名" size="16"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <s:password name="userPassword" label="密码" size="18"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <s:textfield name="userAge" label="年龄" size="16"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <s:textfield name="userTelephone" label="电话" size="16"/> 
                        </td>
                    </tr>
                    <tr>
                        <td><s:submit value="提交"/></td>
                    </tr>
                </table>
            </s:form>
        </center>
    </body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值