Restrict inputText value to alphabetic characters only

18,270

Solution 1

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.

Solution 2

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>
Share:
18,270
Linconnue55
Author by

Linconnue55

Updated on June 24, 2022

Comments

  • Linconnue55
    Linconnue55 almost 2 years

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

    How can I do this?

  • Irfan Nasim
    Irfan Nasim about 8 years
    how to changed regex message? currently it is showing 'Regex pattern does not match'. @Matt Handy
  • Irfan Nasim
    Irfan Nasim about 8 years
    done with validatorMessage="Alpha numaric characters are not allowed in Last Name." property of inputbox.
  • Petr Slavicek
    Petr Slavicek about 4 years
    Range À-ú not includes some "regular" alphabetic characters as 'ů' or 'ž' but includes two non alphabetic - ÷ (c3b7) and × (c397)