Link to home
Start Free TrialLog in
Avatar of Luv2Muff
Luv2Muff

asked on

Form validation

Hi,

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.

<?php
// 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/mailer.inc.php");
    } 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">
Avatar of star_trek
star_trek

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.';
Avatar of Luv2Muff

ASKER

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\subscribe.inc.php on line 19

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of star_trek
star_trek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Luv.