Form validation

Posted on 2006-04-28
Last Modified: 2010-05-18

I currently validate my form with the following code, it is very limited as it only checks that each field has something in it. My attempt to perform a basic email validation also does not work and I have been forced to comment it out.

I would appreciate any example code that extends the current validation ie no more than 20 chars, numbers only, upper lower case and spaces only etc etc.

Many thanks in advance.

// Create an empty array to hold the error messages.
$subscribe_arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['subscribe_Submit'])) {
    // Each time there's an error, add an error message to the error array
    // using the field name as the key.
    if ($_POST['subscribe_name']=='')
        $subscribe_arrErrors['subscribe_name'] = 'Please enter your name.';
    if ($_POST['subscribe_job_title']=='')
        $subscribe_arrErrors['subscribe_job_title'] = 'Please enter your Job Title.';
    if ($_POST['subscribe_company']=='')
        $subscribe_arrErrors['subscribe_company'] = 'Please enter Company Name.';      
    if ($_POST['subscribe_address']=='')
        $subscribe_arrErrors['subscribe_address'] = 'Please enter your address.';                  
    if ($_POST['subscribe_email']=='')
        $subscribe_arrErrors['subscribe_email'] = 'Please enter an email address.';
//    if (!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $_POST['subscribe_email'])
//        $subscribe_arrErrors['subscribe_email'] = 'Please enter a valid email.';
    if ($_POST['subscribe_web']=='')
        $subscribe_arrErrors['subscribe_web'] = 'Please enter www address.';
    if (count($subscribe_arrErrors) == 0) {
        // If the error array is empty, there were no errors.
        // Insert form processing here.
      $subscribe_validated = '<p></p><div class="formerror"><p>Thank you, the form has been submitted</p></div>';
      include ("includes/email/");
    } else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $subscribe_strError = '<div class="formerror"><img src="/images/triangle_error.gif" alt="" class="img_error"><span class="errortext">Please correct the following:</span><ul>';
        // Get each error and add it to the error string
        // as a list item.
        foreach ($subscribe_arrErrors as $subscribe_error) {
            $subscribe_strError .= "<li><span class='errortext'>- </span>$subscribe_error</li>";
        $subscribe_strError .= '</ul></div>';
       <div id="subscribe">
         <div class="form">
Question by:Luv2Muff
    LVL 11

    Expert Comment

    try the following
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['subscribe_email'])
           $subscribe_arrErrors['subscribe_email'] = 'Please enter a valid email.';

    Author Comment

    Thank star trek,

    Have copied and pasted - then get the following error:

    Parse error: parse error, unexpected T_VARIABLE in D:\my_domain\html\includes\ on line 19

    Any ideas?
    LVL 11

    Accepted Solution

    oops missed a ) try this
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['subscribe_email']))
         $subscribe_arrErrors['subscribe_email'] = 'Please enter a valid email.';

    Author Comment

    Thanks a million it works a treat.

    I will close this to give you the points and repost for further help on ivcreasing the validation in other areas.

    Thanks again.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now