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 '>'

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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)
{

Link to comment
Share on other sites

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
}

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.