Foren

Server Side validation using Validator class in Lifaray

thumbnail
sheela mk, geändert vor 11 Jahren.

Server Side validation using Validator class in Lifaray

Regular Member Beiträge: 111 Beitrittsdatum: 17.02.12 Neueste Beiträge
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
thumbnail
Jitendra Rajput, geändert vor 11 Jahren.

RE: Server Side validation using Validator class in Lifaray

Liferay Master Beiträge: 875 Beitrittsdatum: 07.01.11 Neueste Beiträge
/**
* 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.
thumbnail
Jitendra Rajput, geändert vor 11 Jahren.

RE: Server Side validation using Validator class in Lifaray

Liferay Master Beiträge: 875 Beitrittsdatum: 07.01.11 Neueste Beiträge
Not sure but try to use validate(phoneNumber) method of PhoneNumberUtil or PhoneNumberFormatUtil that may also help you one this .
thumbnail
sheela mk, geändert vor 11 Jahren.

RE: Server Side validation using Validator class in Lifaray

Regular Member Beiträge: 111 Beitrittsdatum: 17.02.12 Neueste Beiträge
Thanks..de..for replying

I try out..and let U knw when I do it..emoticon
thumbnail
Akash Jaisawal, geändert vor 8 Jahren.

RE: Server Side validation using Validator class in Lifaray

Regular Member Beiträge: 141 Beitrittsdatum: 03.03.12 Neueste Beiträge
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");
}
}
}