How to allow in login only 0-9, a-z, A-Z, _ signs ONLY?

Posted on 2009-07-14
Last Modified: 2013-12-13
as topic (php)
Question by:AndyPSV
LVL 13

Expert Comment

ID: 24852457
if( preg_match('/^\w+$/',  trim($user_name) ) ){
     // process

Accepted Solution

xBellox earned 500 total points
ID: 24852465
This should work:

if( preg_match( "/^[A-Za-z0-9_]+$/", $login ) ) {

    echo "Login ok";

} else {

    echo "Bad login";


Open in new window


Author Closing Comment

ID: 31603400

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript Hashmap output 8 46
How to loop through records  using SELET COUNT 2 14
PHP: concatenate query 12 38
mysql between clause 2 0
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now