form with validation

Posted: May 28, 2012 in Java Scripts

FORM VALIDATION

<script type=”text/javascript” src=”../js/validation.js”></script>

function validate(frm)

{var hi011=frm.name.value.replace(/^\s+|\s+$/g,””);if(hi011==””||hi011==”Name*:”)

{alert(“Please enter Name”)

frm.name.value=”;frm.name.focus();return false;}

if(!IsChar(frm.name.value,”Name”))

{frm.name.focus();return false;}

var hi012=frm.cname.value.replace(/^\s+|\s+$/g,””);if(hi012==””||hi012==”Company Name*:”)

{alert(“Please enter Company Name”)

frm.cname.value=”;frm.cname.focus();return false;}

if(!IsChar(frm.cname.value,”Company Name”))

{frm.cname.focus();return false;}

if(frm.email.value==””||frm.email.value==”Email*:”)

{alert(“Please enter your Email”)

frm.email.value=”;frm.email.focus();return false;}

if(!checkemail(frm.email.value))

{frm.email.focus();return false;}

if(frm.phone.value==””||frm.phone.value==”Phone Number*:”)

{alert(“Please enter the Phone Number”)

frm.phone.value=”;frm.phone.focus();return false;}

if(!IsFax(frm.phone.value,”Phone Number”)){frm.phone.focus();return false;}

if(frm.website.value==””||frm.website.value==”Website*:”)

{alert(“Please enter your Website”)

frm.website.value=”;frm.website.focus();return false;}

if(!is_valid_domain_name(frm.website.value))

{frm.website.focus();return false;}

var hi0=frm.country.value.replace(/^\s+|\s+$/g,””);if(hi0==””||hi0==”Country*:”)

{alert(“Please enter Country”)

frm.country.value=”;frm.country.focus();return false;}

if(!IsChar(frm.country.value,”Country”)){frm.country.focus();return false;}

var hi=frm.keyword.value.replace(/^\s+|\s+$/g,””);if(hi==””||hi==”Keywords*:”)

{alert(“Please enter Keywords”)

frm.keyword.value=”;frm.keyword.focus();return false;}

var hi1=frm.special.value.replace(/^\s+|\s+$/g,””);if(hi1==””||hi1==”Special Needs*:”)

{alert(“Please enter Special Needs”)

frm.special.value=”;frm.special.focus();return false;}

return true;}

function IsChar(sText,Label)

{var ValidChars=”a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z  “;var IsChar=true;var Char;for(i=0;i<sText.length&&IsChar==true;i++)

{Char=sText.charAt(i);if(ValidChars.indexOf(Char)==-1)

{IsChar=false;}}

if(!IsChar)

alert(Label+” must be alphabets”)

return IsChar;}

function char(sText,message)

{var ValidChars=”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;var IsAlphabetic=true;var Char;for(i=0;i<sText.length&isAlphabetic==true;i++);{Char=sText.charAt(i);if(ValidChars.indexOf(Char)==-1)

{IsAlphabetic=false;alert(message+” must be alphabets”);}}

return IsAlphabetic;}

function IsFax(sText,Label)

{var ValidChars=”0123456789-()”;var IsNumber=true;var Char;if(sText==””){alert(Label+” Should not be Empty”);return false}

for(i=0;i<sText.length&&IsNumber==true;i++)

{Char=sText.charAt(i);if(ValidChars.indexOf(Char)==-1)

{IsNumber=false;}}

if(!IsNumber)

alert(Label+” Must Be in Format eg:229-230-4443 or (229)-230-4443″)

return IsNumber;}

function checkemail(val){var str=val

var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

if(filter.test(str))

testresults=true

else{alert(“Please input a valid email address!”)

testresults=false}

return(testresults)}

function IsNum(sText,Label)

{var ValidChars=”0123456789″;var IsNumber=true;var Char;if(sText==””){alert(Label+” Should not be Empty”);return false}

for(i=0;i<sText.length&&IsNumber==true;i++)

{Char=sText.charAt(i);if(ValidChars.indexOf(Char)==-1)

{IsNumber=false;}}

if(!IsNumber)

alert(Label+” Must Be in Format eg:229-230-4443 or (229)-230-4443″)

return IsNumber;}

