Login   Register  
PHP Classes
elePHPant
Icontem

File: validate.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of dan boorn  >  OO Mysql Wrapper  >  validate.js  >  Download  
File: validate.js
Role: Auxiliary data
Content type: text/plain
Description: Validation JS Script used in the example application
Class: OO Mysql Wrapper
MySQL database SQL queries wrapper
Author: By
Last change:
Date: 2006-07-29 15:48
Size: 2,376 bytes
 

Contents

Class file image Download
/*****************************************************************************************************
* Easy Javascript Validation by Daniel Boorn
* Copyright 2006, All Rights reserved by Daniel Boorn
* Contact: daniel.boorn@gmail.com - wwww.dboorn.com
* In any form element add the following form attributes to validate
* required ="yes"
* validate = { "int", "float", "text", "email" }
* message = "Error Message for Element"
* Example: <input type="text" validate="int" message="Please enter valid zip code" name="zip">
*
* It is required that you add the following to any submit button
*     onClick="validate(this.form); return document.formSubmit;"
******************************************************************************************************/

	function validate(form){
		
		var error = "";
		//for each form element
		for(var i=0; i<form.length; i++){
			var element = form[i];
			//if required
			if(element.getAttribute("required") == "yes"){
				//if form element if empty
				if(!valid(element.value,element.getAttribute("validate"),element))
					error += element.getAttribute("message") + "\r\n";	
			}
			else if(element.getAttribute("validate") != ""){
				//if validation is need by not required
				if(element.value != ""){
					if(!valid(element.value,element.getAttribute("validate"),element))
						error += element.getAttribute("message") + "\r\n";
				}
			}
		}
		if(error != ""){
			alert(error);
			document.formSubmit = false;
		}
		else
			document.formSubmit = true;
	}	
	
	function valid(value,type,element){
		if(value == "")
			return false;
			
		switch(type){
			case "int":
				if(isNaN(parseInt(value)))
					return false;
				break;
			case "float":
				if(isNaN(parseFloat(value)))
					return false;
				break;
			case "email":
				var p = value.indexOf('@');
				if(p<1 || p==(value.length-1))
					return false;
				break;
			case "checked":
				if(!element.checked)
					return false;
				break;
			default://string
				break;
		}
		return true;
	}	

    function check_length(maxchars,message,div_id)
    {
    	var len = message.value.length;

        if(len > maxchars){
        	message.value = message.value.substr(0,maxchars);
        	len = maxchars;
        }

        document.getElementById(div_id).innerHTML = maxchars - len;
    }