Jump to content


Photo

A function for form validating


  • Please log in to reply
7 replies to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 12 July 2006 - 05:01 AM

I am trying to write a form validating script and have most of the functions figured out. I have a couple where I want the opportunity to leave it blank, but, if they choose to fill it in, I want it to meet certain requirements.

Example. The field can be left blank, but, if they put anything in the field, it must be in the form of a email address. (I know how the format for an email is

function checkEmail($email) { //Check Email Format
$pattern = "/^[A-z0-9\._-]+"
. "@"
. "[A-z0-9][A-z0-9-]*"
. "(\.[A-z0-9_-]+)*"
. "\.([A-z]{2,6})$/";
return preg_match ($pattern, $email);

but how do I add that it can be blank also.

The same thing except it would be in a dollar value, but could be blank.

Thanks,

Ken

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 12 July 2006 - 07:41 AM

You could do it this way:
function checkEmail($email) { //Check Email Format
    if(empty($email)) {
        return true;
    } else {
        $pattern = "/^[A-z0-9\._-]+"
        . "@"
        . "[A-z0-9][A-z0-9-]*"
        . "(\.[A-z0-9_-]+)*"
        . "\.([A-z]{2,6})$/";
        return preg_match ($pattern, $email);
    }
}


#3 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 12 July 2006 - 07:53 AM

Thanks for the help.  I can see how that will work, and will give it a try.

The demo that I followed through had some instructions, but for a newbie like me, I am getting more and more confused by the minute.  I am able to follow how writing a separate functions script will save time in the long run as I can reuse those functions for other purposes, but am not able to see how by listing the following on the same page as the form, it is able to know when to run, and what to look for, as their are no titles?  Can you also please explain what isset means and how it works.

<?php
/* validation.php */

require_once ('functions.php');

$valid = TRUE;

if (isset ($_POST['submit'])) {
    foreach($_POST as $key=>$value) {
      $$key = $value;
    }
    $valid = $user = LettersAndDigits($id);
    $title = $Variable = ($item_title);
    $valid = $valid && $title;
    $category = $Variable = ($item_category);

This is part of the form as well.

<body bgcolor="#F5F3F5">
<p align="center"><b><font face="Arial" size="3" color="#0000FF">Quick Lister</font></b></p>
<p align="center"><b><font face="Arial" color="#FF0000">For Sale 4 U - Canada's
Online Auction</font></b></p>
<p align="center"><font face="Arial" size="2"><b>Please note:&nbsp; </b>You may
not use characters such as $, #, &quot;, &amp;, *,/, etc.&nbsp; It is best to use normal
text and numerical characters only.&nbsp;
</p><hr>

<form method="POST" action="validation.php">
        <font size="2">
        User ID </font> <font size="2">
        &nbsp;<font face="Arial"><input type="text" name="id" size="12" value="<?= $id ?>"</font>&nbsp;&nbsp;
        <font size="2">You must use the <font color="#FF0000">same User ID</font> for all items you are
        uploading during this session.</font> <font size="2">(It does not have to be
        your regular username.)<BR> </font>
        <font face="Arial"><font size="2">Item Title </font> <font size="2">
        &nbsp;<input type="text" name="item_title" size="68" value="<?= $item_title ?>"</font>  <font size="2">&nbsp;
        Item Category: </font><select size="1" name="item_category" value="<?= $item_category ?>">
        <option value="000">Please Select Category</option>
        <option value="150">Antiques</option>
        <option value="18">Automobiles - Cars</option>
        <option value="75">Home and Garden - Lawnmowers</option>
        <option value="76">Home and Garden - Rototillers</option>

Thanks,

Ken

#4 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 12 July 2006 - 08:04 AM

if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $emailfield))

that is what i use to check for an appropiate email address

#5 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 12 July 2006 - 08:41 AM

isset() is used to check if a variable has been set/initialised. It is used in the example you have given where both your form and its validation are in the same script. if you have a button with the name "submit" and run if(isset($_POST['submit'])), this checks if the button has been pressed, if so, it then goes on to validate the submitted fields.

#6 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 12 July 2006 - 09:51 AM

Thank You for the explanation.  It is starting to make a bit more sense to me know.  I don't have it working yet, but I think I am getting closer (atleast I hope so) and I am starting to understand what some of the commands do, and how they work.

If I need to build a function that relates to time, would it be best to have it validate that it allows numbers and the colon, or is there a way to validate to actual time?

Ken

#7 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 12 July 2006 - 10:08 AM

What sort of time are you trying to validate? The function would depend on what the input is acually supposed to be.

#8 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 12 July 2006 - 04:10 PM

I need to validate that the time is a valid time.  It is the time an auction ends.  It would always be on the hour (2:00 A.M. or 4:00 P.M.)

Also, I am sure there must be an if variable.  Example:  If field A is #1, then you must have a valid answer in this field.

Example:  If the auction_type is Auction, then you must have a valid answer in this box.  If the auction_type is Fixed Price, then this box may be empty.

Thanks,

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users