Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non mandatory field validation and Regular expression validation in primefaces?

 
Janardhan Chowdary
Greenhorn
Posts: 15
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18009
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic