Results 1 to 3 of 3
  1. #1
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

    Question [SOLVED] Struts 2 Integer Validation not working

    I am trying to add Integer validation to the my Struts 2 application.
    Here is what I have done

    <validators>
    <validator type="int">
    <param name="fieldName">zipCode</param>
    <message>Zip Code needs to be a valid number </message>
    </validator>
    </validators>

    I have already set validate="true" in the <s:form> tag
    This is successfully generating the java script code at my JSP. But when I see the generated code. The validation code for my field is


    <script type="text/javascript">
    function validateForm_generateAction() {
    form = document.getElementById("generateAction");
    clearErrorMessages(form);
    clearErrorLabels(form);
    // field name: zipCode
    // validator name: int
    if (form.elements['zipCode']) {
    field = form.elements['zipCode'];
    var error = "Zip code should be a valid number";
    if (field.value != null) {
    if (false || false) {
    addError(field, error);
    errors = true;
    }
    }
    }

    return !errors;
    }
    </script>
    Not sure why this code is coming

    quote:if (false || false) {


    Due to this it will not do validation for this field.

    Any clue? Am I doing something wrong here? Please help.
    Live life king size
    [Lucene]

  2. #2
    kzvi.kzvi.1 is offline Member
    Join Date
    Oct 2008
    Location
    US
    Posts
    58
    Rep Power
    0

    Default

    looks like the Struts 2 "int" type validator does the range check. It uses IntRangeFieldValidator which is checking for integer range only.

    I think its still beta so they may be fixing some issues. It should be working as you are expecting but if this does not work then have a workaround by using regular expression for number validation in your case it should work fine as you have zip code which need not be checked for the range.

    Here is a Example Code which demonstrate how you can use regular expression validation for number validation in Struts 2.

    ____________________________________________
    Software Wiki | Interview FAQs | Lucene Search | Oracle | ORM | Struts2 | Job Seeker
    Have fun....
    JAVA FAQs

  3. #3
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

    Default

    I dont know what was wrong with my code, but now i used a different aproach suggested by kzvi.kzvi.1 and it worked.
    so just closing the thread.
    Live life king size
    [Lucene]

Similar Threads

  1. Struts validation
    By Jack in forum Web Frameworks
    Replies: 3
    Last Post: 10-06-2008, 01:54 AM
  2. Struts custom Validation
    By ganesan in forum Web Frameworks
    Replies: 0
    Last Post: 07-17-2008, 02:15 PM
  3. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  4. log4j not working in struts
    By Manu in forum Web Frameworks
    Replies: 0
    Last Post: 03-19-2008, 03:54 PM
  5. java validation?
    By lockmac in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 05:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •