2

我正在开发 Struts 2 应用程序,试图为 10 位数的手机号码提供验证。

我只允许 10 位数字,但即使我输入 10 位数字,它仍然显示验证错误消息,并且还想知道有什么方法可以购买我们可以对日期时间选择器 DOJO 标签执行验证。

Empaction-validation.xml

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
   <field name ="firstname">
     <field-validator type="requiredstring">
      <message key="requiredstring"/>
    </field-validator>
  </field>


       <field name ="email">
        <field-validator type = "email">
        <message>provide valid Email</message>

        </field-validator>

       </field> 

   <field name ="mobileno">

    <field-validator type="long">
            <param name="min">10</param>
            <param name="max">10</param>

            <message key="stringlength" />
    </field-validator>

</field>


</validators>

注册表单是

<%@ page language ="java" contentType ="text/html; charset=ISO-8859-1" pageEncoding ="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx" %> 
 <html>
    <head>
       <sx:head/>
    <script type="text/javascript"  src ="script.js"></script>

    </head>  

      <body>
        <div align="center"> <h1 style="color: red">  ENPLOYEE REGISTRATION FORM</h1>
       <s:form  action="emplogin"  method="post" >

      <s:textfield name="firstname" label="Employee Firstname"/>
      <s:textfield name ="lastname" label ="Last name"/>  
      <s:textfield name ="id"  label="Id"/>
      <s:radio name ="gender"   list="{'male', 'female'}" label = "Gender"/>
    <sx:datetimepicker name="dob" displayFormat="dd-MMM-yyyy"  label="DOB"></sx:datetimepicker> 
      <s:radio  name ="maritalstatus" list="{'singale','married'}" label="Marital Status" />

      <s:textfield name ="email" label ="Email"/>
 <sx:datetimepicker name ="joiningdate" displayFormat="dd-MMM-yyyy" label="Joining Date"></sx:datetimepicker>

      <s:textfield name= "designation" label = "Designation"/>
      <s:textarea name ="address" label ="Address" />
      <s:textfield name = "country" label ="Country" />     
      <s:textfield name  ="state" label = "State" />
      <s:textfield name  ="city" label ="City"/> 
      <s:textfield name ="pincode" label ="Pincode"/>
      <s:textfield name ="mobileno" label="Mobile No"/>
      <s:select   name ="groups" list="{'group 1', 'group 2', 'group 3'}"  label ="Group"  cssStyle="{width:184px"/>
     <tr><td>&nbsp;</td></tr>
        <tr>
    <td>&nbsp;</td>
      <s:submit align="center"></s:submit>
      </s:form>
       </div>
    </body>
 </html>

在我的 bean 类中它是long类型

package model;


public class Empmodel {
private String firstname;
private String lastname;
private String id;
private String gender;
private String dob;
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}
private String maritalstatus;
private String email;
private String joiningdate;
private String designation;
private String address;
private String country;
private String state;
private String city;
private int  pincode;
private long mobileno;
private String groups;
public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}



public String getMaritalstatus() {
    return maritalstatus;
}
public void setMaritalstatus(String maritalstatus) {
    this.maritalstatus = maritalstatus;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getJoiningdate() {
    return joiningdate;
}
public void setJoiningdate(String joiningdate) {
    this.joiningdate = joiningdate;
}
public String getDesignation() {
    return designation;
}
public void setDesignation(String designation) {
    this.designation = designation;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}


public long getMobileno() {
    return mobileno;
}
public void setMobileno(long mobileno) {
    this.mobileno = mobileno;
}
public String getGroups() {
    return groups;
}
public void setGroups(String groups) {
    this.groups = groups;
}
public int getPincode() {
    return pincode;
}
public void setPincode(int pincode) {
    this.pincode = pincode;
}

}
4

1 回答 1

0

使用stringlenth验证器可以验证字符串的长度。检查字符串长度验证器

并且使用date验证器可以验证日期格式。检查日期验证器

如果您想使用验证日期时间选择器,validate()您应该使用解析选择器返回的值的逻辑,SimpleDateFormat如果您不使用其他日期时间库(如JODA)并且不关心时区,那么您应该ThreadLocal为日期时间创建解析器。

编辑:

验证器.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">

<validators>
  <validator name="longlength" class="jspbean.struts.vaidators.LongLengthFieldValidator"/>
</validators>

LongLengthFieldValidator.java

public class LongLengthFieldValidator extends FieldValidatorSupport {

  private boolean doTrim = true;
  private int maxLength = -1;
  private int minLength = -1;


  public void setMaxLength(int maxLength) {
    this.maxLength = maxLength;
  }

  public int getMaxLength() {
    return maxLength;
  }

  public void setMinLength(int minLength) {
    this.minLength = minLength;
  }

  public int getMinLength() {
    return minLength;
  }

  public void setTrim(boolean trim) {
    doTrim = trim;
  }

  public boolean getTrim() {
    return doTrim;
  }

  public void validate(Object object) throws ValidationException {
    String fieldName = getFieldName();
    String val = getFieldValue(fieldName, object).toString();

    if (val == null || val.length() <= 0) {
      // use a required validator for these
      return;
    }
    if (doTrim) {
      val = val.trim();
      if (val.length() <= 0) {
        // use a required validator
        return;
      }
    }

    if ((minLength > -1) && (val.length() < minLength)) {
      addFieldError(fieldName, object);
    } else if ((maxLength > -1) && (val.length() > maxLength)) {
      addFieldError(fieldName, object);
    }
  }
}

XXX-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>

  <validator type="longlength">
                <param name="fieldName">mobileno</param>
                <param name="minLength">10</param>
               <param name="maxLength">10</param>
               <param name="trim">true</param>
               <message>Your mobileno number needs to be 10 characters long</message>
  </validator>
</validators>
于 2013-02-27T10:49:50.037 回答