现在的位置: 首页 > 综合 > 正文

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

2014年07月16日 ⁄ 综合 ⁄ 共 3334字 ⁄ 字号 评论关闭

在服务器端,主要重写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>

抱歉!评论已关闭.