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.
He who cannot obey himself will be commanded. That is the nature of living creatures.
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.