Return to Snippet

Revision: 14991
at October 11, 2011 19:13 by brownrl


Updated Code
<?php


//  Quick function to loop through regexs and compare to what is in _POST
//  
//  $regs    ->   associative array of regular expressions
//  $ferrors ->   error messages to display to the users asociative array

function validatePost( $regs , $ferrors )
{
	$errors = array();
	foreach( $regs as $k => $v )
	{
		if( ! preg_match( $v , $_POST[$k] ) )
		{
			$errors[$k] = $ferrors[$k];
		}
	}
	return $errors;
}


// has the post been submitted?
if( count( $_POST ) )
{
	// yes it has been submitted so lets validate
	$regs['last_name']  = "/^[[:alpha:]\ -]+$/";  // require a alpha
	$regs['first_name'] = "/^[[:alpha:]\ -]+$/";  // require a alpha
	$regs['email']      = "/^..*\@..*$/";         // VERY simple email check
	                                            // Use google to find better
												

	// Ok here are the error message to display when it is bad
	$ferrors['last_name']  = "Last name required";
	$ferrors['first_name'] = "First name required";
	$ferrors['email']      = "Email name required";

	$errors = validatePost( $regs , $ferrors );

	// Do we have errors?
	if( count( $errors ) == 0 )
	{
		//  WE HAVE NO ERRORS DO SOMETHING
		//  PUT IT INTO THE DATABASE, EMAIL, BOUNCE THE USER
		//  TO A THANK YOU PAGE, ETC...
	}
}
?>



<!-- OK WE ARE IN HTML -->
<!-- LETS MAKE THE FORM AND NOW YOU SEE HOW SIMPLE THIS IS I HOPE -->

<form method="POST">

<p>
<label>Last Name</label>
<input type="text" name="last_name" value="<?= $_POST['last_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['last_name'] ?></span>
</p>


<p>
<label>First Name</label>
<input type="text" name="first_name" value="<?= $_POST['first_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['first_name'] ?></span>
</p>


<p>
<label>Email</label>
<input type="text" name="email" value="<?= $_POST['email'] ?>" />
<span style="color: #FF0000;"><?= $errors['email'] ?></span>
</p>


<p>
<input type="submit" name="subby" value="GO" />
</p>


</form>

Revision: 14990
at June 19, 2009 05:56 by brownrl


Updated Code
<?php


//  Quick function to loop through regexs and compare to what is in _POST
//  
//  $regs    ->   associative array of regular expressions
//  $ferrors ->   error messages to display to the users asociative array

function validatePost( $regs , $ferrors )
{
	$errors = array();
	foreach( $regs as $k => $v )
	{
		if( ! ereg( $v , $_POST[$k] ) )
		{
			$errors[$k] = $ferrors[$k];
		}
	}
	return $errors;
}


// has the post been submitted?
if( count( $_POST ) )
{
	// yes it has been submitted so lets validate
	$regs['last_name']  = "^[[:alpha:]\ -]+$";  // require a alpha
	$regs['first_name'] = "^[[:alpha:]\ -]+$";  // require a alpha
	$regs['email']      = "^..*\@..*$";         // VERY simple email check
	                                            // Use google to find better
												

	// Ok here are the error message to display when it is bad
	$ferrors['last_name']  = "Last name required";
	$ferrors['first_name'] = "First name required";
	$ferrors['email']      = "Email name required";

	$errors = validatePost( $regs , $ferrors );

	// Do we have errors?
	if( count( $errors ) == 0 )
	{
		//  WE HAVE NO ERRORS DO SOMETHING
		//  PUT IT INTO THE DATABASE, EMAIL, BOUNCE THE USER
		//  TO A THANK YOU PAGE, ETC...
	}
}
?>



<!-- OK WE ARE IN HTML -->
<!-- LETS MAKE THE FORM AND NOW YOU SEE HOW SIMPLE THIS IS I HOPE -->

<form method="POST">

<p>
<label>Last Name</label>
<input type="text" name="last_name" value="<?= $_POST['last_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['last_name'] ?></span>
</p>


<p>
<label>First Name</label>
<input type="text" name="first_name" value="<?= $_POST['first_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['first_name'] ?></span>
</p>


<p>
<label>Email</label>
<input type="text" name="email" value="<?= $_POST['email'] ?>" />
<span style="color: #FF0000;"><?= $errors['email'] ?></span>
</p>


<p>
<input type="submit" name="subby" value="GO" />
</p>


</form>

Revision: 14989
at June 19, 2009 05:38 by brownrl


Updated Code
<?php


//  Quick function to loop through regexs and compare to what is in _POST
//  
//  $regs    ->   associative array of regular expressions
//  #ferrors ->   error messages to display to the users asociative array

