Issue with website Email Code

kim_foxx

Member
Messages
84
Reaction score
0
Points
6
I designed this page http://roadbullettransport.com.au/contact.html and have set validation rules so blank emails do not get sent. However, the owner of the website has now realized that blank emails with no information are being as as below.

First Name:
Last Name:
Company:
Contact Number:
Email:
Message:

Does anyone know why and how this can be stopped?

The PHP Code that i used is:

PHP:
<?php 
    

function is_valid_email($from_email)
{
    return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s\'"<>]+\.+[a-z]{2,6}))$#si', $from_email);
} 



$to_email = "info@roadbullettransport.com.au";
$subject = "Road Bullet Transport Enquiry";
$firstname.=$_POST['firstname']."\n" ;
$lastname.=$_POST['lastname']."\n" ;
$company.=$_POST['company']."\n" ;
$contactnumber.=$_POST['contactnumber']."\n" ;
$from_email.=$_POST['email'] ."\n" ;
$msg.=$_POST['message']."\n" ;


$message = "

    First Name:
    $firstname
    
    Last Name:
    $lastname

    Company:
    $company
    
    Contact Number:
    $contactnumber

    Email:
    $from_email

    Message:
    $msg


";
    
    
$sent = mail($to_email, $subject, $message) ;
if($sent)
echo "<p>Thanks $name. Your message has been sent successfully.</p>";


else
echo "<p>Sorry $name. We encountered an error sending your mail. Please try again.</p>";

?>


And the Javascript

Code:
<script language="JavaScript" type="text/javascript">

function emailcheck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		//Checks to see there is an @ exists
		//Checks to see if the @ is at the start
		//Checks to see if the @ is at the end
		   alert("Your Email address is incorrect please correct it and try again.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//Checks to see there is a . exists
		//Checks to see if the . is at the start
		//Checks to see if the . is at the end
		    alert("Your Email address is incorrect please correct it and try again.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		 //Checks to see if there is more then one @
		    alert("Your Email address is incorrect please correct it and try again.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		 //Checks to see if the . is before the @
		 //Checks to see if there is a . after the @
		    alert("Your Email address is incorrect please correct it and try again.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		//Checks to see if there is a dot 2 characters past the @
		    alert("Your Email address is incorrect please correct it and try again.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		 //Checks to see if there are any spaces
		    alert("Your Email address is incorrect please correct it and try again.")
		    return false
		 }

 		 return true					
	}
	
	

function validate(mailform){ 

var digits="0123456789" 
var temp

if (document.mailform.firstname.value=="") { 
// Check to see if firstname is blank

alert("Please fill in your First Name and try again.") 
document.mailform.firstname.focus()
return false 
} 



for (var i=0;i<document.mailform.firstname.value.length;i++){ 
temp=document.mailform.firstname.value.substring(i,i+1) 
if (digits.indexOf(temp)!=-1){ 
// Check to see if firstname contains numbers

alert("Your First Name cannot contain numbers. Please try again.") 
document.firstname.name.focus()
return false 

}
}


if (document.mailform.lastname.value=="") { 
// Check to see if firstname is blank

alert("Please fill in your Last Name and try again.") 
document.mailform.lastname.focus()
return false 
} 



for (var i=0;i<document.mailform.lastname.value.length;i++){ 
temp=document.mailform.lastname.value.substring(i,i+1) 
if (digits.indexOf(temp)!=-1){ 
// Check to see if last name contains numbers

alert("Your Last Name cannot contain numbers. Please try again.") 
document.lastname.name.focus()
return false 

}
}

if (document.mailform.contactnumber.value.length < 4) { 
// Check to see if contact number is atleast 4 digits long

alert("Your Contact Number is incorrect. Please try again.")
document.mailform.contactnumber.focus() 
return false 
}

for (var i=0;i<document.mailform.contactnumber.value.length;i++){ 
temp=document.mailform.contactnumber.value.substring(i,i+1) 
if (digits.indexOf(temp)==-1){ 
// Check to see if the contact number contains characters

alert("Your Contact Number is incorrect. Please try again.") 
document.mailform.contactnumber.focus() 
return false 
}
}


if (document.mailform.message.value=="") { 
// Check to see if message is blank

alert("Please write a message and try again.") 
document.mailform.message.focus()
return false 
} 




if (document.mailform.email.value.length < 6) { 
// Check to see if the email address is atleast 6 characters long

alert("Your Email address is incorrect. Please try again.") 
document.mailform.email.focus() 
return false 
} 
	

if (emailcheck(document.mailform.email.value)==false){
//Goes to the function emailcheck and performs the checks listed below it
document.mailform.email.focus()
return false
}



return true 
} 

</script>


</head>


<body>
<script type="text/javascript">
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
You aren't testing anything. Why do you think it would stop blank emails?
 
Top