• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Non mandatory field validation and Regular expression validation in primefaces?

 
Greenhorn
Posts: 15
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic