...

View Full Version : Form Validation Help



kevinkhan
02-17-2010, 12:10 PM
Im trying to make a script that will only allow use full information in a form on my website... Here is the code i have so far...


<?php
if(isset($_POST['submit']))
{
$name = $_GET['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$comments = $_POST['comments'];

if(empty($errors))
{
$to = "info@eventpromotion.ie";
$subject = "Event Promotion Enquiry!";
$body =
"First Name: " . $_POST['name'] .
"\nEmail: " . $_POST['email'] .
"\nMobile: " . $_POST['mobile'] .
"\nMessage: " . $_POST['comments'];
if (mail($to, $subject, $body)) {
echo("<p>Thanks for submitting your enquiry.</p>");
}
else
{
echo("<p>Message delivery failed.</p>");
}
}
else
{
echo "<p>".$error."</p>";
}
}
?>
<form id="form" method="post" action="testing.php">
<p>
<label>Name</label><br />
<input type="text" name="name" id="firstName" />
</p>
<p>
<label>Email:</label><br />
<input type="text" name="email" id="email" />

</p>
<p>
<label>Mobile:</label><br />
<input type="text" name="mobile" id="mobile" />

</p>
<p>
<label>Comments:</label> <br />
<textarea name="comments" cols="30" rows="3"></textarea>
</p>
<p> <input type="submit" name="submit" value="Submit" /></p>
</form>

I only want the form to be processed if the form fields are correctly filled out..

for name i want something like the input to be two words of a minimum of 3 charaters each maybe.. and if the user has not put this i want the script to display an error above the form saying something like "Please insert your full name with a space between your first and last name"

for the email field i would like a correctly formatted email address to be used and again if there is an error i would like the message to say something like "Please enter your correct email address"

For the phone field i would like the user to only enter a ten digit number

and for the comments maybe have a minimum of 20 chars..

Can somebody help me with this its bugging me all morning and i cant figure out the logic in it..

Thanks for your help...

DaiWelsh
02-17-2010, 12:27 PM
for name i want something like the input to be two words of a minimum of 3 charaters each maybe.. and if the user has not put this i want the script to display an error above the form saying something like "Please insert your full name with a space between your first and last name"

for the email field i would like a correctly formatted email address to be used and again if there is an error i would like the message to say something like "Please enter your correct email address"

For the phone field i would like the user to only enter a ten digit number

and for the comments maybe have a minimum of 20 chars..

Can somebody help me with this its bugging me all morning and i cant figure out the logic in it..

Thanks for your help...

You need to investigate regular expression and then write a few validation functions to specify your rules. For example (not fully tested but functional):



$strEmail = 'test@test.com';
$strName = 'blah blah';
$strComments = 'blah blah blah blah blah';
$strPhone = '1234567890';

$arrErrors = array();
if(!Validate_Email($strEmail)) { $arrErrors[] = 'Bad email'; }
if(!Validate_Regex($strName,'/^\w{3,}\s+\w{3,}$/')) { $arrErrors[] = 'Bad name'; }
if(!Validate_Length($strComments,20,1000)) { $arrErrors[] = 'Bad comment'; }
if(!Validate_Regex($strPhone,'/^\d{10}$/')) { $arrErrors[] = 'Bad phone'; }
if(count($arrErrors)) {
echo('errors: <ul><li>'.implode('<li></li>',$arrErrors).'</li></ul>');
} else {
echo('ok');
}
function Validate_Length($strValue,$intMin,$intMax) {
return((strlen($strValue) >= $intMin) and (strlen($strValue) <= $intMax));
}
function Validate_Email($strValue) {
$strPattern = '/^[a-z0-9&\'\.\-_\+!#$%*\/=?^`{\|}~]+@[a-z0-9\-]+\.(?:[a-z0-9\-]+\.)*?[a-z]{2,4}$/i';
return(preg_match($strPattern,$strValue));
}
function Validate_Regex($strValue,$strPattern) {
return(preg_match($strPattern,$strValue));
}


HTH,

Dai

phpcodelearner
02-17-2010, 02:24 PM
hi

there is a very easy way of doing this. but you will have to have dreamweaver:confused:.

dreamweaver have some very good validation functions like email, inters, ceditcard numbers, zip code and simple things like mini character and max es.t

dreamweaver validation works using javascript. if you want to look into it furth google spry validation.

if you don't have dreamweaver have a look here:

http://www.w3schools.com/js/js_form_validation.asp
or have a google on javascript validation

hope this help

p.s

sorry if you wanted the validation in php but i find java script easier to work with when doing validations.

Fou-Lu
02-17-2010, 02:28 PM
hi

there is a very easy way of doing this. but you will have to have dreamweaver:confused:.

dreamweaver have some very good validation functions like email, inters, ceditcard numbers, zip code and simple things like mini character and max es.t

dreamweaver validation works using javascript. if you want to look into it furth google spry validation.

if you don't have dreamweaver have a look here:

http://www.w3schools.com/js/js_form_validation.asp
or have a google on javascript validation

hope this help

p.s

sorry if you wanted the validation in php but i find java script easier to work with when doing validations.

Javascript is not an acceptable solution to form validation. It can be used to indicate problems before hitting processing, but should never be used in place of server validation. Ever.

kevinkhan
02-17-2010, 02:32 PM
THanks for your help guys.. I think i have it :)

