doubts about Regex using Primefaces

Asked

Viewed 299 times

3

I own a input, where when a name is entered in the field, ex.: João Da Silva, he of the error of Regex. In case this field should allow uppercase and lowercase letters. Now when I put space it does not save.

Follows the code:

<p:outputLabel value="Nome" for="nome" />
<p:inputText id="nome" value="#{cadastroClienteBean.cliente.nome}" size="40" maxlength="50" required="true">
<f:validateRegex pattern="[a-zA-Z]+" />
</p:inputText>
  • 3

    regex only allows letters (uppercase and minuscule), you can add a space in the list like this [a-zA-Z ] or [a-zA-Z\s]

  • I haven’t yet managed the two ways you quoted.

1 answer

1

When creating a regular expression, you need to keep in mind what you want to give match. I learned a lot about this from Aurelius Verde’s book Regular Expressions; the regular expression guide is made available free of charge.

I will divide the construction of this regular expression in 2 steps:

  1. Match a name without special characters or numbers (ASCII)
  2. Match on multiple names

(I will use the nomenclature of the regex guide of the Green Aurelium)

To match a name, I need to know which characters may be in the name. In this case, all letters (uppercase and lowercase), at least once. To match the letters, we can use a list containing those letters: [A-Za-z]. So I can guarantee the letters. To guarantee a name, a letter needs to appear once or more times, so it needs to be the plus, staying like this now: [A-Za-z]+

The interpretation you get when reading this regular expression is: alphabetic characters, in high or low box, that appear once or more times. That doesn’t include multiple words yet.

To include another name, I need to separate the next name from the previous one with space, and it needs to have 1 or more letters that make up a name. So I can say that subsequent names are given by regular expression [A-Za-z]+ (note the space before the list). This expression is interpreted as a space followed by a name (as described above). I can use the group followed by asterisk, because secondary names may appear once or more times. So, ( [A-za-z]+)* represents all names after the first name. To put a name before that, we obtain [A-za-z]+( [A-za-z]+)*.

A weaker check (which allows multiple spaces between names, to begin with space or to consist exclusively of spaces) is [ A-Za-z]+. The interpretation of this expression is: a string of alphabetic characters, independent of case, and blank spaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.