Jump to content

Contact Form validation


jessar1994

Recommended Posts

I have paid for a Contact Form script.

 

The script is working but i have a field for phone number.

 

how should I do so my script validate with a if?

 

Like this:

If the field Telephone_Number is empty then $error_message .= 'you provided no number<br />';

 

If the field Telephone_Number have more than ten digits then $error_message .= 'your number is not valid<br />';

 

If the field Telephone_Number have less than ten digits then $error_message .= 'your number is not valid<br />';

 

If the field Telephone_Number don't have numbers in the field then $error_message .= 'Please insert a valid numbers in the contact form<br />';

 

 

My code for my contact form is:

 

contactformprocess.php

<?phpif(isset($_POST['Email_Address'])) {	        $use_default_email_option = true; // change this value to false if are having problems getting emails    	include 'contactformsettings.php';		function died($error) {		echo "Sorry, but there were error(s) found with the form you submitted. ";		echo "These errors appear below.<br /><br />";		echo $error."<br /><br />";		echo "Please go back and fix these errors.<br /><br />";		die();	}		if(!isset($_POST['Full_Name']) ||		!isset($_POST['Email_Address']) ||		!isset($_POST['Telephone_Number']) ||		!isset($_POST['Your_Message']) || 		!isset($_POST['AntiSpam'])				) {		died('Sorry, there appears to be a problem with your form submission.');			}		$full_name = $_POST['Full_Name']; // required	$email_from = $_POST['Email_Address']; // required	$telephone = $_POST['Telephone_Number']; // not required	$comments = $_POST['Your_Message']; // required	$antispam = $_POST['AntiSpam']; // required		$error_message = "";		$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';  if(preg_match($email_exp,$email_from)==0) {  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';  }  if(strlen($full_name) < 2) {  	$error_message .= 'Your Name does not appear to be valid.<br />';  }  if(strlen($comments) < 2) {  	$error_message .= 'The Comments you entered do not appear to be valid.<br />';  }    if($antispam <> $antispam_answer) {	$error_message .= 'The Anti-Spam answer you entered is not correct.<br />';  }    if(strlen($error_message) > 0) {  	died($error_message);  }	$email_message = "Form details below.rn";		function clean_string($string) {	  $bad = array("content-type","bcc:","to:","cc:");	  return str_replace($bad,"",$string);	}		$email_message .= "Full Name: ".clean_string($full_name)."rn";	$email_message .= "Email: ".clean_string($email_from)."rn";	$email_message .= "Telephone: ".clean_string($telephone)."rn";	$email_message .= "Message: ".clean_string($comments)."rn";if($emailing_option == "default") {        $headers = 'From: '.$email_from."rn".    'Reply-To: '.$email_from."rn" .    'X-Mailer: PHP/' . phpversion();    mail($email_to, $email_subject, $email_message, $headers);    } else {        require_once 'lib/swift_required.php';        if($emailing_option == "sendmail") {                $transport = Swift_SmtpTransport::newInstance($smtp_host, $smtp_port, 'ssl')    		->setUsername($smtp_user)      		->setPassword($smtp_pass);            } else {                $transport = Swift_SendmailTransport::newInstance(ini_get('sendmail_path'));        $mailer = Swift_Mailer::newInstance($transport);        $message = Swift_Message::newInstance($email_subject)          ->setFrom($email_from)          ->setTo($email_to)          ->setBody($email_message);        $mailer->send($message);                }    }header("Location: $thankyou");?><script>location.replace('<?php echo $thankyou;?>')</script><?php}die();?>

contactformvalidation.js

function has_id(id){try{var tmp=document.getElementById(id).value;}catch(e){return false;}return true;}function has_name(nm){try{var tmp=cfrm.nm.type;}catch(e){return false;}return true;}function $$(id){if(!has_id(id)&&!has_name(id)){alert("Field "+id+" does not exist!n Form validation configuration error.");return false;}if(has_id(id)){return document.getElementById(id).value;}else{return;}}function $val(id){return document.getElementById(id);}function trim(id){$val(id).value=$val(id).value.replace(/^s+/,'').replace(/s+$/,'');}var required={field:[],add:function(name,type,mess){this.field[this.field.length]=[name,type,mess];},out:function(){return this.field;},clear:function(){this.field=[];}};var validate={check:function(cform){var error_message='Please fix the following errors:nn';var mess_part='';var to_focus='';var tmp=true;for(var i=0;i<required.field.length;i++){if(this.checkit(required.field[i][0],required.field[i][1],cform)){}else{error_message=error_message+required.field[i][2]+' must be suppliedn';if(has_id(required.field[i][0])&&to_focus.length===0){to_focus=required.field[i][0];}tmp=false;}}if(!tmp){alert(error_message);}if(to_focus.length>0){document.getElementById(to_focus).focus();}return tmp;},checkit:function(cvalue,ctype,cform){if(ctype=="NOT_EMPTY"){if(this.trim($$(cvalue)).length<1){return false;}else{return true;}}else if(ctype=="EMAIL"){exp=/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;if($$(cvalue).match(exp)==null){return false;}else{return true;}}},trim:function(s){if(s.length>0){return s.replace(/^s+/,'').replace(/s+$/,'');}else{return s;}}};

contactformsettings.php

<?php$email_to = "youremailaddress@yourdomain.com"; // your email address$email_subject = "Contact Form Message"; // email subject line$thankyou = "thankyou.htm"; // thank you page// if you update the question on the form -// you need to update the questions answer below$antispam_answer = "25";// If you have problems receiving emails, // change the option below:// OPTIONS: "default", "sendmail" or "smtp"$emailing_option = "default";// if you are using "smtp" add the details here$smtp_host = "smtp.****.***";$smtp_port = "587";$smtp_user = "XXXX";$smtp_pass = "XXXX";?>

Hope somebody can help me.

 

Thank you in advance!

Edited by jessar1994
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...