chriskiely Posted May 8, 2007 Share Posted May 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
taith Posted May 8, 2007 Share Posted May 8, 2007 your missing an ] Quote Link to comment Share on other sites More sharing options...
chriskiely Posted May 8, 2007 Author Share Posted May 8, 2007 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.... Quote Link to comment Share on other sites More sharing options...
taith Posted May 8, 2007 Share Posted May 8, 2007 ^[A-Z][a-z][0-9][\_]$ ^ <-- start of string $ <-- end of string [] <-- conditionals Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 8, 2007 Share Posted May 8, 2007 use ^[A-Za-z0-9\_]+$ as "chris" "chris1" are more than one char Quote Link to comment Share on other sites More sharing options...
chriskiely Posted May 8, 2007 Author Share Posted May 8, 2007 ah i was missing the plus... thanks very much indeed for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.