Jump to content

Recommended Posts

I own http://imshare.us.to  And I need help adding validation to the registration form. I'm willing to pay if need be. But if it's simple, I would appreciate the help. So if someone registers without a e-mail, password, etc... Make it reject, and tell the user what went wrong.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/122123-adding-validation-to-a-form/
Share on other sites

Well, there are many methods to do a validation system. Personally, I use custom class for that, but we can make a simple validation system without one:

 

Suppose you have the following form:

 

<form method="post" action="">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="pass" /><br />
Retype Password: <input type="password" name="pass2" /><br />
E-Mail: <input type="text" name="email" />
</form>

 

Now you have this PHP Script:

 

<?php

$username = $_POST['username'];
$pass = $_POST['pass'];
$pass2 = $_POST['pass2'];
$email = $_POST['email'];

if(strlen($username) < 3){ // If the username has less than 3 characters, print an error message and leave the script after that.
echo "Invalid username. It must have 3 characters or more.";
return 0;
}

if(strlen($pass) < 6){ // If the password has less than 6 characters, print a error message and leave the script.
echo "Your password must have 6 characters or more.";
return 0;
}

if($pass != $pass2){
echo "The passwords did not match. Please try again.";
return 0;
}

if(strlen($email) < 3){ // If the E-Mail has less than 3 scripts, print a message and leave the script.
return 0;
}

if(!preg_replace("/^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email){ // If the E-Mail does not have a "@" or dot at the end, print a message and leave the script.
echo "Invalid E-Mail";
return 0;

//If there were no errors, you can put all your database stuff here.

?>

 

Obviously, this was made quickly and it is not reliable. With that script I don't even check for user input so SQL injection is something very easy to do. So probably you can fix the script and make it better. There are also many things I don't like from this script, so I would post my method, but it only works well for my custom forum. I'm sure you can modifty that though.

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.