kevinkhan
02-17-2010, 03:08 PM
Thanks for your help guys. I came up with this code in the end


<?php



if(isset($_POST['submit']))
{
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$comments = $_POST['comments'];

$errors = array();

function display_errors($error_array)
{
echo "<p class=\"errors\">";

foreach($error_array as $error)
{
echo $error . "<br />";
}
echo "</p>";
}

function validateNames($names)
{
return(strlen($names) < 3);
}

function validateEmail($strValue)
{
$strPattern = '/([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})/sim';
return(preg_match($strPattern,$strValue));
}

function validateMobile($strValue)
{
$strPattern = '/^\d{10}$/';
return(preg_match($strPattern,$strValue));
}

function validateComments($comments)
{
return(strlen($comments) < 10);
}

if(validateNames($firstName))
{
$errors[] = 'Please Enter Your First Name';
}

if(validateNames($lastName))
{
$errors[] = 'Please Enter Your Second Name';
}


if(!validateEmail($email))
{
$errors[] = 'Please Enter Your Correct Email';
}

if(!validateMobile($mobile))
{
$errors[] = 'Please Enter Your Correct Mobile Number';
}

if(validateComments($comments))
{
$errors[] = 'Please Enter A Comment More Than 10 Characters';
}


if(empty($errors))
{
$to = "info@eventpromotion.ie";
$subject = "Event Promotion Enquiry!";
$body =
"First Name: " . $_POST['firstName'] .
"\nLast Name: " . $_POST['lastName'] .
"\nEmail: " . $_POST['email'] .
"\nMobile: " . $_POST['mobile'] .
"\nMessage: " . $_POST['comments'];
$headers = "From: ". $firstName ." ". $lastName . " <" . $email . ">\r\n";


if (mail($to, $subject, $body, $headers)) {
echo("<p>Thanks for submitting your enquiry.</p>");
}
else
{
echo("<p>Message delivery failed.</p>");
}
}
else
{
//echo "error";
display_errors($errors);
}
}
?>
<form id="form" method="post" action="testing.php">
<p>
<label>First Name</label><br />
<input type="text" name="firstName" value="<?php if(isset($firstName)){echo $firstName;} ?>" />
</p>
<p>
<label>Last Name</label><br />
<input type="text" name="lastName" value="<?php if(isset($lastName)){echo $lastName;} ?>" />
</p>
<p>
<label>Email:</label><br />
<input type="text" name="email" value="<?php if(isset($email)){echo $email;} ?>" />

</p>
<p>
<label>Mobile:</label><br />
<input type="text" name="mobile" value="<?php if(isset($mobile)){echo $mobile;} ?>" />

</p>
<p>
<label>Comments:</label> <br />
<textarea name="comments" cols="30" rows="3" ><?php if(isset($comments)){echo $comments;} ?></textarea>
</p>
<p> <input type="submit" name="submit" value="Submit" /></p>
</form>



if there are more than one error how do i limit to displaying only one error at a time...

do i need a different loop or how would i go about doing it does anyone know?

DaiWelsh
02-17-2010, 04:00 PM
if there are more than one error how do i limit to displaying only one error at a time...

do i need a different loop or how would i go about doing it does anyone know?

My first though is why? For me being told about form validation errors one at a time drives me potty?

However if that is just me and you really want to do it just change this function



function display_errors($error_array)
{
echo "<p class=\"errors\">";
echo $error_array[0];
echo "</p>";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum