View Full Version : form validation
sarah
01-15-2003, 10:54 AM
Hi,
I'm stuck and lost on how to vaildate my form. My form has 5 fields which all have to be populated. I know how to check if field is blank echo out an error message, but i dont know how to check the email field to make sure that there is an @ sign in the field. I dont want to use formmail.php because then I cant use my customized error messages for the form.
Any ideas?
below is my code for my form (if it helps):
<form action="form.php" method="POST">
<input type="hidden" name="submitted" value="validate">
<input type="hidden" name="mailsubj" value="Form from Website">
<input type="text" name="name" size="40" value="<?php echo $name; ?>">
<input type="text" name="address1" size="40" value="<?php echo $address1; ?>">
<input type="text" name="city" size="30" value="<?php echo $city; ?>">
<input type="text" name="postcode" size="30" value="<?php echo $postcode; ?>">
<input type="text" size="40" name="email" value="<?php echo $email; ?>">
if ($name == "") {
$flgError=TRUE;
$strGenError.= "<li>Please enter your name.</li>\n";}
if ($address1 == "") {
$flgError=TRUE;
$strGenError.= "<li>Please enter your address.</li>\n";}
if ($city == "") {
$flgError=TRUE;
$strGenError.= "<li>Please enter your city / town.</li>\n";}
if ($country == "") {
$flgError=TRUE;
$strGenError.= "<li>Please enter your country.</li>\n";}
if ($postcode == "") {
$flgError=TRUE;
$strGenError.= "<li>Please enter your post / zip code.</li>\n";}
if ($email == "") {
$flgError=TRUE;
$strGenError.= "<li>Please enter your email address.</li>\n";}
<input type="submit" value="Submit" name="submit">
<input type="reset" name="Reset" value="Reset">
</form>
sarah
01-15-2003, 12:26 PM
Hi all,
Found what i was looking for, just didnt know wat it was called. Link below for anyone whose interested:
http://www.codingforums.com/showthread.php?
s=&threadid=8658&highlight=regexp
sarah
01-15-2003, 12:31 PM
using the above example as a guide, and regexlib.com, i found the regexp i want to use but for some reason it doesnt seem to be working. Below is my code:
/* function to check if email address is valid*/
function f_validate($email){
if (preg_match ('/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/', $email)) {
echo("match");
} else {
echo("no match"); }
}
/* validate the fields in the form*/
if (!($email=="")) {
f_validate($email); }
When i enter a valid email address I still get the "no match" error message!
If anyone can see the error, please let me know
Cheers
sarah
01-15-2003, 04:06 PM
phew!
finally cracked it! there was a syntax error:
if (preg_match ('/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/', $email)) {
echo("match");
should have been
if (preg_match ('/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}+$/', $email)) {
echo("match");
i missed out the "+"
:)
sarah
01-15-2003, 04:18 PM
oh Gawd!
Now when i try to put in the customized error its stopped working again!!! (And i thought i had cracked it). Below is my code (no error messages are received):
<?php
/* function to check if email address is valid*/
function f_validate($email){
if (preg_match ('/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-z0-9]+$/i', $email)) {
$flgError=TRUE;
$strGenError.='<li>Please enter a valid email address.</li>\n'; }
}
/*check form for any empty fields*/
if ($name == "") {
$flgError=TRUE;
$strGenError.= "<li>Please enter your name.</li>\n";}
/* do the same for all other fields*/
/* validate the fields in the form*/
if (!($email=="")) {
f_validate($email); }
no validation occurs!
However, if i change the function to :
/* function to check if email address is valid*/
function f_validate($email){
if (preg_match ('/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-z0-9]+$/i', $email)) {
echo("email match");
}else {
echo ("email doesn't match");
}
This works fine but I dont want the error messages to be at the top of the webpage, I want them to be in the body of the page. This is where in the first function i set a variable $flgError and $strGenError.
In the body I check for this variable and display all error messages:
<body>
<form>
<?php
if ($flgError) {
echo "<div>";
echo "<h5><p><li>There were problems processing your form. ";
if (strlen($strGenError)>0) {
echo "The following errors occurred:<ul>".$strGenError."</ul>";
?>
</form>
</body>
firepages
01-15-2003, 04:30 PM
<?
function f_validate($email,&$flgError,&$strGenError){
if (preg_match ('/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-z0-9]+$/i', $email)) {
$flgError=TRUE;
$strGenError.='<li>Please enter a valid email address.</li>\n';
}
}
f_validate($email,$flgError,$strGenError);
?>
OR ...
<?
function f_validate($email){
global $flgError,$strGenError;
if (preg_match ('/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-z0-9]+$/i',$email)) {
$flgError=TRUE;
$strGenError.='<li>Please enter a valid email address.</li>\n';
}
}
?>
OR ...
<?
function f_validate($email){
if (preg_match ('/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-z0-9]+$/i',$email)) {
return true;
} else{
return false;
}
}
if(f_validate($email)!=true){
$flgError=TRUE;
$strGenError.='<li>Please enter a valid email address.</li>\n';
}
?>
sarah
01-15-2003, 05:22 PM
Hey, thanks for the quick reply and 3 different suggestions. I tried all 3, the first two caused the same error as before (no validation occured), the third one detected that a non-valid email address was entered but when i entered a valid email it still came back with the same error message.
Also if the email field is left blank, two error messages occur, first error message is enter an email address and second message is enter a valid email address.
I changed this piece of code from:
if(f_validate($email)!=true){
$flgError=TRUE;
$strGenError.="<li>Please enter a valid email address.</li>\n";
}
to:
if(!($email=="")) {
f_validate($email){
$flgError=TRUE;
$strGenError.="<li>Please enter a valid email address.</li>\n"; }
}
This then only produced one error message if the field was left blank, again when a valid email address was entered it still came up with the enter valid email error message.
Im depressed...
:(
firepages
01-15-2003, 09:22 PM
ok sorry a working example & my preffered method.
note the forum has stripped out some of the regex!!
<?
function f_validate($email,&$flgError,&$strGenError){
if (!preg_match ('/^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-z0-9]+$/i', $email)) {
$flgError=TRUE;
$strGenError.='<li>Please enter a valid email address.</li>\n';
return false;
}
}
$email='fire@firepagesorg';
f_validate($email,$flgError,$strGenError);
if ($flgError) {
echo "<div>";
echo "<h5><p><li>There were problems processing your form. ";
if ($strGenError) {
echo "The following errors occurred:<ul>".$strGenError."</ul>";
}}
?>
sarah
01-16-2003, 09:34 AM
Hi,
Apologies for the late response - i finally got it working (don't know why i didnt do it this way first time round). Code is below:
function f_check_address_form ($name, $address1, $city, $country, $postcode, $email) {
$error_string = "";
/*check form for any empty fields*/
if ($name == "") {
$error_string.= "<li>Please enter your name.</li>\n";}
if ($address1 == "") {
$error_string.= "<li>Please enter your address.</li>\n";}
if ($city == "") {
$error_string.= "<li>Please enter your city / town.</li>\n";}
if ($country == "") {
$error_string.= "<li>Please enter your country.</li>\n";}
if ($postcode == "") {
$error_string.= "<li>Please enter your postcode / zip code.</li>\n";}
if ($email == "") {
$error_string.= "<li>Please enter your email address.</li>\n";}
else {
if (!(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$email))) {
$error_string.= "<li>Please enter a valid email address.</li>\n";} }
return $error_string;
}
$error_string = f_check_address_form($name, $address1, $city, $country, $postcode, $email);
if ($error_string != "") {
$flgError=TRUE;
$strGenError.=$error_string; }
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.