i want a text that only accept letters upper/lower, accents (Á,á,É,é,Í,í,Ó,ó,Ú,ú) and character spain 'ñ,Ñ'
if (!preg_match("/[a-zA-Z]/", $_POST["text"]))
$er = "Introduce un nombre válido";
Where i must put this charácter?
if (!preg_match("/[a-zA-Z][Á,á,É,é,Í,í,Ó,ó,Ú,ú][ñ]/", $_POST["text"]))
$er = "Introduce un nombre válido";
This doesnt work, where is the wrong?
Thanks
My Answer:
The issue with your regular expression is that you are using square brackets [] to define character classes, which means it will only match one character from the specified set. Instead, you should use parentheses () to group the characters that you want to match together.
Here is the corrected regular expression:
php
if (!preg_match("/^[a-zA-ZÁáÉéÍíÓóÚúñÑ]+$/", $_POST["text"]))
$er = "Introduce un nombre válido";
In this regular expression:
- ^ indicates the start of the string
- [a-zA-ZÁáÉéÍíÓóÚúñÑ] matches any letter, accent, or the character 'ñ' or 'Ñ'
- + allows for one or more occurrences of the previous character class
- $ indicates the end of the string
This regular expression will match a string that only contains letters, accents, and the characters 'ñ' or 'Ñ'.
Rate this post
3 of 5 based on 1617 votesComments