Validation for optional field

I have these lines of code to check for an email address:

<h:outputText value="#{labels.eMail}: " /> <p:inputText size="30" maxlength="50" validatorMessage="Please provide a valid e-mail address" value="#{personelView.eMail}"> <f:validateRegex pattern=" .+@. +\.[a-zA-Z]+" /> </p:inputText> 

When I leave the email address field blank and submit the form, it gives a validation error. Why is jsf doing this? Shouldn't it just check the email field with the above code? I even tried to add:

 required="false" 

still no good. Does anyone know about this case?

+4
source share
1 answer

your inputText value inputText checked against your <f:validateRegex pattern=" .+@. +\.[a-zA-Z]+" /> , and if its value is invalid, you get an error, so you need to improve the regular expression so that it matched your pattern or took an empty string ...

So wrapping with () and adding ? must complete the task

Try

 <f:validateRegex pattern="( .+@. +\.[a-zA-Z]+)?"/> 
+14
source

Source: https://habr.com/ru/post/1412735/


All Articles