Forum Moderators: coopster

Message Too Old, No Replies

Form Validation Help

         

kevinkhan

12:12 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



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]<?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> [/PHP]

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...

jatar_k

2:04 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this thread will give some basics [webmasterworld.com...]

I would suggest making 2 inputs for name, 1 for first, 1 for last, then just check that it is not empty [php.net] and the strlen [php.net] is > 3

for the email address you could search for a regex, or regular expression using your search engine of choice, they are all out there. then test it

you could check your phone number as a string using strlen again == 10 and use ctype_digit [php.net] for the number check

and use strlen again for your commments field

kevinkhan

3:11 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



ok thanks :)

Thanks for your help guys. I came up with this code in the end

[php]<?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>

[/php]

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?

Matthew1980

7:00 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there KevinKhan,

Wouldn't displaying one error a a time defeat the object, or could you not just have 1 message to say "hey, check the detail you entered as there's something wrong/missing" Or just highlight the area that's wrong? I don't mean to criticise... Everyone has their own preference.

And when you submit anything by $_POST, make sure that the data being submitted is sanitised. Just by adding
strip_tags($_POST['your_data']);
will make your transmitted data safer, as this would strip out any unwanted tags that could cause your site problems.

And

if you are using the $_POST data to send to a database, use the..
mysql_real_escape_string(strip_tags($_POST['your_data']));


mysql_real_escape_string(); function to stop any malicious code injections.

so your code would look like:-

$firstName = strip_tags($_POST['firstName']);
$lastName = strip_tags($_POST['lastName']);
$email = strip_tags($_POST['email']);
$mobile = strip_tags($_POST['mobile']);
$comments = strip_tags($_POST['comments']);


Just thought I would mention those to you..

Cheers,

MRb