Javascript Form Validation

Dibawah ini sample script yang sudah dimodif untuk validasi email dan nomor telepon (+62-21 5555555)

<script language=”javascript”
type=”text/javascript”>
<!– hide script from older browsers
function IsNumeric(strString)
//  check for valid numeric strings
{
var strValidChars = “0123456789.-+”;
var strChar;
var blnResult = true;
if (strString.length == 0) return false;
//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
return blnResult;
}
function validateForm(contact)
{
if(“”==document.forms.contact.name.value)
{
alert(“Please enter your name.”);
document.forms.contact.name.focus();
return false;
}
var phone = document.forms.contact.phone.value;
if(“”==phone)
{
alert(“Please enter your phone.”);
document.forms.contact.phone.focus();
return false;
}else if(!IsNumeric(phone)){
alert(“Please enter your phone all in numeric e.g. +62-21-5555555.”);
document.forms.contact.phone.focus();
return false;
}
if(“”==document.forms.contact.email.value)
{
alert(“Please enter your email.”);
document.forms.contact.email.focus();
return false;
}else if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.forms.contact.email.value)){}
else{
alert(“Please enter your correct email address.”);
document.forms.contact.email.focus();
return false;
}
document.forms.contact.submit();
}
–>
</script>

<script language=”javascript”   type=”text/javascript”>
<!– hide script from older browsersfunction IsNumeric(strString)   //  check for valid numeric strings    {   var strValidChars = “0123456789.-+”;   var strChar;   var blnResult = true;
if (strString.length == 0) return false;
//  test strString consists of valid characters listed above   for (i = 0; i < strString.length && blnResult == true; i++)      {      strChar = strString.charAt(i);      if (strValidChars.indexOf(strChar) == -1)         {         blnResult = false;         }      }   return blnResult;   }

function validateForm(contact){
if(“”==document.forms.contact.name.value){alert(“Please enter your name.”);document.forms.contact.name.focus();
return false;}
var phone = document.forms.contact.phone.value;
if(“”==phone){alert(“Please enter your phone.”);document.forms.contact.phone.focus();
return false;}else if(!IsNumeric(phone)){ alert(“Please enter your phone all in numeric e.g. +62-21-5555555.”);document.forms.contact.phone.focus();
return false; }
if(“”==document.forms.contact.email.value){alert(“Please enter your email.”);document.forms.contact.email.focus();
return false;}else if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.forms.contact.email.value)){}else{ alert(“Please enter your correct email address.”);document.forms.contact.email.focus();
return false; }
document.forms.contact.submit();} –></script>

Semoga bermanfaat.

source: pbdr.com

One thought on “Javascript Form Validation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s