Jump to content

Form Validation


j05hr

Recommended Posts

Hi,

 

I have this script that submits my form, it makes you fill out all the fields but I want it to be able to make you use an @ sign as a valid email address.  How can I do this?

 

Here is my code,  I've taken all sensitive information out like my email address and links.

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

// Minimal form validation:
if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
&& !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

	// Create the body:
	$body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
	\n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

	// Make it no longer than 70 characters long:
	$body = wordwrap($body, 70);

	// Send the email:
	mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

	// Print a message:
	$message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";


	// Clear $_POST (so that the form's not sticky):
	$_POST = array();

} else {
	$message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';

}

} // End of main isset() IF.

// Create the HTML form:
?>
<body>

<div class="wrapper">
        <div id="header">
        	<img src="images/logo.jpg" alt="logo" />
            <div id="headerImage">
             
            </div>
         </div>
	<div id="navigation">
    	<div id="nav">
			<ul>
				<li class="about"><a href="about.html">About</a></li>
				<li class="print"><a href="print.html">Print</a></li>
				<li class="design"><a href="design.html">Design</a></li>
				<li class="web"><a href="web.html">Web</a></li>
				<li class="shop"><a href="shop.html">Shop</a></li>
				<li class="quotationActive"><a href="quote.php">Quotation</a></li>
				<li class="contact"><a href="contact.html">Contact</a></li>
			</ul>
	</div>
	</div>
        <div id="quoteContent">
	      <div id="contentLeftQuote">
            <form action="quote.php" method="post" class="mailing" />
			<h3>Contact Information</h3>
			<h4>Contact Name</h4>
			<input class="contactInput" type="text" name="name" class="contact" id="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>
			<h4>Phone Number</h4>
			<input class="contactInput" type="text" name="number" class="contact" id="number" value="<?php if (isset($_POST['number'])) echo $_POST['number']; ?>"/>
			<h4>Email Address</h4>
			<input class="contactInput" type="text" name="eMail" class="contact" id="eMail" value="<?php if (isset($_POST['eMail'])) echo $_POST['eMail']; ?>"/>
			<h4>Address</h4>
			<textarea class="contactInput" name="address" rows="5" cols="30" id="address" value="<?php if (isset($_POST['address'])) echo $_POST['address']; ?>" ></textarea>

            </div>
            <div id="contentRightQuote"> 
			<div class="contentQuote">
			<h3>Quotation</h3>
			<div class="floatLeft">
			<h4>Size:</h4>
			<input type="text" name="size" id="size" value="<?php if (isset($_POST['size'])) echo $_POST['size']; ?>" />
			</div>
			<div class="floatRight">
			<h4>Quantity:</h4>
			<input type="text" name="quantity" id="quantity" value="<?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>" />
			</div>
			<div class="clear">
			<h4>Paper Type/Weight:</h4>
			<input class="longInput" type="text" name="paperType" id="paperType" value="<?php if (isset($_POST['paperType'])) echo $_POST['paperType']; ?>" />
			</div>
			<div class="floatLeft">
			<h4>Number of Colours:</h4>
			<input type="text" name="colours" id="colours" value="<?php if (isset($_POST['colours'])) echo $_POST['colours']; ?>" />
			</div>
			<div class="floatRight">
			<h4>Finishing Info:</h4>
			<input type="text" name="finishing" id="finishing" value="<?php if (isset($_POST['finishing'])) echo $_POST['finishing']; ?>" />
			</div>
			<div class="clear">
			<h4>Other Info:</h4>
			<textarea name="otherInfo" rows="5" cols="40" id="otherInfo" <?php if (isset($_POST['otherInfo'])) echo $_POST['otherInfo']; ?>></textarea>
			<p class="submit"><input type="submit" name="submitted" value="" /></p>
			</div>
			</div>

        	</div>
        </div>
        <div id="footer">
        <div id="footerlinks"><a href="about.html">About</a> <a href="print.html">Print</a> <a href="design.html">Design</a> <a href="web.html">Web</a> <a href="quotation.php">Quotation</a> <a href="contact.html">Contact</a> <a href="terms.html">Terms</a></div>
	<div id="digitalStudio">© </a>
        </div>
        </div>
    </div>
