Jump to content

[SOLVED] Regular Expressions


chriskiely

Recommended Posts

Hello, I am writing a form validation script in php and have got the whole thing working except for checking to see if the username supplied by the user is in the correct format (i.e. contains only letters, numbers and the underscore character). This the code I have come up with but it always produces the error message. I have to say I don't really understand the syntax for creating regular expressions so I am assuming this is where I am going wrong. ($user_name is passed from the form)

 

$user_name=stripslashes($user_name);

$user_name=trim($user_name);

$user_name=strtolower($user_name);

 

if(!eregi("^[A-Za-z0-9\_$", $user_name)){

    $errmsg.='Invalid user name, please try again.';

}

 

Any help would be really appreciated as this is driving me mental!!

Thanks, Chris

Link to comment
Share on other sites

ah yes sorry, but still trying it like "[^A-Za-z0-9\_$]" and "^[A-Za-z0-9\_]$" doesn't work either... i am testing it with words such as "chris" "chris1" "chris_1" etc, all of them fail the test and produce the error message....

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.