Search code examples
jsfprimefacesjsf-2.2mojarra

Does f:validateRegex render to javascript?


I have a p:inputText (PrimeFaces) with an included f:validateRegex field. I'm trying to make sure the entered information follows a format. Snippet:

<p:inputText id="usernameInput" required="true" validatorMessage="Invalid Email" value="#{sessionHandler.username}" >
    <f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"/>
    <f:ajax event="keyUp" render="emailMessage" />
</p:inputText>
<p:watermark for="usernameInput" value="Email"/>
<h:message id="emailMessage" />

The f:validateRegex and f:ajax don't seem to be working (No message appears if incorrect format). When I look at the rendered HTML, I notice that both the f:validateRegex and f:ajax appear just as that and not as a rendered HTML component or javascript field. So, my question: is f:validateRegex supposed to render into javascript for the client side? If so, why is it not rendering correctly?


Solution

  • Nope. JSF is a HTML code generator. <f:xxx> tags are not HTML. Browsers only understand HTML, not JSF.

    The symptoms indicate that you forgot to declare the f: XML namespace in a parent element. E.g.

    <html ... xmlns:f="http://xmlns.jcp.org/jsf/core">
    

    This way, you should not see anything related to <f:validateRegex> at all. It's namely entirely server side. The <f:ajax> should translate to a script in some HTML DOM event attribute, such as onkeyup when you use keyup (and thus not keyUp).