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
https://forums.phpfreaks.com/topic/33861-i-need-help-with-form-validation/
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]
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?
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.