MobileNo验证错误和日期时间验证技术

时间:2013-02-27 07:18:48

标签: java javascript jsp struts2 struts-validation

我正在研究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;
}

}

1 个答案:

答案 0 :(得分:0)

使用stringlenth验证程序可以验证字符串的长度。查看stringlength validator

使用date验证程序,您可以验证日期格式。查看date validator

如果要使用validate()验证日期时间选择器,如果不使用{{3}等其他日期时间库,则应使用SimpleDateFormat解析选择器返回的值的逻辑。然后你应该为日期时间创建ThreadLocal解析器。

。并且不关心时区

修改

<强> validators.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>