Woad.php Posted January 12, 2007 Share Posted January 12, 2007 I'm trying to create a members system and i'm having problems with the form validation. I want to have the validation code in a seperate file and be able to check if the username (one of the form's fields) has been taken by querying the database and comparing names. How do I do this? Quote Link to comment Share on other sites More sharing options...
jwk811 Posted January 12, 2007 Share Posted January 12, 2007 to see if the username is taken you will need to select that name from the row and find out how many rows had that information.. if it returns 0 then its not taken if more than 0 it is taken.. heres an example..[code]$sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'"); $username_check = mysql_num_rows($sql_username_check); if($username_check > 0){ echo "Username is already being Used! Please submit another email address!"; } [/code] Quote Link to comment Share on other sites More sharing options...
Woad.php Posted January 12, 2007 Author Share Posted January 12, 2007 OK, thanks that's better than what I was thinking of doing to check the username. But that still doesn't answer my validation issues. Quote Link to comment Share on other sites More sharing options...
jwk811 Posted January 12, 2007 Share Posted January 12, 2007 i dont understand what your trying to do.. can you explain it better? Quote Link to comment Share on other sites More sharing options...
Woad.php Posted January 12, 2007 Author Share Posted January 12, 2007 I have a form on my webpage. Let's say I have three fields. Username, Email, and password. For all fields I have to check if they're too short or have invalid characters. For Username I need to check if it's been taken by another user, and for email I need to check whether it's a valid email. How do I perform these checks? Quote Link to comment Share on other sites More sharing options...
jwk811 Posted January 12, 2007 Share Posted January 12, 2007 for the length you can do this:[code] if(strlen($username) > 12 || strlen($username) < 4 ){ echo "Username should have 4 - 12 characters only"; if(strlen($username) > 12 ){ echo "username is too long"; } if(strlen($username) < 4 ){ echo "username is too short"; }[/code]and heres an email thing i already have written up[code]if ($email_address) { if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address)) { echo "The email you entered is not valid. Please try again. <br/>"; exit(); }} [/code] Quote Link to comment Share on other sites More sharing options...
Woad.php Posted January 12, 2007 Author Share Posted January 12, 2007 But I do I validate the forms using that code? 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.