Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:25:18+00:00 2026-05-28T20:25:18+00:00

I am using preg_match_all in php to check the characters in the username and

  • 0

I am using preg_match_all in php to check the characters in the username and password before i add them to the database, but I can’t seem to get it to work the way I want. Here is what I have right now:

preg_match_all(USERNAME_PATTERN,$username,$usernameMatches);
preg_match_all(PASSWORD_PATTERN,$password,$passwordMatches);

Here are the patterns, defined as constants:

/*Username and Password Patterns*/
define("USERNAME_PATTERN","[-*_a-z0-9A-Z.]");
define("PASSWORD_PATTERN","[_a-z0-9A-Z]");

I don’t know what is wrong with it. Its suppose to check to see if the username has anything other than a-z, A-Z, 0-9, the dash, the astrisk,the underscore, and a period. The password is the same as the username.

Here is the code I use to check:

if ($usernameMatches == 0){
echo("Bad characters in username<br />");
}

The password is the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:25:20+00:00Added an answer on May 28, 2026 at 8:25 pm

    There are several issues with your code.

    1. Your regexes only match a single character.
    2. There are no begin and end anchors in your regex.
    3. Make sure you instantiate the matches array before calling preg_match_all()
    4. Your regex should be surrounded with a / (or other valid char).
    5. Check for a non-match by checking that the array is empty, not by checking if it’s equal to zero. There are many type/value checking gotchas in php and it’s best to avoid them.

    Try this:

    /*Username and Password Patterns*/
    define("USERNAME_PATTERN","/^[-*_a-z0-9A-Z.]+$/");
    define("PASSWORD_PATTERN","/^[_a-z0-9A-Z]+$/");
    
    $usernameMatches = array();
    $passwordMatches = array();
    
    preg_match_all(USERNAME_PATTERN,$username,$usernameMatches);
    preg_match_all(PASSWORD_PATTERN,$password,$passwordMatches);
    
    if (empty($usernameMatches)){
        echo("Bad characters in username<br />");
    }
    
    if (empty($passwordMatches)){
        echo("Bad characters in password<br />");
    }
    

    BTW: Your code could simplified by simply using preg_match() instead of preg_match_all(). Something like this should work as well as your code:

    /*Username and Password Patterns*/
    define("USERNAME_PATTERN","/^[-*_a-z0-9A-Z.]+$/");
    define("PASSWORD_PATTERN","/^[_a-z0-9A-Z]+$/");
    
    if (!preg_match(USERNAME_PATTERN, $username)) {
        echo("Bad characters in username<br />");
    }
    if (!preg_match(PASSWORD_PATTERN, $password)) {
        echo("Bad characters in password<br />");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate usernames in PHP using preg_match() but I can't seem to
How can I get each value returned by PHP preg_match_all using for each loop,
Using the following code I get img tags from some html and check them
Using PHP and preg_match_all I'm trying to get all the HTML content between the
How can I get real host name by not using $_SERVER['SERVER_NAME'] in PHP? Is
ive created a form and im using php to check if the username exists
I want extract the path of images from a html page using PHP-preg_match_all(), the
I'm using this function: preg_match_all('/((\d{9})\.html)/s', $content, $results); It works fine but i just want
I get an array using the function preg_match_all. The code is as it is:
I'm trying to extract richard123 using php preg_replace but there are a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.