Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys

I am trying to do server side validation. i am storing error messages in StringBuilder object. If, I enter values in wrong format, Then it is working fine.....but ,if I enter values in proper format, it is failing to display success msg...

I found that Problem is with StringBuilder object. but unable to figure out the exact problem.

If I use length(), method instead of equals() method then it works fine.

...pls help me to sort out this issue

Java
public void validateForm(ActionRequest request, ActionResponse actionResponse)
	   
	{   
	        String firstName = request.getParameter("firstName");
	       
	        String lastName =  request.getParameter("lastName");
	        
	        String telephone = request.getParameter("phone");
	        
	        String email =  request.getParameter("email");
	        
	        String comments= request.getParameter("comments");
	        
	        StringBuilder errorMsg=new StringBuilder();
	        
	        //StringBuffer errorMsg=new StringBuffer();
	        
	        System.out.println(errorMsg.equals(""));
	        System.out.println(errorMsg.length());
	        System.out.println("errrr="+errorMsg);
	       
	        String fname="[a-zA-z]+([ '-][a-zA-Z]+)*";
	        
	        String lname="[a-zA-z]+([ '-][a-zA-Z]+)*";
	        
	        String matchPhoneNumberPattern="[0-9]*";
	        
	        String emailPattern="^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
	        
	        if (firstName == null ||firstName.equals(""))
	        {
	        	errorMsg.append("Please Enter First Name "+"<br />");
	            
	        }
	        else if(!firstName.matches(fname))
	        {
	            errorMsg.append("Please Enter First Namet "+"<br />");
	            
	        }
	       
	        if(lastName == null ||lastName .isEmpty())
	        {
	            errorMsg.append("Please Enter Last Name "+"<br />");
	            
	        }
	        else if(!lastName.matches(lname))
	        {
	            errorMsg.append("Enter lastname in Name Format "+"<br />");
	           
	        }
	       
	        if(telephone  == null || telephone.isEmpty())
	        {
	            errorMsg.append("Please Enter Telephone Number "+"<br />");
	            
	        }
	        else if(!telephone.matches(matchPhoneNumberPattern))
	        {
	           
	            errorMsg.append("Invalid Number please Enter valid number"+"<br />");
	          
	        }
	        if(email == null ||  email.isEmpty())
	        {
	            errorMsg.append("Please Enter Email "+"<br />");
	            
	        }
	        else if(!email.matches(emailPattern))
	        {
	           
	            errorMsg.append("Invalid email id"+"<br />");
	       
	        }
	        if (comments == null ||  comments.equals(""))
	        {
	            errorMsg.append("You must Enter Comments "+"<br />");
	            
	        }
	        
	       
	      /*IN all cases this if block is executing,EVen though i enter all fields in proper format */
	        System.out.println(errorMsg.equals(""));
	        
               if(!errorMsg.equals(""))
	        
	        {
	            request.setAttribute("errorMsg", errorMsg);
	            request.setAttribute("phone", telephone);
	            request.setAttribute("email", email);

	           System.out.println("If block..........!");
	           
	        }
	        else
	        {
	            String successMsg="Form Submited Successfully";
	           
	            request.setAttribute("successMsg", successMsg);
	            System.out.println("done..........!");
	        }
	                      
	      
	        	    }
Posted
Updated 21-May-14 9:09am
v2

I think you should consider the ArrayList<errorinfo> errorList approach instead.


Java
if(!errorMsg.equals(""))

Then,change this line to
Java
if(errorList.size()>0){

}
 
Share this answer
 
Try,

C#
if(errorMsg != String.Empty)

instead of

C#
if(!errorMsg.equals(""))


Btw, you can actually use the System.Text.StringBuilder, it would be less expensive to use because you don't have to deal with boxing and un-boxing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900