</body>
</html>

 

Thanks for any help,

Josh

Link to comment
Share on other sites

function validateEmailAddr($email){
return preg_match('/[^\x00-\x20()<>@,;:\\".
[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
\x7f-\xff]+)+/i', $email);
}

if(!validateEmailAddr($email)){
die("Invalid email");
}

Link to comment
Share on other sites

Thanks for the help,  Am I supposed to use both the functions or are they doing the same thing?  The first one worked but the error message came up on a new page.

 

I tried to turn

 

if(!validateEmailAddr($email)){
die("Invalid email");
}

 

into

 

if(!validateEmailAddr($email)){
		$message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>';
	}

 

But then the form submitted after I did that.  How can I write my error message to come out on the same page using $message? 

 

I'm sorry my PHP skills aren't great,

 

Thanks again for your help,

Josh

Link to comment
Share on other sites

It comes out blank with no message,

 

Is this how it should look?

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

// Minimal form validation:
if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
&& !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

function validateEmailAddr($email){
		return preg_match('/[^\x00-\x20()<>@,;:\\".
		[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+)+/i', $email);
	}

	if(!validateEmailAddr($email)){
		die($message);
	}

	// Create the body:
	$body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
	\n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

	// Make it no longer than 70 characters long:
	$body = wordwrap($body, 70);

	// Send the email:
	mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

	// Print a message:
	$message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";


	// Clear $_POST (so that the form's not sticky):
	$_POST = array();

} else {
	$message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';

}

} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

@vividona- I believe what he is wanting to do is keep the form open and make a message beside the email box or at the top of the form saying that the email is not valid. and then make the fix the problem and resubmit. I myself have never worked with forms much so I don't know how to do any of it. but I believe that I know what you a wanting.

Link to comment
Share on other sites

@vividona- I believe what he is wanting to do is keep the form open and make a message beside the email box or at the top of the form saying that the email is not valid. and then make the fix the problem and resubmit. I myself have never worked with forms much so I don't know how to do any of it. but I believe that I know what you a wanting.

 

Hi unlishema.wolf

thank you for your remark

 

die() function, will stop all codes after it

throw Exception will give error and continue the rest of codes.

 

but I think he must throw exception and then redirect the page or use JavaScript.

Link to comment
Share on other sites

Thanks, that

die('<p style="font-weight: bold; color: #C00">Please use a valid email address</p>');

works but as you said die function stops all the code after it so it doesn't submit it on the same page, is there any way to substitute die for something else so the error message will come up in the same page?

Link to comment
Share on other sites

The other code didn't work

throw new Exception('<p style="font-weight: bold; color: #C00">Please use a valid email address</p>');

 

It comes up with

 

Parse error: syntax error, unexpected T_NEW in /homepages/32/d244452625/htdocs/quote.php on line 27

 

Link to comment
Share on other sites

let me take a quick wack at this. you need the rest of the code for when you throw the exception. you only have part of it. try this.

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {



// Minimal form validation:



if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 



&& !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {






function validateEmailAddr($email){







return preg_match('/[^\x00-\x20()<>@,;:\\".







[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]







\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]







\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]







\x7f-\xff]+)+/i', $email);





}





if(!validateEmailAddr($email)){




try{



if(!validateEmailAddr($email)){





throw new Exception('<p style="font-weight: bold; color: #C00">Please use a valid email address</p>');



}
}
catch ( Exception $e ) {
echo $e->getMessage();
}







}








// Create the body:





$body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 





\n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";










// Make it no longer than 70 characters long:





$body = wordwrap($body, 70);








// Send the email:





mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");










// Print a message:





$message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";















// Clear $_POST (so that the form's not sticky):





$_POST = array();






} else {





$message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';








}




} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

The other code didn't work

throw new Exception('<p style="font-weight: bold; color: #C00">Please use a valid email address</p>');

 

It comes up with

 

Parse error: syntax error, unexpected T_NEW in /homepages/32/d244452625/htdocs/quote.php on line 27

 

Plz copy your code

Link to comment
Share on other sites

Ok I went a redid the code so it looks better and fixed the error while I was at it.

 

