Jump to content

I need Help with Form Validation


Woad.php

Recommended Posts

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?
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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]
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.