function validatePost( $regs , $ferrors )
{
	$errors = array();
	foreach( $regs as $k => $v )
	{
		if( ! ereg( $v , $_POST[$k] ) )
		{
			$errors[$k] = $ferrors[$k];
		}
	}
	return $errors;
}


// has the post been submitted?
if( count( $_POST ) )
{
	// yes it has been submitted so lets validate
	$regs['last_name']  = "^[[:alpha:]\ -]+$";  // require a alpha
	$regs['first_name'] = "^[[:alpha:]\ -]+$";  // require a alpha
	$regs['email']      = "^..*\@..*$";         // VERY simple email check
	                                            // Use google to find better
												

	// Ok here are the error message to display when it is bad
	$ferrors['last_name']  = "Last name required";
	$ferrors['first_name'] = "First name required";
	$ferrors['email']      = "Email name required";

	$errors = validatePost( $regs , $ferrors );

	// Do we have errors?
	if( count( $errors ) == 0 )
	{
		//  WE HAVE NO ERRORS DO SOMETHING
		//  PUT IT INTO THE DATABASE, EMAIL, BOUNCE THE USER
		//  TO A THANK YOU PAGE, ETC...
	}
}
?>



<!-- OK WE ARE IN HTML -->
<!-- LETS MAKE THE FORM AND NOW YOU SEE HOW SIMPLE THIS IS I HOPE -->

<form method="POST">

<p>
<label>Last Name</label>
<input type="text" name="last_name" value="<?= $_POST['last_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['last_name'] ?></span>
</p>


<p>
<label>First Name</label>
<input type="text" name="first_name" value="<?= $_POST['first_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['first_name'] ?></span>
</p>


<p>
<label>Email</label>
<input type="text" name="email" value="<?= $_POST['email'] ?>" />
<span style="color: #FF0000;"><?= $errors['email'] ?></span>
</p>


<p>
<input type="submit" name="subby" value="GO" />
</p>


</form>

Revision: 14988
at June 19, 2009 05:30 by brownrl


Updated Code
<?php


//  Quick function to loop through regexs and compare to what is in _POST
//  
//  $regs    ->   associative array of regular expressions
//  #ferrors ->   error messages to display to the users asociative array

function validatePost( $regs , $ferrors )
{
     $errors = array();
     foreach( $regs as $k => $v )
     {
          if( ! ereg( $v , $_POST[$k] ) )
          {
               $errors[$k] = $ferrors[$k];
          }
     }
     return $errors;
}



// has the post been submitted?
if( count( $_POST ) )
{

     // yes it has so lets validate
     $regs['last_name'] = "^[alpha]+$";    // require a alpha
     $regs['first_name'] = "^[alpha]+$";   // require a alpha
     $regs['email'] = "^..*\@..*$";        // VERY simple email check

     $ferrors['last_name']  = "Last name required";
     $ferrors['first_name'] = "First name required";
     $ferrors['email']      = "Email name required";

     $errors = validatePost( $regs , $errors );


     // Do we have errors
     if( count( $errors ) == 0 )
     {
          //  WE HAVE NO ERRORS DO SOMETHING
          //  PUT IT INTO THE DATABASE, EMAIL, BOUNCE THE USER
          //  TO A THANK YOU PAGE, ETC...
     }

}
?>



<!-- OK WE ARE IN HTML -->
<!-- LETS MAKE THE FORM AND NOW YOU SEE HOW SIMPLE THIS IS I HOPE -->

<form method="POST">

<p>
<label>Last Name</label>
<input type="text" name="last_name" value="<?= $_POST['last_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['last_name'] ?></span>
</p>


<p>
<label>First Name</label>
<input type="text" name="first_name" value="<?= $_POST['first_name'] ?>" />
<span style="color: #FF0000;"><?= $errors['first_name'] ?></span>
</p>


<p>
<label>Email</label>
<input type="text" name="email" value="<?= $_POST['email'] ?>" />
<span style="color: #FF0000;"><?= $errors['email'] ?></span>
</p>


</form>

Revision: 14987
at June 19, 2009 05:16 by brownrl


Initial Code
<?php

function validatePost( $regs , $ferrors )
{
     $errors = array();
     foreach( $regs as $k => $v )
     {
          if( ! ereg( $v , $_POST[$k] ) )
          {
               $errors[$k] = $ferrors[$k];
          }
     }
     return $errors;
}

?>

Initial URL
http://www.goingson.be/2009/05/my-super-awesome-php-functions-part-v.html

Initial Description
This is a good way to validate form input from PHP, in essence this could be a blue print for your forms.

Initial Title
PHP Form Validation

Initial Tags
form, php, validation

Initial Language
PHP