<!-- SophiaKnows -->

JAVASCRIPT FORMS VALIDATION
REV 4: December 2004

This Form Validates User Input
On the Go
Email:
Phone:
Plaintext:
This Form Validates User Input
On Submit
Email:
Phone:
Plaintext:

<script type='text/javascript'>
<!--
/* --------------------------------- */
/*     SOPHIAKNOWS: JAVASCRIPT       */
/* --------------------------------- */
/*         FORM SUBROUTINES          */
/* --------------------------------- */
/*  Created:  2000-03-15             */
/*  Issued:   2000-03-15             */
/*  Modified: 2004-12-17             */
/*  Component VALIDATION             */
/*  Copyright (c) 2000-2004          */
/*  Tony Pisarra, SophiaKnows        */
/* --------------------------------- */
/* --------------------------------- */

/*
  These subroutines validate the format 
  and content of user form input

  Note that Validate() is modular and
  can be used with any form whereas
  PreValidate() must be edited to work
  with the elements of the specific
  form
*/

// ERROR MESSAGES
var errorMsg=new Array();

errorMsg['email']="You must enter a valid email address\n";
errorMsg['phone']="You must enter a valid phonenumber\n";
errorMsg['plaintext']="You have enterd 1 or more illegal characters\n";
errorMsg['nothtml']="This form does not accept HTML\n";

// VALIDATION: VALIDATE
function Validate(t,m) {
   if(t.value && m=='email') {if(!IsEmail(t.value)) {alert(errorMsg[m]);}}
   if(t.value && m=='phone') {if(!IsPhonenumber(t.value)) {alert(errorMsg[m]);}}
   if(t.value && m=='plaintext'){if(!IsPlaintext(t.value))  {alert(errorMsg[m]);}}
   if(t.value && m=='nothtml'){if(!IsNotHtml(t.value))  {alert(errorMsg[m]);}}
   }

// VALIDATION: PRE VALIDATE
function PreValidate(f) {
   var errors="";
   errors+=((IsEmail(f.email.value))?"":errorMsg['email']);
   errors+=((IsPhonenumber(f.phone.value))?"":errorMsg['phone']);
   errors+=((IsPlaintext(f.plaintext.value))?"":errorMsg['plaintext']);
   if(errors) {alert(errors);} else {f.submit();}
   }

// VALIDATION: IS EMAIL
function IsEmail(stringIn) {
   return ((stringIn.search(/[\w-\.]+@[\w-\.]+\.\w+/)>-1)? 1 : 0);
   }
// VALIDATION: IS PHONENUMBER      
function IsPhonenumber(stringIn) {
   return ((stringIn.search(/\(?\d{3}\)?[ \.-]?\d{3}[ \.-]\d{4}/)>-1)? 1 : 0);
   }

// VALIDATION: IS PLAINTEXT
function IsPlaintext(stringIn) {
   return ((stringIn.search(/[^\w]/)>-1)? 0 : 1);
   }

// VALIDATION: IS NOT HTML
function IsNotHtml(stringIn) {
   return ((stringIn.search(/<[^>]+>/)>-1)? 0 : 1);
   }
-->
</script>

<form>
<p>This Form Validates User Input On the Go
<p>Email:<br /><input type=text name=email onBlur="Validate(this,'email')">
<br />Phone:<br /><input type=text name=phone onBlur="Validate(this,'phone')">
<br />Plaintext:<br /><input type=text name=plaintext onBlur="Validate(this,'plaintext')">
</form>

<form action="javascript:alert('Nice Work')" method="post">
<p>This Form Validates User Input On Submit
<p>Email:<br /><input type=text name=email>
<br />Phone:<br /><input type=text name=phone>
<br />Plaintext:<br /><input type=text name=plaintext>
<br /><input type=button value=submit onclick="PreValidate(this.form)">
</form>


< CODEBASE | TOP^ | MAINPAGE >

Text & Design By Tony Pisarra
© SophiaKnows 1998-2004