user2854201
user2854201

Reputation:

How to validate all the form fields in Struts 2

I have created one login page with the following fields,now how can i validate these fields before submitting it for authentication? Please guide me,I am new to struts2. Is struts2 provides any otherway of validating form fields?

Which one is the best approach? can i able to use javascript to validate struts2 form fields?

please guide me.Thanks in advance

My action class name is LoginAction.java

and my login.jsp is

<s:form action="">
            <s:textfield label="Username" key="username"></s:textfield><br/>
            <s:password label="Password" key="password"></s:password><br/>
            <s:textarea label="Address" key="address"></s:textarea>
            <s:radio list="{'Male','Female'}" label="Gender" key="gender"></s:radio>
            <s:file key="file"></s:file>
            <s:select list="{'Select a value','India','Australia','England','Srilanka','Japan','Other'}" label="Country"    key="country"></s:select>    
            <s:submit label="Add Information"></s:submit>
            </s:form>  

I have created one login page with the following fields,now how can i validate these fields before submitting it for authentication? Please guide me,I am new to struts2. Is struts2 provides any otherway of validating form fields?

which one is the best approach? can i able to use javascript to validate struts2 form fields?

please guide me.Thanks in advance

My action class name is LoginAction.java     

Upvotes: 0

Views: 4225

Answers (1)

Pratap A.K
Pratap A.K

Reputation: 4517

You can use validate method of ActionSupport class,in that case your Action class should extends ActionSupport class,another approach is validating inside XML file add below method inside your Action class

    public void validate()
        {
            if(StringUtils.isEmpty(getUsername()))
            {
                addFieldError("username", "UserId can't be blank");
            }
            if(StringUtils.isEmpty(getPassword()))
            {
                addFieldError("password","password can't be blank");
            }       
        }

Or using XML as

    <validators>  
          <field name="username">  
               <field-validator type="requiredstring">  
                    <message>userID is Required.</message>  
               </field-validator>  
               <field-validator type="regex">  
           <param name="expression">[a-zA-Z]{2,20}</param>  
           <message>Please enter valid userID.</message>  
         </field-validator>  
          </field>  
           <field name="password">  
               <field-validator type="requiredstring">  
                    <message>Password is Required.</message>  
               </field-validator>  
          </field> 
          <field name="address">  
               <field-validator type="requiredstring">  
                    <message>address is Required.</message>  
               </field-validator>  
          </field>  
          <field name="gender">  
               <field-validator type="requiredstring">  
                    <message>Gender is Required.</message>  
               </field-validator>  
          </field> 
          <field name="country">  
               <field-validator type="regex">  
           <param name="expression">India|Australia|England|Srilanka|Japan|Other</param>  
           <message>Course is required.</message>  
         </field-validator>  
          </field>  
     </validators>  

Name of XML file should be in the format -validation.xml and in the same package

Finally add

    <result name="input">/login.jsp</result> in struts.xml file inside <action></action>

Upvotes: 1

Related Questions