Fórumok

Server Side validation using Validator class in Lifaray

thumbnail
sheela mk, módosítva 11 év-val korábban

Server Side validation using Validator class in Lifaray

Regular Member Bejegyzések: 111 Csatlakozás dátuma: 2012.02.17. Legújabb bejegyzések
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, módosítva 11 év-val korábban

RE: Server Side validation using Validator class in Lifaray

Liferay Master Bejegyzések: 875 Csatlakozás dátuma: 2011.01.07. Legújabb bejegyzések
/**
* 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, módosítva 11 év-val korábban

RE: Server Side validation using Validator class in Lifaray

Liferay Master Bejegyzések: 875 Csatlakozás dátuma: 2011.01.07. Legújabb bejegyzések
Not sure but try to use validate(phoneNumber) method of PhoneNumberUtil or PhoneNumberFormatUtil that may also help you one this .
thumbnail
sheela mk, módosítva 11 év-val korábban

RE: Server Side validation using Validator class in Lifaray

Regular Member Bejegyzések: 111 Csatlakozás dátuma: 2012.02.17. Legújabb bejegyzések
Thanks..de..for replying

I try out..and let U knw when I do it..emoticon
thumbnail
Akash Jaisawal, módosítva 8 év-val korábban

RE: Server Side validation using Validator class in Lifaray

Regular Member Bejegyzések: 141 Csatlakozás dátuma: 2012.03.03. Legújabb bejegyzések
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");
}
}
}