Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
sheela mk
Server Side validation using Validator class in Lifaray
June 5, 2012 10:59 PM
Answer

sheela mk

Rank: Regular Member

Posts: 111

Join Date: February 16, 2012

Recent Posts

hai..EveryBody,

Pls,Let me know how to validate phoneNo using Validator class...

I'm using

public static boolean validateuser(User user,List<String> errors)
{
boolean valid=true;
if(Validator.isNull(user.getPhoneNo()))
{
System.out.println("phone");
errors.add("phone-required");
valid=false;
}
else
{
if(!Validator.isPhoneNumber(user.getPhoneNo()))
{
errors.add("phone-required");
valid=false;
}
}

}



I saw the code of Validator class also..isPhoneNumber method in turn calls isNumber method of Validator class...
so..it doesnt check exactly for 10 digit No..if i give two digit also..it accepts..

Pls Let me knw how to handle it....

Thanksemoticon
Jitendra Rajput
RE: Server Side validation using Validator class in Lifaray
June 5, 2012 11:02 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 858

Join Date: January 7, 2011

Recent Posts

/**
* Returns <code>true</code> if the string is a valid phone number. The only
* requirement is that there are decimal digits in the string; length and
* format are not checked.
*
* @param phoneNumber the string to check
* @return <code>true</code> if the string is a valid phone number;
* <code>false</code> otherwise
*/
public static boolean isPhoneNumber(String phoneNumber) {
return isNumber(StringUtil.extractDigits(phoneNumber));
}



As you can see in Liferay code its clearly mentioned that it will not check length .
I think you can write one more condition to check length of the number and if its 10 then go ahead otherwise show error message.
Jitendra Rajput
RE: Server Side validation using Validator class in Lifaray
June 5, 2012 11:08 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 858

Join Date: January 7, 2011

Recent Posts

Not sure but try to use validate(phoneNumber) method of PhoneNumberUtil or PhoneNumberFormatUtil that may also help you one this .
sheela mk
RE: Server Side validation using Validator class in Lifaray
June 6, 2012 5:13 AM
Answer

sheela mk

Rank: Regular Member

Posts: 111

Join Date: February 16, 2012

Recent Posts

Thanks..de..for replying

I try out..and let U knw when I do it..emoticon
Akash Jaisawal
RE: Server Side validation using Validator class in Lifaray
June 12, 2015 11:52 PM
Answer

Akash Jaisawal

Rank: Regular Member

Posts: 103

Join Date: March 3, 2012

Recent Posts

I know its late, but it may help others......!!
if(!Validator.isDigit(phoneNumber))
{if(phoneNumber.length()!=10)
{
SessionErrors.add(request, "lessLength");
}
request.setAttribute("phoneNumber", phoneNumber);

if(SessionErrors.isEmpty(request))
{
response.setRenderParameter("jspPage","/path/to/your.jsp");
}
}
}