If this don't work then vividona will have to help you out.

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

// Minimal form validation:   
if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
&& !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

	function validateEmailAddr($email){
		return preg_match('/[^\x00-\x20()<>@,;:\\".
		[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+)+/i', $email);
	}

	try{
		if(!validateEmailAddr($email)){
			throw new Exception('<p style="font-weight: bold; color: #C00">Please use a valid email address</p>');
		}
	}
	catch ( Exception $e ) {
	echo $e->getMessage();
	}

	// Create the body:
	$body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
	\n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

	// Make it no longer than 70 characters long:
	$body = wordwrap($body, 70);

	// Send the email:
	mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

	// Print a message:
	$message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

	// Clear $_POST (so that the form's not sticky):
	$_POST = array();

} else {
	$message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
}
} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

Still didn't work, it doesn't like the

 try {

  Thanks for trying though

 

Here is my full code,

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

// Minimal form validation:   
if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
&& !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

	function validateEmailAddr($email){
		return preg_match('/[^\x00-\x20()<>@,;:\\".
		[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
		\x7f-\xff]+)+/i', $email);
	}

	try{
		if(!validateEmailAddr($email)){
			throw new Exception('<p style="font-weight: bold; color: #C00">Please use a valid email address</p>');
		}
	}
	catch ( Exception $e ) {
	echo $e->getMessage();
	}

	// Create the body:
	$body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
	\n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

	// Make it no longer than 70 characters long:
	$body = wordwrap($body, 70);

	// Send the email:
	mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

	// Print a message:
	$message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

	// Clear $_POST (so that the form's not sticky):
	$_POST = array();

} else {
	$message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
}
} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

<?php 

function validateEmailAddr($email){
return preg_match('/[^\x00-\x20()<>@,;:\\".
[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
\x7f-\xff]+)+/i', $email);
}
// Check for form submission:
if (isset($_POST['submitted'])) {

// Minimal form validation:
try{
	if(!validateEmailAddr($_POST['eMail'])){
		throw new Exception('<p style="font-weight: bold; color: #C00">Please use a valid email address</p>');
	}

	if (empty($_POST['name']) && empty($_POST['number']) && empty($_POST['eMail']) && empty($_POST['address']) && empty($_POST['size']) 
	&& empty($_POST['quantity']) && empty($_POST['paperType']) && empty($_POST['colours']) && empty($_POST['finishing']) && empty($_POST['otherInfo']) ) {
		throw new Exception('<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>');
	}

	// Create the body:
	$body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

	// Make it no longer than 70 characters long:
	$body = wordwrap($body, 70);

	// Send the email:
	mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

	// Print a message:
	echo "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

	// Clear $_POST (so that the form's not sticky):
	$_POST = array();

}
catch ( Exception $e ) {
echo $e->getMessage();
}
}else{
//print your form here
}
?>

 

At the end of code I wrote

}else{
//print your form here
}

 

Plz fill the form inside else statement

Link to comment
Share on other sites

I just tried your full code vivi and it didn't work saying about the

 try {

not working.  This is on a company I work for server.  So I tried it on my personal one and it works, is there any chance try { is a PHP 5 function?

Link to comment
Share on other sites

yes try { is a php 5 function I will look around for a solution.

 

try this in the meantime.

 

<?php 

// Check for form submission:
if (isset($_POST['submitted'])) {

   // Minimal form validation:   
   if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) 
   && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) {

      function validateEmailAddr($email){
         return preg_match('/[^\x00-\x20()<>@,;:\\".
         [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]
         \x7f-\xff]+)+/i', $email);
      }

      if(!validateEmailAddr($email)){
            echo '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>';
      }
      catch ( Exception $e ) {
      echo $e->getMessage();
      }

      // Create the body:
      $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']}  \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} 
      \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}";

      // Make it no longer than 70 characters long:
      $body = wordwrap($body, 70);

      // Send the email:
      mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}");

      // Print a message:
      $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours.";

      // Clear $_POST (so that the form's not sticky):
      $_POST = array();

   } else {
      $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>';
   }
} // End of main isset() IF.

// Create the HTML form:
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.