Jump to content

A function for form validating


kenwvs

Recommended Posts

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
Link to comment
Share on other sites

You could do it this way:
[code]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);
    }
}[/code]
Link to comment
Share on other sites

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.

[code]<?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);[/code]

This is part of the form as well.

[code]<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>[/code]

Thanks,

Ken
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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