GeeCON Prague 2014*
The moose likes JSF and the fly likes Non mandatory field validation and Regular expression validation in primefaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "Non mandatory field validation and Regular expression validation in primefaces?" Watch "Non mandatory field validation and Regular expression validation in primefaces?" New topic
Author

Non mandatory field validation and Regular expression validation in primefaces?

Janardhan Chowdary
Greenhorn

Joined: May 21, 2012
Posts: 15

Hi friends,

Requirement:- I have a field called Phone number which is optional. And i have to allow only the numbers and ' - ' (hyphen) symbol.
Method I followed:- <f:validateRegex pattern="^[0-9]+[- ]?[0-9]+"></f:validateRegex>

Issue:- The field is optional so the value is empty still it will go for check against regular expression and show error message.

Please tell me a solution friends.

Thank you.


He who cannot obey himself will be commanded. That is the nature of living creatures.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16095
    
  21

I could be wrong here, but I think that as long as you don't have the 'required="true"' attribute set on the input control that an empty input will be accepted.

However, if that isn't allowed, try an extended pattern that allows for an empty input. Something like this:


You might also consider creating a custom phone number validator class for this. It won't solve your problem, but it's easier to work with and something you can re-use. Some JSF extension tagsets even include special controls for things like phone numbers since it's such a common requirement.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
GeeCON Prague 2014
 
subject: Non mandatory field validation and Regular expression validation in primefaces?