I am new to PHP, and very new to Regular Expressions. I am currently trying to write a Regular Expression that makes sure the characters are all Alpha Numeric, and that accepts other characters a well. I would like to accept áéíñóúüÁÉÍÑÓÚÜ and @.+-. To accept all alphanumeric characters, and the symbols @.+-. I use the expression:
if (!preg_match("!^[\[email protected]]*$!")
Also, before I continue I would like to add. These are values being grabbed from a form input, and they should all be one word phrases (as in no spaces). Ok now lets continue! :D Now I want to grab the characters áéíñóúüÁÉÍÑÓÚÜ as well. When I use the expression:
if (!preg_match("/[\wáéíñóúüÁÉÍÑÓÚÜ@.\+-].*/")
It does not seem to work, could anyone please help? If you are confused, or it is worded bad just ask and I will rewrite it :)
UPDATE
My expression:
if (!preg_match("!^[\[email protected]]*$!")
does work, it is the other one that is not working. Thank You! :)
UPDATE 2
Now when I try the expression:
if (!preg_match("~^[\p{L}\p{N}@+._-]+$~", $email)) {
with the input of michael.jonesáéí@gmail.com it raises a error. There error it raises is "Email contains restricted characters." because I made it say that. Here is the specific code I am using:
<?php
if (!preg_match("~^[\p{L}\p{N}@+._-]+$~", $email)) {
?>
<div id="allinputboxerror" class="col-xs-12"> Email contains restricted characters.</div>
<?php } ?>
}
and here is all of the code I am using:
<?php
if ($_POST) {
$emailtest1 = False;
$emailtest2 = False;
$emailtest3 = False;
$emailtest4 = False;
$emailtest5 = False;
// Test #1 - Makes sure there is a input
if(empty($_POST['email'])) {
?>
<div id="allinputboxerror" class="col-xs-12"> Please enter your email. </div>
<?php
}
else {
$emailtest1 = True;
// Test #2 - Makes sure it does not already exist
$usernamequery = "SELECT 1 FROM users WHERE email = :email";
$usernameparams = array(':email' => $_POST['email']);
try{
$emailstmt = $connection->prepare($usernamequery);
$emailresult = $emailstmt->execute($usernameparams);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$emailcolumns = $emailstmt->fetch();
if($emailcolumns){
?>
<div id="allinputboxerror" class="col-xs-12"> This email already exists. </div>
<?php
}
else {
$emailtest2 = True;
// Test #3 - Makes sure it fits the length requirements
if(strlen($email) < 5 ) {
?>
<div id="allinputboxerror" class="col-xs-12"> Email is to short. </div>
<?php
}
elseif(strlen($email) > 75 ) {
?>
<div id="allinputboxerror" class="col-xs-12"> Email is to long. </div>
<?php
}
else {
$emailtest3 = True;
// Test #4 - Makes sure it does not have any restricted characters
if (!preg_match("~^[\p{L}\p{N}@+._-]+$~", $email)) {
?>
<div id="allinputboxerror" class="col-xs-12"> Email contains restricted characters. </div>
<?php
}
else {
$emailtest4 = True;
// Test #5 - Makes sure the email is valid
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
?>
<div id="allinputboxerror" class="col-xs-12"> Not a valid email. </div>
<?php
}
else {
$emailtest5 = True;
// Final Check
if (($emailtest1 = True) and ($emailtest2 = True) and ($emailtest3 = True) and ($emailtest4 = True) and ($emailtest5 = True)) {
// Email is valid! :D
}
else {
?>
<div id="allinputboxerror" class="col-xs-12"> There is a error. </div>
<?php
}
}
}
}
}
}
}
?>
Regex to accept all alphanumeric characters, and the symbols @.+-.
^[\p{L}\p{N}@+.-]+$
Your regex ^[\[email protected]]*$
won't match the accented characters. That is \w
would match only the English alphabets, digits 0-9
plus the underscore symbol.
\p{L}
matches any kind of letter from any language
\p{N}
matches any kind of numeric character in any script.
+
after the char class would repeat the previous token one or more times. I suggest you to use +
instead of *
because *
repeats the previous token zero or more times. So it matches empty strings also.
Update:
You ust need to include the unicode modifier u
, so that it would make \p{L}
to match accented characters.
if (!preg_match("~^[\p{L}\p{N}@+._-]+$~u", $email)) {
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments