Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Form Validation | Help with some code

Options
  • 12-12-2005 4:06pm
    #1
    Registered Users Posts: 3,514 ✭✭✭


    Hi guys, i'm fairly new to php so excuse my naviety. I have been following a tutorial on creating a user management system but it doesn't have the best form checking. The only thing form checking it uses is to verify if the username/email address are already in the database. I would like to modify the code so that it only allows the user pick a username that contains english characters (no numbers or characters) and that the first name and last name can't contain characters or numbers either.

    This is the code that is currently in place....
    /* Let's do some checking and ensure that the user's email address or username
     does not exist in the database */
     
     $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
     $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
     
     $email_check = mysql_num_rows($sql_email_check);
     $username_check = mysql_num_rows($sql_username_check);
     
     if(($email_check > 0) || ($username_check > 0)){
     	echo "Please fix the following errors: <br />";
     	if($email_check > 0){
     		echo "<strong>Your email address has already been used by another member in our database. Please submit a different Email address!<br />";
     		unset($email_address);
     	}
     	if($username_check > 0){
     		echo "The username you have selected has already been used by another member in our database. Please choose a different Username!<br />";
     		unset($username);
     	}
     	include 'join_form.html'; // Show the form again!
     	exit();  // exit the script so that we do not create this account!
     }
    

    Thanks for any forthcoming help. I'm sure its a simple enough thing to solve and it will probably be very logical for me to follow but i have no previous php experience so i'm at a bit of a loss at the moment.

    I was told that the function ctype_alpha($username) would need to be used, but i'm not sure how to place it into the code that i already have.


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Your best best is to have a series of ifs - each if representing another check so you have...

    [php]
    if(($email_check > 0) || ($username_check > 0)){
    ...............
    } elseif($username != "" || $email_address != "" || !ctype_alpha($username) || !ctype_alpha($email_address)) {
    //Failure of alpha test, do whatever
    } else {
    //Both tests passed, continue.
    }[/php]

    It's not *exactly* the way I'd do it, but it fits neatly into what you already have.


  • Registered Users Posts: 3,514 ✭✭✭Rollo Tamasi


    thanks for the response seamus. I've placed your code within my code and i have received the following error
    Fatal error: Call to undefined function: ctype_alpha() in c:\ibserver\www\db\register.php on line 48

    Do i need to declare the ctype_alpha function within the code. I have looked it up and i'm still a bit lost.

    Here is the code i am using now :
    [PHP]if((!$first_name) || (!$last_name) || (!$email_address) || (!$username) || (!$username)){
    echo 'You did not submit the following required information! <br />';
    if(!$first_name){
    echo "First Name is a required field. Please enter it below.<br />";
    }
    if(!$last_name){
    echo "Last Name is a required field. Please enter it below.<br />";
    }
    if(!$email_address){
    echo "Email Address is a required field. Please enter it below.<br />";
    }
    if(!$username){
    echo "Desired Username is a required field. Please enter it below.<br />";
    }

    if(($email_check > 0) || ($username_check > 0)){
    echo "error.<br />";
    }
    elseif($username != "" || $email_address != "" || !ctype_alpha($username) || !ctype_alpha($email_address)) {
    echo "error 2.<br />";
    }

    include 'join_form.html';
    exit();
    }[/PHP]


  • Subscribers Posts: 9,716 ✭✭✭CuLT


    What version of php are you running under?

    If it's any lower than 4.0.4 then you should update fairly sharply (ctype_alpha was implemented in 4.0.4).


  • Registered Users Posts: 3,514 ✭✭✭Rollo Tamasi


    its 4.2.3. I don't have a clue why thats happening, but i got around it anyway.


Advertisement