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>";
}
Related
I am trying to validate if a new user account's password is matching these criterias:
Between 8-30 characters long
Contains at least 1 lowercase letter (a-z)
Contains at least 1 uppercase letter (A-Z)
Contains at least 1 of the following special characters: _-!#*#&
I have a function like this:
function validPassword($str) {
return preg_match("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_-!#*#&])[A-Za-z\d_-!#*#&]{8,30}$", $str);
}
But I am getting an error. It should return "true" for this password for example: HelloWorld123!
But instead it is returning false. Any idea what may be wrong?
if (validPassword($password) == true) {
// good password
}
You forgot to escape '-', and delimiters...
function validPassword($str) {
return preg_match("/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_\-!#*#&])[A-Za-z\d_\-!#*#&]{8,30}$/", $str);
}
Your regex is having errors which is why there is no match in the first place.
Change your regex to this:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[_\-!#*#&])[A-Za-z\d_\-!#*#&]{8,30}$
Have a look at your regex in action here: https://regex101.com/r/ogPPeb/1
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.
I have this code in preg_match
if (preg_match("/(for+\([\w\-]+\;[\w\-]+\;[\w\-]+\){)/",$email))
{
$message = "Valid input";
}
else
$message ="Invalid Input";
if the user will input for(aw;aw;aw){
if will output Valid input
but if the user will put a space like for (awd ; awd; awd) {
it will output invalid input..
my problem is how can i bypass space or remove space without using explode to my string..
need help..
You can match a space like any other character. So for example, you can just add spaces where needed, like below:
if (preg_match("/(for+ *\([\w\-]+ *\; *[\w\-]+ *\; *[\w\-]+\) *{)/",$email))
However, for+ matches 1 or more literal r's so would also match positively on forrrr, so just using for might be more appropriate there.
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'd like to get some help regarding PHP.
Let's say I have a string ($fullname).
I want to validate that it's in the form of "Firstname_Lastname".
For example, make sure that it's "Nathan_Phillips" and not "Nathan Phillips" or "Nathan122" etc.
Can you guys help me with the function?
Thanks in advance! :-)
------------ EDIT -------------
Thank you guys! Managed to do that. Also added the numbers filter. Here's the function:
function isValidName($name)
{
if (strcspn($name, '0123456789') != strlen($name))
return FALSE;
$name = str_replace(" ", "", $name);
$result = explode("_", $name);
if(count($result) == 2)
return TRUE;
else
return FALSE;
}
Usage example:
if(isValidName("Test_Test") == TRUE)
echo "Valid name.";
else
echo "Invalid name.";
Thanks again!
Maybe try something like this:
function checkInput($input) {
$pattern = '/^[A-Za-z]{2,50}_[A-Za-z]{2,50}/';
return preg_match($pattern, substr($input,3), $matches, PREG_OFFSET_CAPTURE);
}
This will accept a string containing 2-50 alphabetic characters, followed by an underscore, followed by 2-50 alphabetic characters.
I'm not the best with regex, so I invite corrections if anyone sees a flaw.
If you have special characters (è, í, etc.), the regex I gave probably won't accept it. Also, it won't accept names like O'Reilly or hyphenated names. See this:
Regex for names
I'll let you track down all the exceptions to the regex for names, but I definitely think regex is the way to go.