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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:45:38+00:00 2026-05-27T16:45:38+00:00

How to validate a username using php My rules are: Username can only contain

  • 0

How to validate a username using php

My rules are:

Username can only contain letters, numbers and 1 underscore.

This is my current preg_match

preg_match('/^[a-zA-Z0-9][_]{1}[a-zA-Z0-9]+$/',$_POST['uname'])
  • 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-27T16:45:38+00:00Added an answer on May 27, 2026 at 4:45 pm

    This should do it:

    /^[a-zA-Z0-9]*_?[a-zA-Z0-9]*$/
    

    Keep in mind that a character class of a single item is redundant. Write the atom solely instead. And {1} is another seriously redundant quantifier.

    /[_]{1}/
    

    is just the same as

    /_/
    

    Edit:

    Based on the new constrains and in the helpful comment by chris, this seems to be a better regex:

    /^[a-zA-Z0-9]+_?[a-zA-Z0-9]+$/D
    

    It matches usernames of at least two character, not ending or beginning with the optional underscore, and no newlines at the end.

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

Sidebar

Related Questions

How do I validate a username using PHP? On my registration page I need
I am using a PHP login script that challenges user for username & password.
In PHP, how can you bind using a property other than the default cn?
I am using jquery.validate plugin for checking username availability if user type sample i
This is using jQuery 1.6.1 and Validate 1.8.1. I have been banging my head
I'm trying to validate a php register form using valid8 jquery plugin. i need
Having trouble getting a form using PHP, JQuery Form plugin, and JQuery Validate plugin
I have created a form and have validated everything using PHP, but can't figure
The following script works fine: $(#regform).validate().showErrors({username:message}); After I changed the script to the below
I want to validate a set of credentials against the domain controller. e.g.: Username:

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.