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 6083477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:24:27+00:00 2026-05-23T11:24:27+00:00

I am writing a PHP validation for a user registration form. I have a

  • 0

I am writing a PHP validation for a user registration form. I have a function set up to validate a username which uses perl-compatible regular expressions. How can I edit it so that one of the requirements of the regular expression are AT MOST a single . or _ character, but NOT allow that character at the beginning or end of the string? So for example, things like “abc.d”, “nicholas_smith”, and “20z.e” would be valid, but things like “abcd.”, “a_b.C”, and “_nicholassmith” would all be invalid.

This is what I currently have but it does not add in the requirements about . and _ characters.

function isUsernameValid()
{
    if(preg_match("/^[A-Za-z0-9_\.]*(?=.{5,20}).*$/", $this->username))
    {
        return true; //Username is valid format
    }
    return false;
}

Thank you for any help you may bring.

  • 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-23T11:24:28+00:00Added an answer on May 23, 2026 at 11:24 am
    if (preg_match("/^[a-zA-Z0-9]+[._]?[a-zA-Z0-9]+$/", $this->username)) {
        // there is at most one . or _, and it's not at the beginning or end
    }
    

    You can combine this with the string length check:

    function isUsernameValid() {
        $length = strlen($this->username);
        if (5 <= $length && $length <= 20
        &&  preg_match("/^[a-zA-Z0-9]+[._]?[a-zA-Z0-9]+$/", $this->username)) {
            return true;
        }
        return false;
    }
    

    You could probably do the whole lot using just one regex, but it would be much harder to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP form that needs some very simple validation on submit. I'd
Hi I'm writing and app in PHP. I want to use jquery form validation
I am writing a validation class in PHP which I would like to be
I am writing php client for .Net web service which uses ws.* specification. In
I've been writing PHP for about six years now and have got to a
I've been writing PHP web applications for some time, and have come across very
Often when writing PHP I'll have it output some HTML like this - echo
I am writing some PHP code where I end it with a email validation
I have lots of experience of writing php scripts that are run in the
I'm writing a php forms class with client and server side validation. I'm having

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.