0

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!

4
Contributors
7
Replies
8
Views
4 Years
Discussion Span
Last Post by phorce
1

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!

4

@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\-\_]+)

2

here is another one... :).

<?php

$string = 'abcd_23$_&';

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

echo 'oops! not a valid username';

}
else{

echo 'Valid Username'. $string;


}
0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.