PHP Alphanumeric Regular Expression

user3448743

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
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }   
?>
Avinash Raj

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.

DEMO

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

alphanumeric regular expression in R

From Dev

Regular expression for alphanumeric in Angularjs

From Dev

regular expression not between alphanumeric

From Dev

Regular Expression for a alphanumeric after a text

From Dev

Regular Expression to check alphanumeric with decimals but reject only alphanumeric, alphabet or numeric

From Dev

Regular Expression advice for [Alphanumeric][alphanumeric.-_@] 31 characters

From Dev

javascript- regular expression alphanumeric and special characters

From Dev

Regular expression for alphanumeric and underscore c#

From Dev

Regular Expression for the given format - start and end with alphanumeric

From Dev

Regular Expression For Alphanumeric Characters and Underscores and character Limit

From Dev

Regular expression for an alphanumeric string of 10 characters

From Dev

Complex regular expression - alphanumeric, hyphen-space-hyphen, + and ++

From Dev

Regular Expression pattern : alphanumeric(compulsory) , some special characters (optional)

From Dev

Regular expression to match alphanumeric, hyphen, underscore and space string

From Dev

VBA regular expression containing commas, dashes and alphanumeric characters

From Dev

Regular expression to accept alphanumeric strings but reject a particular string (.htaccess)

From Dev

Regular Expression to replace all non alphanumeric characters except / to empty("") character

From Dev

Regular Expression for alphanumeric with first alphabet and at least one alphabet and one number

From Dev

Php regular expression for date

From Dev

PHP refusing this regular expression

From Dev

PHP Regular expression with arrows (>>)

From Dev

Regular expression in PHP with comma

From Dev

php Regular expression fails

From Dev

+ Symbol in Regular Expression PHP

From Dev

Regular expression issue in php

From Dev

Regular Expression (PHP) => greater that or if not

From Dev

Regular expression PHP

From Dev

Creating regular expression in php

From Dev

PHP Regular Expression not matching

Related Related

  1. 1

    alphanumeric regular expression in R

  2. 2

    Regular expression for alphanumeric in Angularjs

  3. 3

    regular expression not between alphanumeric

  4. 4

    Regular Expression for a alphanumeric after a text

  5. 5

    Regular Expression to check alphanumeric with decimals but reject only alphanumeric, alphabet or numeric

  6. 6

    Regular Expression advice for [Alphanumeric][alphanumeric.-_@] 31 characters

  7. 7

    javascript- regular expression alphanumeric and special characters

  8. 8

    Regular expression for alphanumeric and underscore c#

  9. 9

    Regular Expression for the given format - start and end with alphanumeric

  10. 10

    Regular Expression For Alphanumeric Characters and Underscores and character Limit

  11. 11

    Regular expression for an alphanumeric string of 10 characters

  12. 12

    Complex regular expression - alphanumeric, hyphen-space-hyphen, + and ++

  13. 13

    Regular Expression pattern : alphanumeric(compulsory) , some special characters (optional)

  14. 14

    Regular expression to match alphanumeric, hyphen, underscore and space string

  15. 15

    VBA regular expression containing commas, dashes and alphanumeric characters

  16. 16

    Regular expression to accept alphanumeric strings but reject a particular string (.htaccess)

  17. 17

    Regular Expression to replace all non alphanumeric characters except / to empty("") character

  18. 18

    Regular Expression for alphanumeric with first alphabet and at least one alphabet and one number

  19. 19

    Php regular expression for date

  20. 20

    PHP refusing this regular expression

  21. 21

    PHP Regular expression with arrows (>>)

  22. 22

    Regular expression in PHP with comma

  23. 23

    php Regular expression fails

  24. 24

    + Symbol in Regular Expression PHP

  25. 25

    Regular expression issue in php

  26. 26

    Regular Expression (PHP) => greater that or if not

  27. 27

    Regular expression PHP

  28. 28

    Creating regular expression in php

  29. 29

    PHP Regular Expression not matching

HotTag

Archive