Jump to content

PHP validation


fallenangel1983

Recommended Posts

My question is a simple one but i am having a hard time solving it and wondered if anyone could help.

 

I have a html page with a form on it linking directly to a php page in which i want to do some validation. i have five text boxes in which a user may enter data. these are 1. desirable feature, 2. firstname, 3. last name, 4. telephone number and 5. email address.

 

this is the code i have for the validation:

 

 

 

#####

 

    function checkDesFea($checkDesFea)

 

    { 

 

      if(!preg_match("/[^a-zA-Z]+$/ ",$checkDesFea))

 

        return TRUE;

      else return FALSE;

 

    }

 

 

  if(!checkDesFea($Feature))

 

  {

 

    $error++;

    echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Feature-----</H3></Font>";

 

  }

  echo "<h3> Desirable Feature: </h3> $Feature";

  echo "<br><h3> Contact Details: </h3>";

 

#####

 

    function checkFName($checkFName)

 

    { 

 

      if(!preg_match("/[^a-zA-Z]+$/ ",$checkFName))

 

        return TRUE;

 

      else return FALSE;

 

    }

 

 

  if(!checkFName($FName))

 

  {

 

    $error++;

    echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid First Name-----</H3></Font>";

 

  }

 

#####

 

  function checkLName($checkLName)

 

  { 

 

    if(!preg_match("/[^a-zA-Z]+$/ ",$checkLName)&&($Feauture==''))

 

      return TRUE;

 

    else

 

      return FALSE;

 

  }

 

 

 

  if(!checkLName($LName))

 

  {

 

    $error++;

    echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Last Name-----</H3></Font>";

 

  }

 

#####

 

  function checkTeNum($checkTeNum)

 

  { 

 

    if(!preg_match("/[0-9]{11}$/",$checkTeNum))

 

      return TRUE;

 

    else

 

      return FALSE;

 

  }

 

 

 

  if(!checkTeNum($TeNum))

 

  {

 

    $error++;

    echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Telephone Number-----</H3></Font>";

 

  }

 

#####

 

  function checkEmail($checkEmail)

 

  { 

 

    if(!preg_match("/^([a-z0-9._-](\+[a-z0-9])*)+@[a-z0-9.-]+\.[a-z]{2,6}$/i",$checkEmail))

      return TRUE;

    else

      return FALSE;

 

  }

 

 

 

  if(!checkEmail($Email))

 

  {

 

    $error++;

    echo "<Font color=\"red\"><H3>-----Error Encountered-----Please Enter A Valid Email-----</H3></Font>";

 

  }

 

  echo "<p><br> First Name: $FName";

  echo "<br> Last Name: $LName";

  echo "<br> Phone Number: $TeNum";

  echo "<br> Email address: $Email";

 

 

 

The code i have seems to work ok but it allows the user to leave the textfield blank. I do not want to allow thee user to leave a textfield blank. how can i get around this issue. i have tried an embedded if statement and a while loop but to no avail. Any suggestions would be appreciated or if you can think of an easier way to validate the dat let me know as the Preg_Match function seems confusing to me lol

 

thanks again

 

FallenAngel

Link to comment
Share on other sites

Hello.

 

I followed a tutorial the other day, and it uses this code to check if a field is blank:

 

<?php
  if(!$_POST['first_name']){                        // $_POST['first_name'] is the first_name field from your html form.
            $errors .= "Missing First Name\n";


// loads more code here
?>

 

Anyway theres bound to be loads of tutorials on google for php validation check some out!

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.