Hello! On my registration form I ask for a username and I only want a letters,numbers and underscores(_) in the username. How would I go about checking that? Thanks for any help!

Recommended Answers

All 7 Replies

Hey,

You can use Regex, because you didn't submit any example code, I did an example which hopefully you can follow:

<?php

    $string = "faaf2424_";

    if(preg_match('/^[a-z_\-\d]{3,}$/i', $string))
    {
        echo 'Allowed';
    }else{
        echo 'Not allowed :(';
    }
?>

This would display 'Allowed' whereas if the string was this:

$string = "@24@24@";

Then the output would be: 'Not allowed'

Hope this makes sense!

Member Avatar for LastMitch

@NardCake

Hello! On my registration form I ask for a username and I only want a letters,numbers and underscores(_) in the username. How would I go about checking that? Thanks for any help!

You meaning using Regex to do that?

Try this: ([A-Za-z0-9\-\_]+)

here is another one... :).

<?php

$string = 'abcd_23$_&';

if(!preg_match('/^[\w-]+$/',$string)){

echo 'oops! not a valid username';

}
else{

echo 'Valid Username'. $string;


}

Thanks!

@NardCake Please mark this thread as solved and give rep to those who helped you :)!

Oh yes sorry phorce, Kinda new don't quite get the general situation yet!

@NardCake - No problem :) People for years still don't follow the rules ;)! Anyway, good luck with your project!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.