<!--
/*
Javascript Form Field Validator 
This handy javascript library provides for simple validation of the data within form fields. 
It uses a "validator" option within your form input fields, to match against it's library of regular expressions. 
To use it, follow these simple steps: 
	Download the validation library.
	right click the link above, and choose "Save Target As" ... save it as "validator.js" 
	
	Upload this to your web site, in any directory 
	Add the following:
		<script language="javascript" src="/path/to/validator.js"></script> 

	On any form that you want validated, create your form like this: 
		<FORM id = "theForm" onSubmit = "return validateForm(theForm)">
		Note that you must give your form an "id", and also use that "id" in the validateForm clause 
	
	For any input field that you want validated, add a "validator" clause, ie: 
		<INPUT TYPE=TEXT name=ip value="128.0.0.1" validator="ipAddress"> 

	Valid "validator" clause values are: 
	Validator Clause 			Purpose 
	zipCode 				Matches a US zip Code (ie: 12345 or 12345-1234) 
	Currency 				Matches $17.23 or $14,281,545.45 
	Time 					Matches 5:04 or 12:34 but not 75:83 
	emailAddress 			Matches a valid email address (ie: someone@here.com or someone@here.co.uk) 
	phoneNumber 			Matches US phone (###-###-#### or (###) ###-####) 
	phoneNumberInternational 	Matches international phone numbers 
	ipAddress 				Matches a valid IP Address (###.###.###.###, all numbers being <= 255) 
	Date 					Matches a date in xx/xx/xxxx format 
	State 				Matches a US state abbreviation 
	SSN 					Matches a valid US Social Security Number
*/


    // Validator Object
    var valid = new Object();

    // REGEX Elements

        // matches zip codes
        valid.zipCode = /\d{5}(-\d{4})?/;

        // matches $17.23 or $14,281,545.45 or ...
        valid.Currency = /\$\d{1,3}(,\d{3})*\.\d{2}/;

        // matches 5:04 or 12:34 but not 75:83
        valid.Time = /^([1-9]|1[0-2]):[0-5]\d$/;

        //matches email
        valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
		valid.emailAddress.description = "Email Address";

        // matches phone ###-###-####
        valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;

        // International Phone Number
        valid.phoneNumberInternational = /^\d(\d|-){7,20}/;

        // IP Address
        valid.ipAddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;

        // Date xx/xx/xxxx
        valid.Date = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
		valid.Date.description = "dd/mm/yyyy Date format";
		
        // MonthYear xx/xxxx
        valid.MonthYear = /^([1-9]|0[1-9]|1[0-2])(\-|\/|\.)\d{4}$/;
		valid.MonthYear.description = "mm/yyyy Date format";

		// Date mm/yy
		valid.ShortDate = /^([1-9]|0[1-9]|1[0-2])(\-|\/|\.)\d{2}$/;
		valid.ShortDate.description = "mm/yy Date format";

        // State Abbreviation
	    valid.State = /^(NSW|ACT|VIC|TAS|QLD|NT|WA|SA)$/i;

        // Social Security Number
        valid.SSN = /^\d{3}\-\d{2}\-\d{4}$/;
		
		// decimal d.d
		valid.Decimal1 = /^[0-9]\.[0-9]$/;
		valid.Decimal1.description = "Decimal point format e.g: 1.1";

		valid.AlphaNumeric = /^[a-zA-Z0-9 \-\.]*$/;
		valid.AlphaNumeric.description = "Alpha Numeric text.";
		
		valid.Alphabetical = /^[a-zA-Z \-\.]*$/;
		valid.Alphabetical.description = "Alpha text.";
		
		valid.Numeric = /^[0-9]*$/;
		valid.Numeric.description = "Number.";
		
    function validateElement(element, validator) {
		var v=validator;

		if (!v) return true; 
		
		var thePat = valid[v]; 
		
		var gotIt = thePat.exec(element.value); 

		if(! gotIt){
		 alert(element.value + " is not a valid " + thePat.description);                  
		 //element.select();
		 element.value = "";
		 element.focus(); 
		 return false;
		}
		
		return true;
	}
		
	function validateElementPrintln(element, validator) {	
		var v=validator;
		var message="";
		if (!v) return true; 
		
		var thePat = valid[v]; 
		
		var gotIt = thePat.exec(element.value); 

		if(! gotIt){
			message = element.value + " is not a valid " + thePat.description + "\n";                  
		}
		
		return message;
	}
	
    function validateForm(theForm) {

        var elArr = theForm.elements; 

        for(var i = 0; i < elArr.length; i++) {

           with(elArr[i]) { 

              var v = elArr[i].validator; 

              if(!v) continue; 

              var thePat = valid[v]; 

              var gotIt = thePat.exec(value); 

              if(! gotIt){
                 alert(name + ": failure to match " + v + " to " + value);                  
                 elArr[i].select();
                 elArr[i].focus(); 
                 return false;
              }
           }
        }

        return true;

    }
//-->