phpregex

Instagram username Regex -PHP


I have register form and input of "instagram-username".

Instagram username can included only: a-z A-Z 0-9 dot(.) underline(_)

This is my code:

if(!empty($instaUsername) && preg_match("/([A-Za-z._])\w+/", $instaUsername)) {
            throw new Exception ("Your name Instagram is incorrect");
}

When $instaUsername = "name.123" or "name_123" this give me the error.

How to make a regular expression according to the following requirements?

a-z A-Z 0-9 dot(.) underline(_)

I would love to have good tutorials on regex as comment.


Solution

  • Moving forward from the comment section, this is what you want:

    if(!empty($instaUsername) && preg_match('/^[a-zA-Z0-9._]+$/', $instaUsername)) {
                throw new Exception ("Your name Instagram is incorrect");
    }