I need a code which will accept a name (last name and first name). Therefore I need it to accept only letters and it should accept accented letters as well (letters like á, č, ť, í, é, ľ, š, ď, ž, ý -> those are Slovak letters). Plus a space between last and first name.
I have already tried some codes I found on this website, but nothing worked as it should.
I want to do a form and if the name is filled bad it will give you a warning. Now I have this code:
$mistakes = array();
if (isset($_POST["submit"])) {
if (isset($_POST['name'])) $name = securee($_POST['name']); else $name = '';
if (!check_lenght_of_name($name)) $mistakes['name'] = 'Name has wrong lenght';
if (empty($name)) $mistakes['meno'] = 'You didnt fill name';
if (!preg_match("~^\p{L}+(?:[-\h']\p{L}+)*$~u", $name)) $mistakes['name'] = 'You used a wrong letter';
}
if (!empty($mistakes)) {
echo '<p class="mist"><strong>Mistakes</strong>:<br>';
foreach($mistakes as $mis) {
echo "$mis<br>\n";
}
echo '</p>';
}
Here are two functions I use:
function securee($wha){
return trim(strip_tags($wha));
}
function check_lenght_of_name($n) {
return substr_count($n, " ") == 1 && strlen(substr($n, 0 , strpos($n, " "))) >= 3 && strlen(substr($n, strpos($n, " "), strlen($n))) >= 4;
But the problem is that when I dont fill name then what I get as a warning is 'You used a wrong letter' but I should have gotten 'You didnt fill name'. The problem is here
(!preg_match("~^\p{L}+(?:[-\h']\p{L}+)*$~u", $name))
I have already tried several preg_match codes, but nothing works as I want it to work. Any ideas?
I think you want this regex:
/^[A-Záčťíéľšďžý]+ [A-Záčťíéľšďžý]+$/i
/^[A-Záčťíéľšďžý]+: Must start with at least one character from the approved character set
: Must be followed by exactly one space
[a-zA-Záčťíéľšďžý]+$/: Must end with at least one character from the approved character set
i: Case insensitive
For future regex debugging I highly recommend using https://regexr.com/ so that you can write and test your regular expressions really quickly and the site will explain what the regex is doing.
Related
I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok".
I don't know what I am doing wrong, can you help? thanks
$string = 'é';
if(preg_match('/^[\p{L}]+$/i', $string)){
echo 'ok';
} else{
echo 'nok';
}
Add the UTF-8 modifier flag (u) to your expression:
/^\p{L}+$/ui
There is also no need to wrap \p{L} inside of a character class.
I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.
<?php
$string = 'États unis and états unis';
if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){
echo 'ok';
} else{
echo 'nok';
}
?>
If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9
Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:
if (trim($_POST['forminput']) == "") {... some error message ...}
to reject the submission.
When people sign up to my site I validate their names with this code:
if (preg_match("[\W]", $name))
{
$mess = $mess . "Your name must contain letters only.<br>";
$status = "NOTOK";
}
This is because your actual name cannot contain symbols unless your parents were drunk when they named you.
However, this regex doesn't detect spaces. How can I fix it?
You can use the following regular expression:
^[\w ]+$
This matches any combinations of word characters \w and spaces , but as the guys said be careful because some names might contain other symbols.
So you can use it like this:
if (preg_match("/^[\\w ]+$/", $name)) {
// valid name
}
else {
// invalid name
}
Try this:
<?php
$user_input = 'User_name';
if (!preg_match('/^[a-z0-9_\s]+$/i', $user_input)) {
// Matches English letters, numbers underscores(_) and spaces
$mess = $mess . "Your name must contain letters only.<br>";
$status = "NOTOK";
}
?>
You just missed regexp separators. I do this sometimes even after 10 years of programming.
if (preg_match("/[\W]/", $name)) ...
I'm using regex (I think that's what it's called - haha) to check my users' name to make sure it's valid. I want to make sure that the user doesn't have any characters. Just letters and spaces. I've got the only letters part down, but I can't get the spaces part fixed.
Here's what I'm using now..
if(preg_match("/[^a-zA-Z]/", " ", $name) != 0) {
$errorlist = $errorlist."<li>You must enter a valid First and Last name (check for invalid characters)</li>";
}
Anyone see what I'm doing wrong?
Just add a space to your regex pattern. PS - your second parameter for preg_match() should probably be $name, right? Is there a reason you were testing " "?
if(preg_match("/[^a-zA-Z ]/", $name) != 0) {
$errorlist = $errorlist."<li>You must enter a valid First and Last name (check for invalid characters)</li>";
}
For my PHP script I have this code:
if (!preg_match("/[^A-Za-z]/", $usersurname))
$usersurname_valid = 1;
This worked untill I realized a surname can be two or more words... doh.
Anyone can tell me how to write this code if I want to allow 1 space between two worlds? For example:
Jan Klaas is now wrong and Jan Klaas should be allowed, also Jan Klaas Martijn and so on should be allowed.
Even better would be a preg replace, to replace two or more spaces with 1, so when you write: Jan(space)(space)Klaas or Jan(space)(space)(space)(space)Klaas, it would return Jan(space)Klaas.
I searched around for a while but somehow I just can't get this space matching to work..
PS: When I got this working, I will apply this for the mid and last name too ofcourse.
===========================================
EDIT: After you helping me out, I re-wrote my code to:
// validate usersurname
$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname))
$usersurname_valid = 1;
// validate usermidname
$usermidname = preg_replace("/\s{2,}/"," ", $usermidname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usermidname))
$usermidname_valid = 1;
// validate userforename
$userforename = preg_replace("/\s{2,}/"," ", $userforename);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$userforename))
$userforename_valid = 1;
and the error notifications
elseif ($usersurname_valid !=1)
echo ("<p id='notification'>Only alphabetic character are allowed for the last name. $usersurname $usermidname $userforename</p>");
// usermidname character validation
elseif ($usermidname_valid !=1)
echo ("<p id='notification'>Only alphabetic character are allowed for the middle name. $usersurname $usermidname $userforename</p>");
// userforename character validation
elseif ($userforename_valid !=1)
echo ("<p id='notification'>Only alphabetic character are allowed for the (EDIT) first name. $usersurname $usermidname $userforename</p>");
Replacing the spaces are working well and I need this preg_match to check on on A-Za-z + space. I think in this case it doesn't matter if it's matching more than 1 spaces because it's replaced anyway, right?
EDIT:
Solution for my case:
$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/[^A-Za-z ]/", $usersurname))
This does the work. Thanks for helping out, J0HN
Well, solving the problem you have in mind:
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname)) { ... }
But, well, it's just a part of the solution, and it's not bulletproof. Look at the list of common mistakes when handling names.
So, you'd better to re-think on your validation approach.
Replacing the multiple spaces is simpler to achieve as a separate instruction, something like
$processed_usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
This will match and replace any two or more consequent whitespace characters (space, tab, linebreak and carriage return) to single space
I would like to validate a string with a pattern that can only contain letters (including letters with accents). Here is the code I use and it always returns "nok".
I don't know what I am doing wrong, can you help? thanks
$string = 'é';
if(preg_match('/^[\p{L}]+$/i', $string)){
echo 'ok';
} else{
echo 'nok';
}
Add the UTF-8 modifier flag (u) to your expression:
/^\p{L}+$/ui
There is also no need to wrap \p{L} inside of a character class.
I don't know if this helps anybody that will check this question / thread later. The code below allows only letters, accents and spaces. No symbols or punctuation like .,?/>[-< etc.
<?php
$string = 'États unis and états unis';
if(preg_match('/^[a-zA-Z \p{L}]+$/ui', $string)){
echo 'ok';
} else{
echo 'nok';
}
?>
If you want to add numbers too, just add 0-9 immediately after Z like this a-zA-Z0-9
Then if you are applying this to form validation and you are scared a client/user might just hit spacebar and submit, just use:
if (trim($_POST['forminput']) == "") {... some error message ...}
to reject the submission.