Mahmoud Saleh
Mahmoud Saleh

Reputation: 33605

Validate input onblur/on focus lost

i am making email validation for an input with regular expression as follows:

<h:inputText id="email" value="#{settingsBean.aFriendEmail}" required="true" label="Email" validatorMessage="#{settingsBean.aFriendEmail} is not valid">
    <f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<p:message for="email" />

and i was wondering how to make this validation onblur/on focus lost for that input. please advise how to accomplish that, thanks.

also, i want to restrict the valid domains to be .com, .net, .org any advises about that ?

Upvotes: 1

Views: 9123

Answers (1)

BalusC
BalusC

Reputation: 1108632

Add a <f:ajax> which is hooked on blur event and updates the message on complete.

<h:inputText id="email" ...>
    ...
    <f:ajax event="blur" render="emailMessage" />
</h:inputText>
<p:message id="emailMessage" for="email" />

Restricting the email domain by regex is a separate question completely unrelated to JSF.


Unrelated to the concrete problem, your validatorMessage is not right.

validatorMessage="#{settingsBean.aFriendEmail} is not valid"

The model value is never updated when the validation has failed, so this would always print the initial value, which may be null/empty. You should instead display the component's submitted value.

validatorMessage="#{component.submittedValue} is not valid"

Upvotes: 3

Related Questions