Jump to content

validating username


sudhakararaog

Recommended Posts

i have used the following code to validate the username it is working fine

 

=============================================

if( $username == "" ||  !preg_match("/^[a-z0-9]+(?:_[a-z0-9]+)?$/i", $username) )

{

$error.="User name cannot be blank or has special characters";

}

=============================================

 

it does not accept UNDERSCORE at the beginning or end however while i was testing with different special characters except for # the validation works fine for all other special characters.

 

for example if i enter the user name as = abc#123

 

in this case # sign and what comes after # sign is being ignored. so in this case the username is being read as abc ONLY and not abc#123

 

this is very strange, how can i still validate # sign and tell the user that # sign is not a valid username like i have been doing with any other special characters like = !@$...........

 

please advice.

 

thanks.

 

Link to comment
Share on other sites

I am using this function for checking a valid username:

 

function Valid_username($username)

{   

    if (ereg('^[a-zA-Z0-9]+$', $username))

          return true;

    else

          return false;  

}

 

and it does take into consideration the # character. Try it out.

Link to comment
Share on other sites

Sasa.

 

function Valid_username($username)
{     
    if (ereg('^[a-zA-Z0-9]+$', $username))
           return true;
    else
           return false;         
}

 

That's the function, $username is a variable. So when you call the function, you replace $username with whatever.

 

So you'd have that above your code. Then say a user submitted a username (e.g $_POST[username]); you could then do...

<?php
    if(Valid_username({$_POST['username']})) {
        // Valid_username has returned true, so the username is valid
        echo "Wahay! The username is valid!";
    }else{
        // Valid_username has returned false, so the username isn't valid
        echo "That username is not valid, sorry!";
    }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.