Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict inputText value to alphabetic characters only

I need to allow only alphabetic characters [A-Z,a-z] in a PrimeFaces inputText field.

How can I do this?

like image 803
Linconnue55 Avatar asked Dec 04 '22 04:12

Linconnue55


2 Answers

Not specific to Primefaces but to underlying JSF:

You can use a regex validator on your input field:

   <h:inputText value="#{myBean.myText}" >
     <f:validateRegex pattern="[a-zA-Z]+"/>
   </h:inputText>

This will work with p:inputText as well.

Adapt the regular expression to your functional requirements.

like image 199
Matt Handy Avatar answered Feb 16 '23 20:02

Matt Handy


If you need to avoid characters in the view (input text) you can user p:keyFilter tag as below

<p:inputText id="apePat" 
            placeholder="Apellido Paterno" 
            value="#{actualizaDatos.user.apePat}" 
            autocomplete="off" 
            label="Apellido Paterno" 
            validatorMessage="El campo apellido paterno es requerido">

            <f:validateRequired/>

            <p:keyFilter regEx="/[a-zA-ZÀ-ú\\s\\' ]+/"/>

</p:inputText>
like image 37
Giovanni Perea Avatar answered Feb 16 '23 20:02

Giovanni Perea