Jump to content

Form Validation Array Errors


richiejones24

Recommended Posts

I am trying to design a form validation script, using an array to display errors,but its not working and i am not sure where I am going wrong.

 

<?
$confcode = rand();
$pageid = "reg6";
$root = $_SERVER['DOCUMENT_ROOT'];
require("$root/include/incpost.php"); 
require("$root/include/mysqldb.php");




if ($_POST['firstname'] == NULL) { $error[] = "First Name Field Is Empty"; }
if ($_POST['lastname'] == NULL) { $error[] = "Last Name Field Is Empty"; }
if ($_POST['phonea'] == NULL) { $error[] = "Phone Number Field Is Empty"; } elseif (!is_numeric($_POST['phonea'])) { $error[] = "Phone Number Can Only Contain Numbers"; }
if ($_POST['username'] == NULL) { $error[] = "User Name Field Is Empty"; } elseif (strlen($_POST['username']) <= 4) { $error[] = "Username Is Too Short"; }
if($_POST['email1'] !== $_POST['email2']) {$error[] = "Email's Do Not Match"; } elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email1'])) {$error[] = "Email Address Is Not Valid"; }
if (strlen($_POST['password1']) <= 6) { $error[] = "Password Is To Short"; } elseif ($password1 !== $password2) { $error[] = "Passwords Do Not Mach"; }
if (preg_match('/[^a*()-z0@£"%&-9.#$-]/i', $_POST['password1'])) { $error[] = "Password Contains Invalid Charactors"; }
if (preg_match('/[^a*()-z0@£"%&-9.#$-]/i', $_POST['username'])) { $error[] = "Username Contains Invalid Charactors"; }





if ([array count] == 0)        //if no errors add to DB
{
mysql_query("INSERT INTO Reg_Profile_Private (UIN, firstname, lastname, email, phone, password, username)
VALUES ('$uin', '$firstname', '$lastname', '$email', '$phone', '$passwordenc', '$username')");
}
else {                  //else print errors
$error[] = array();
$active_keys = array();
foreach($error as $key => $myvar)
{print "$myvar </p>\n"; }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/252786-form-validation-array-errors/
Share on other sites

try

<?php
if (!isset($error))        //if no errors add to DB
{
mysql_query("INSERT INTO Reg_Profile_Private (UIN, firstname, lastname, email, phone, password, username)
VALUES ('$uin', '$firstname', '$lastname', '$email', '$phone', '$passwordenc', '$username')");
}
else {                  //else print errors
foreach($error as $key => $myvar)
{print "$myvar </p>\n"; }
}
?>

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.