4

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

How can I do this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Linconnue55
  • 187
  • 3
  • 7
  • 16

2 Answers2

14

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.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
3

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>
Giovanni Perea
  • 328
  • 3
  • 5
  • Range À-ú not includes some "regular" alphabetic characters as 'ů' or 'ž' but includes two non alphabetic - ÷ (c3b7) and × (c397) – Petr Slavicek Apr 23 '20 at 14:48