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

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.