function is_valid_domain_name(str_url){var filter=/^[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s&lt;&gt;\#%”\,\{\}\\|\\\^\[\]`]+)?$/

if(!filter.test(str_url))

{alert(‘URL format is not ok.’)}

else

{return str_url;}}

function key(keyword)

{var number_format=”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopgrstuvwxyz0123456789″;for(var i=1;i<keyword.value.length;i++)

{check_char=number_format.indexOf(keyword.value.charAt(i))

if(check_char<0)

{alert(“Only letters and digits”);return false;}

else

{return true;}}}

=============================================================================================

HTML CODE

<form name=”form2″ id=”form2″ method=”post” action=”../ask-for-quote.php” onsubmit=”return validate(this)”>
<input type=”text” value=”Name*:” name=”name” id=”name” onfocus=”if (this.value==’Name*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Name*:’; this.style.color=”;}” />
<input type=”text” value=”Company Name*:” name=”cname” id=”cname” onfocus=”if (this.value==’Company Name*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Company Name*:’; this.style.color=”;}” />
<input type=”text” value=”Email*:” name=”email” id=”email” onfocus=”if (this.value==’Email*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Email*:’; this.style.color=”;}” />
<input type=”text” value=”Phone Number*:” name=”phone” id=”phone” onfocus=”if (this.value==’Phone Number*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Phone Number*:’; this.style.color=”;}” />
<input type=”text” value=”Website*:” name=”website” id=”website” onfocus=”if (this.value==’Website*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Website*:’; this.style.color=”;}” />
<input type=”text” value=”Country*:” name=”country” id=”country” onfocus=”if (this.value==’Country*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Country*:’; this.style.color=”;}” />
<textarea name=”keyword” id=”keyword” rows=”3″ cols=”3″ onfocus=”if
(this.value==’Keywords*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Keywords*:’; this.style.color=”;}” >Keywords*:</textarea>
<textarea name=”special” id=”special” rows=”3″ cols=”3″ onfocus=”if
(this.value==’Special Needs*:’)this.value=”; this.style.color=’#0000′;” onblur=”if(this.value==”) { this.value = ‘Special Needs*:’; this.style.color=”;}” >Special Needs*:</textarea>
<input type=”submit” value=”” />
</form>

 

=============================================================================================

PHP FILE

<?php

if(isset($_POST[’email’]))

{

$subject =”SEO Concrete(Contact Form)”;

$email=’info@seoconcrete.com, seoconcrete@gmail.com’;

$body = “<table>”;

$body .= “<tr><td>Name: </td><td>:- “.$_POST[‘name’].”</td></tr>”;

$body .= “<tr><td>Company Name: </td><td>:- “.$_POST[‘cname’].”</td></tr>”;

$body .= “<tr><td>Email: </td><td>:- “.$_POST[’email’].”</td></tr>”;

$body .= “<tr><td>Phone number : </td><td>:- “.$_POST[‘phone’].”</td></tr>”;

$body .= “<tr><td>Website: </td><td>:- “.$_POST[‘website’].”</td></tr>”;

$body .= “<tr><td>Country:</td><td>:- “.$_POST[‘country’].”</td></tr>”;

$body .= “<tr><td>Keywords: </td><td>:- “.$_POST[‘keyword’].”</td></tr>”;

$body .= “<tr><td>Special Needs: </td><td>:- “.$_POST[‘special’].”</td></tr>”;

$body .= “</table>”;

$headers  = ‘MIME-Version: 1.0’ . “\r\n”;

$headers .= “From: “.$_REQUEST[‘name’].” <“.$_REQUEST[’email’].”>\n”;

$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;

$headers .= “<b>”.$_POST[’email’].”</b> \r\n”;

mail($email, $subject, $body, $headers);

echo ‘<script>    window.location=”thankyou.html”

</script>’;

}

?>

Advertisements
Comments
  1. I just want to mention I am just all new to weblog and seriously savored your page. Almost certainly I’m going to bookmark your website . You surely come with really good stories. Cheers for revealing your web page.