Jump to content

Form Validation


jassikundi

Recommended Posts

Hi people, I've just created my form and would like to know what methods can be applied for data validation before the user submits it. Its currently setup so it validates over the server and returns a message "please enter your email" for example. What would I need to do in order to have it validated before hand, use loops? If so can someone give me an example? As I'm new to PHP coding.

 

Here the basic code I have used for now.

 

if (eregi('http:', $question)) {
die ("Please enter question");
}
if(!$email == "" && (!strstr($email,"@") || !strstr($email,".")))
{
echo "<h2>Please enter valid email</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}
if(empty($firstname) || empty($email) || empty($question )) {
echo "<h2>Please fill in all fields</h2>\n";
die ("Use back! ! ");
}

 

Thanks in advance.

Link to comment
Share on other sites

You could do something like this:

 

<?php

$errors = array();

$success = false;

if($_POST) {
$user = (isset($_POST['user'])) ? $_POST['user'] : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
if(!preg_match('/^[a-zA-Z0-9_]{1,16}$/', $user)) {
	$errors[] = 'Please enter an alphanumeric (_ allowed) username 1-16 characters long.';
}
if(!preg_match('/^[a-zA-Z0-9]$/', $password)) {
	$errors[] = 'Please enter a 1-16 character alphanumeric password.';
}
if(!preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $email)) { //taken from http://www.regular-expressions.info/email.html  This doens't seem to be the best email regexp ever, but I didn't feel like googling or making one
	$errors[] = 'Please enter a valid email address.';
}
if(count($errors) === 0) {
	//success!
	$success = true;
	echo 'All input fields validated!';
}
//to use later in the form
$user = htmlentities($user);
$password = htmlentities($password);
$email = htmlentities($email);
}
else {
$user = $password = $email = '';
}

if($success != true) {
if(count($errors) !== 0) {
	//this is gonna be ugly....  Oh well.
	echo '<div style="color: red;">One or more errors occured.';
	echo '<ul>';
	foreach($errors as $error) {
		echo '<li>' . htmlentities($error) . '</li>';
	}
	echo '</ul>';
}

echo <<<HERE
<form action="" method="POST">
<table>

	<tr>
		<td>Username:</td>
		<td><input type="text" name="user" value="{$user}" maxlength="16" size="12"	/></td>
	</tr>

	<tr>
		<td>Password:</td>
		<td><input type="password" name="password" value="{$password}" maxlength="16" size="12"	/></td>
	</tr>

	<tr>
		<td>Username:</td>
		<td><input type="text" name="email" value="{$email}" maxlength="50" size="12"	/></td>
	</tr>

	<tr>
		<td colspan="2" style="text-align: center;"><input type="submit" name="submit" value="Submit!" /></td>
	</tr>

</table>
</form>
HERE;

}

?>

 

Or... to use your processing:

 

if($_POST) {
//pretend $question, $email and $firstname are defined
if(eregi('http:', $question)) {
	$error = 'Please enter question';
}
elseif(empty($email) || empty($firstname) || empty($question)) {
	$error = 'Please fill in all fields.';
}
elseif(!strstr($email, '@') || !strstr($email, '.')) {
	$error = 'Please enter a valid email.';
}
else {
	//success!!!!
	$showform = false;
}
}
else {
$question = $email = $firstname = '';
}

if($showform != false) {
if(!empty($error)) {
	echo $error;
}
echo <<<HERE
<!-- form stuff here -->
HERE;
}

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.