Jump to content

[SOLVED] Form Validation?


TheJoey

Recommended Posts

Hey i need help with this i wrote this out

 

<?php
$displayEmailError = 'Email Must be Valid';
$displayAgeError = 'Age must be Valid';
$displayFirstError = 'First Name cant be Empty';
$displayLastError = 'Last Name cant be Empty';
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) $displayEmailError = true;
if (($_POST['Age']) <18 & > 67) $displayAgeError = true;
if (empty($_POST['FirstName']) $displayFirstError = true;
if (empty($_POST['LastName']) $displayLastError = true;
if ($displayEmailError, $displayAgeError, $displayFirstError, $displayLastError) = false
{save to database}

 

but im getting syntax's alover the joint i need a litle help

 

Parse error: syntax error, unexpected '>'

Try:

<?php
$displayEmailError = 'Email Must be Valid';
$displayAgeError = 'Age must be Valid';
$displayFirstError = 'First Name cant be Empty';
$displayLastError = 'Last Name cant be Empty';
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
{
	$displayEmailError = true;
}

if ($_POST['Age'] < 18 OR $_POST['Age'] > 67)
{
	$displayAgeError = true;
}

if (empty($_POST['FirstName']))
{
	$displayFirstError = true;
}	

if (empty($_POST['LastName']))
{
	$displayLastError = true;
}

if (!$displayEmailError && !$displayAgeError && !$displayFirstError && !$displayLastError)
{
	save to database
}
?>

No errors, but nothing seems to be happening.

$displayEmailError = 'Email Must be Valid';
$displayAgeError = 'Age must be Valid';
$displayFirstError = 'First Name cant be Empty';
$displayLastError = 'Last Name cant be Empty';

if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
{
$displayEmailError = true;
}

if ($_POST['Age'] < 18 OR $_POST['Age'] > 67)
{
$displayAgeError = true;
}

if (empty($_POST['FirstName']))
{
$displayFirstError = true;
}

if (empty($_POST['LastName']))
{
$displayLastError = true;
}


if (!$displayEmailError && !$displayAgeError && !$displayFirstError && !$displayLastError)
{

Actually, if you use an array to hold the errors and set the index to be the type of error and set the value to be the error message, it makes both checking if there are no errors and displaying a specific error easier at the proper place when the form is redisplayed.

 

$errors = array(); // define an empty array at the start
....
$errors['EmailError'] = $displayEmailError; // set an error in the array
...

// to check if there are no errors -
if(empty($errors)){
    // no errors, process the data
}

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.