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

  • Home
  • SEARCH
  • 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 9192813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:01:08+00:00 2026-06-17T21:01:08+00:00

I am writing my website user registration part, I have a simple regular expression

  • 0

I am writing my website user registration part, I have a simple regular expression as follows:

if(preg_match("/^[a-z0-9_]{3,15}$/", $username)){
    // OK...
}else{
    echo "error";
    exit();
}

I don’t want to let users to have usernames like: ‘___‘ or ‘x________y‘, this is my function which I just wrote to replace duble underscores:

function replace_repeated_underScores($string){
    $final_str = '';
    $str_len = strlen($string);
    $prev_char = '';
    for($i = 0; $i < $str_len; $i++){
        if($i > 1){
            $prev_char = $string[$i - 1];
        }
        $this_char = $string[$i];
        if($prev_char == '_' && $this_char == '_'){

        }else{
            $final_str .= $this_char;
        }
    }
    return $final_str;
}

And it works just fine, but I wonder if I could also check this with regular expression and not another function.

I would appreciate any help.

  • 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-06-17T21:01:09+00:00Added an answer on June 17, 2026 at 9:01 pm

    Just add negative look-ahead to check whether there is double underscore in the name or not.

    /^(?!.*__)[a-z0-9_]{3,15}$/
    

    (?!pattern), called zero-width negative look-ahead, will check that it is not possible to find the pattern, ahead in the string from the “current position” (current position is the position that the regex engine is at). It is zero-width, since it doesn’t consume text in the process, as opposed to the part outside. It is negative, since the match would only continue if there is no way to match the pattern (all possibilities are exhausted).

    The pattern is .*__, so it simply means that the match will only continue if it cannot find a match for .*__, i.e no double underscore __ ahead in the string. Since the group does not consume text, you will still be at the start of the string when it starts to match the later part of the pattern [a-z0-9_]{3,15}$.

    You already allow uppercase username with strtolower, nevertheless, it is still possible to do validation with regex directly by adding case-insensitive flag i:

    /^(?!.*__)[a-z0-9_]{3,15}$/i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple file uploader for a website. The user sees a form:
I have a single user at a website who gets this error: Object reference
I am writing a PHP website. I have a $_SESSION['id'] that hold the username
I'm currently writing a website in ASP.NET MVC, and my database (which doesn't have
I'm writing a MVC website and using jqGrid. I have managed to get it
I am writing a website version of a part of our windows application. It's
i have the following problem. I'm writing a website with jQuery Mobile (the data
I'm writing up custom code for a CMS for a website. When a user
I'm re-writing a website from the ground up for azure. Each user has ownership
I am writing a password-reset page for my website. Here's my idea: a. User

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.