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['subs cribe_name '] = 'Please enter your name.';
if ($_POST['subscribe_job_tit le']=='')
$subscribe_arrErrors['subs cribe_job_ title'] = 'Please enter your Job Title.';
if ($_POST['subscribe_company ']=='')
$subscribe_arrErrors['subs cribe_comp any'] = 'Please enter Company Name.';
if ($_POST['subscribe_address ']=='')
$subscribe_arrErrors['subs cribe_addr ess'] = 'Please enter your address.';
if ($_POST['subscribe_email'] =='')
$subscribe_arrErrors['subs cribe_emai l'] = '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['subs cribe_emai l'] = 'Please enter a valid email.';
if ($_POST['subscribe_web']== '')
$subscribe_arrErrors['subs cribe_web' ] = 'Please enter www address.';
if (count($subscribe_arrError s) == 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.in c.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_erro r.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</l i>";
}
$subscribe_strError .= '</ul></div>';
}
}
?>
<div id="subscribe">
<div class="form">
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_
// 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['subs
if ($_POST['subscribe_job_tit
$subscribe_arrErrors['subs
if ($_POST['subscribe_company
$subscribe_arrErrors['subs
if ($_POST['subscribe_address
$subscribe_arrErrors['subs
if ($_POST['subscribe_email']
$subscribe_arrErrors['subs
// if (!ereg("[0-9a-z]([-_.]?[0-
// $subscribe_arrErrors['subs
if ($_POST['subscribe_web']==
$subscribe_arrErrors['subs
if (count($subscribe_arrError
// If the error array is empty, there were no errors.
// Insert form processing here.
$subscribe_validated = '<p></p><div class="formerror"><p>Thank
include ("includes/email/mailer.in
} 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_erro
// 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</l
}
$subscribe_strError .= '</ul></div>';
}
}
?>
<div id="subscribe">
<div class="form">
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?
Have copied and pasted - then get the following error:
Parse error: parse error, unexpected T_VARIABLE in D:\my_domain\html\includes
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
I will close this to give you the points and repost for further help on ivcreasing the validation in other areas.
Thanks again.
Luv.
if(!eregi("^[_a-z0-9-]+(\.
$subscribe_arrErrors['subs