
/*
--------------------------------------------------------------------------------
	NAME OF SCRIPT              : JS_GENERICVALIDATOR.JS																	
	CALLED PROGRAM              : include this script file where ever you require validations
	CALLING PROGRAM             : nothing
	LOGICAL DESCRIPTION         : used to validate all the fields in the form where the script is included
								: you have to assin the respective character after delimiter for vaious validation routines
								: ex; id=~BI to valideate blank and interger, id=U to validate uppercase letters etc...
		
	AUTHOR OF THE SCRIPT        : SriHari Raju
	LAST DATE OF MODIFICATION   : june-20-2002												
	Copyright (c) 2001 Global Tele-Systems, Mahape										
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
USAGE:
an example of using genericvalidator
call the below function on click of your submit button or wher ever you require it

function submitit()
{
if (verifyIt() == true){

//your other functions after you finish validating all the fields can be included here

document.forms[0].submit();
}
}
-----------------------------------------------------------------------------------

*/


var DELIMITER = "~"         //delimiter to be used with id attribute, append the below letters after this delimiter depends on your requirement

//append below assigned values to the Id attribute of your form elements

var INTEGERS = "I"			      //append "I" to the Id value to accept only integers
var CHARACTERS = "C"		      //to accept only characters A to Z
var ALPHANUMERIC = "A"		      //to accept only characters A to Z and 0 to 9
var EMAIL = "E"				      // to validate email address
var BLANK = "B"				      // to check for the blank spaces
var UPPERCASE = "U"			      // to accept only Upper case letters
var LOWERCASE = "L"			      // to accept only Lower case letters
var NOSPECIALSYMBOLS = "S"        // to deny special symbols
var MANDATORY = "M"			      // to make check box or option box selection mandatory  
var CHARLENGTH250 = "1"		      // Accepts text length upto 250 only
var NOBLANKSASFIRSTCHAR = "N"     // words with one or more spaces as begining characters are not allowed
var NOBLANKSINWORDS = "I"         // words with more than one space in the middles of the words are not allowed


var breakstatus   //variable used to know and required to stop executing other statment and set focus on the form element


function verifyIt() 
{
//main function from which all the sub functions will be called depends on the types of form elements
//returns true if all the validations are checked else returns false

	breakstatus = 'false'  //by default variable value
	
	var form = document.forms[0] //takin the form object to a variable
	
	for (i = 0; i < form.elements.length; i++) //iterates through the loop depends on the number of elements of the form
	{
				
		//alert(form.elements[i].name)
		//alert(form.elements[i].type)
		//alert(form.elements[i].value)
				
		if (form.elements[i].type == "text" || form.elements[i].type == "password" || form.elements[i].type == "textarea") //checks for all the text and password fields
		{
	
			if (breakstatus == "true"){
			return(false) //exits the loop if breakstatus is true i.e if any validation is failed
			}
			else	
			{
			textboxhandler(form.elements[i]) //checks various validations done on text fields depends on the String of Characters
			}
	
		}
	
	
		if (form.elements[i].type == "checkbox") //checks for all the check box fields
		{
			if (breakstatus == "true"){
				return(false) //exits the loop if breakstatus is true i.e if any validation is failed
				}
				else		
				{
				checkboxhandler(form.elements[i]) //checks various validations done on check box fields depends on the String of Characters
				}
	
		}
	
	
		
	}
		
		if (breakstatus == 'false'){
		//checks for breakstatus and returns true if all the validations are finished
			return(true);
			}
			else
			{
			return(false);
	}
	
}
	
//*************************************************************************

function checkboxhandler(objelement)
{
	var strid = objelement.id  //takes in the Id of the field
	var elementname = objelement.name  //takes in the name of the field
	//var textvalue = objelement.value   //takes in the value of the field
	
	// takes the string appended after the delimiter in the id attribute value
	var strchars = (strid.substring(strid.indexOf(DELIMITER)+1,strid.length))
		
	for (j = 0; j < strchars.length; j++)	//counter runs until it complets all the chars in the string
	
	{
	
		strchar = (strchars.substring(j,j+1)); //takes in the single charactor
	
			
		switch (strchar.toUpperCase()) //acts as select case statement
		{
		case MANDATORY: //email validation
		
				{
					if (checkelement(objelement) == false)
						{
							alert('You can not Deselect Mandatory field : ' + elementname)
							setfocus(objelement) //sets focus on the field
							return
							}
						 }
							break
			
					
		}
	}
}

//*************************************************************************
function textboxhandler(objelement)
//function handles all the validations required on textboxes,  password and textarea boxes
{
	
	var strid = objelement.id  //takes in the Id of the field
	var elementname = objelement.name  //takes in the name of the field
	var textvalue = objelement.value   //takes in the value of the field
	
	// takes the string appended after the delimiter in the id attribute value
	var strchars = (strid.substring(strid.indexOf(DELIMITER)+1,strid.length))
		
	for (j = 0; j < strchars.length; j++)	//counter runs until it complets all the chars in the string
	
	{
	
		strchar = (strchars.substring(j,j+1)); //takes in the single charactor
		//alert(strchar)
			
		switch (strchar.toUpperCase()) //acts as select case statement
	
		{
	
		case EMAIL: //email validation
						
					if (textvalue != "")
						{
						if (CheckEmail(textvalue) == false)
							{
								alert('Please Enter Valid Email Id in Field : ') //+ elementname)
								setfocus(objelement) //sets focus on the field
								return
							}
						 }
							break
	
	
		
		case CHARACTERS: //alerts if other then alfa characters are enterd
	
					if (textvalue != "")
						{ 
							if (OnlyCharacters(textvalue) == false)
							{
							alert('Only Characters are Valid in Field : "' + elementname)
							setfocus(objelement)
							return
							}
						}
					break
		
		case UPPERCASE: //alerts if other than upper case letters are entered
					if (textvalue != "")
					{
						if (OnlyUppercase(textvalue) == false)
						{				
						alert('Only Uppercase Letters are valid in Field : "' + elementname)
						setfocus(objelement)
						return
						}
					}
					break
	
	
		case LOWERCASE: //alerts if other than lowercase letters are entered
				if (textvalue != "")
				{
					if (OnlyLowercase(textvalue) == false)
					{
					alert('Only Lowercase Letters are Valid in Field : "' + elementname)
					setfocus(objelement)
					return
					}
				}
				break
	
		case ALPHANUMERIC: //alerts if other than (A to Z and 0 to 9) are entered
	
				if (textvalue != "")
				{
					if (OnlyAlphanumeric(textvalue) == false)
					{
					alert('Only Alpha Numeric Letters are Valid in Field : "')// + elementname)
					setfocus(objelement)
					return
					}
				}
				break
	
	
		case NOSPECIALSYMBOLS: //alerts if any special symbols are entered
				
				if (textvalue != "")
				{
					if (NoSpecialSymbols(textvalue) == false)
					{
					alert('Special Symbols are Not Valid in Field : "')// + elementname)
					setfocus(objelement)
					return
					}
				}
				break
	
	
		case INTEGERS: //alerts if other than integers are entered
			if (textvalue != "")
			{
				if (OnlyIntegers(textvalue) == false)
					{
					alert('The value You Entered is not valid, Only Integers are Allowed in Field : "') //+ elementname)
					setfocus(objelement)
					return
					}
			}
			break
	
		case CHARLENGTH250: //alerts in case of length exceeding 250 characters
	
			if (Allow250charsonly(objelement) == false)
					{
					alert('The value You Entered is not valid, Only 250 characters are Allowed in Field : "' ) //+ elementname)
					setfocus(objelement)
					return
					}
			break
	
		case NOBLANKSASFIRSTCHAR: //alerts in case of blank spaces at the begining of the words
			
			if (textvalue != "")
			{ 
				if (NoBlanksasfirstchar(textvalue) == false)
					{
					alert('The value You Entered is not valid, Blank spaces are not Allowed in the the Field : "' ) //+ elementname)
					setfocus(objelement)
					return
					}
			}
			break
			
		case NOBLANKSINWORDS: //alerts in case of more than one space in the middle of the words
		
		if (textvalue != "")
			{ 
		if (NoBlanksinwords(textvalue) == false)
					{
					alert('The value You Entered is not valid, Blank spaces are not Allowed in the the Field : "' ) //+ elementname)
					setfocus(objelement)
					return
					}
			}
		break
	
		
		case BLANK: //alerts in case of blank input
			if (NoBlank(objelement) == false)
				{
				alert("Required Value is Missing in the Mandatory Field : ")//+ elementname)
				setfocus(objelement)
				return		
				}
				break
									
		}
		
	} 
	
}


//*******************************************************************
function checkelement(objelement)
//returns false if the element is de selected
{
if (objelement.checked == false)
{
return(false)
}
return(true)
}

//*******************************************************************
function setfocus(objelement)
//used to set the focus on the element on which validation is failed
{
objelement.focus()
breakstatus = "true" //used to break the parent For loop
}

//********************************************************************

function validateValues(Inputval,AcceptCharacters, DenyCharacters)
{
//author: Sri Hari Raju
//function to check the inputvalues against specified accept or denied characters
//specify all the charectors you want to accept in acceptcharacters and all the charectors you want to deny in denycharactors
//at a time you can give either acceptcharactors or denycharactors
//acceptcharactors ex: validateValues(inputval,'abcdef','') gives false if any charector from inpuval is not there in acceptcharactors
//denyedcharactors ex: validateValues(inputval,'','abcdef') gives false if any charector from inpuval exists in denycharactors


for (var k = 0; k < Inputval.length; k++)
//takes in every character of inputval string and iterates the loop for every charector
{
	var char = (Inputval.substring(k,k+1)); //takes in the charector of string on after the other
  
  // If list of characters to accept has been specified
    if (AcceptCharacters != '')
      
    {
        if (AcceptCharacters.indexOf(char) < 0)
        //if the charector from inputval is not there in acceptcharecters then returns false
	    return(false)
    }
    
  // Otherwise use the list of characters to deny
     
    if (DenyCharacters != '')
      {
        if (DenyCharacters.indexOf(char) > -1)
        //if the charector from inputval is there in deniedcharecters then returns false
	    return(false)
           
       }
}
 
// Accept by default and returns true
return(true)
}


//********************************************************************


function OnlyIntegers(inputval)
//returs false in case of chars other than integers
{
return(validateValues(inputval,'0123456789',''));
}
//********************************************************************

function OnlyCharacters(inputval)
//returns false in case of chars other than alfabets
{
return(validateValues(inputval,'ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz ',''));
}
//********************************************************************

function NoBlank(objelement)
//checks for blank and returns fale in case of blank value
{
	if (objelement.value == "")
	{
	return(false);
	}
return(true);
}
//********************************************************************

function NoBlanksasfirstchar(inputval)
//checks for blank spaces in the begining of the word
{
if(inputval.substring(0,1) == ' ')
  {
   return(false);
  }
return(true);
}
//******************************************************************

function NoBlanksinwords(inputval)
//checks checks blank spaces in the middle of the words
{
	for(var i = 0 ;i<inputval.length;i++)
	{
			 if(inputval.charAt(i+1)==' ')
			 {
				return(false);
					
			 }
	   
	}
return(true);
}



//******************************************************************
function CheckEmail(inputval)
{
//returns false in case of invalid email id
var str = inputval

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if (str.indexOf(at)==-1){
		   //checks for @ symbol in the string
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //checks for the position of @, if @ is first character or last character or not there then returns false
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   //checks for dot and the position of dot in the string
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //checks weather other chars are alos present apart from @
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //checks for dots position
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //checks for the postion of dot, weather dot is there besides @ or before @
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //checks for any space in the string
		    return false
		 }

 		 return true //by default returns true
}

//********************************************************************

function OnlyUppercase(inputval)
//returns false in case of chars other than uppercase characters
{
return(validateValues(inputval,'ABCDEFGHIJKLMNOPQURSTUVXYZ',''));
}
//********************************************************************

function OnlyLowercase(inputval)
//returns false in case of chars other than lowercase characters
{
return(validateValues(inputval,'abcdefghijklmnopqurstuvwxyz',''));
}
//********************************************************************

function NoSpecialSymbols(inputval)
//returns false in case of any specialsysmbols like @#$%^& etc..
{
return(validateValues(inputval,'','~!@#$%^&*()_-+=/<>.,?;:"'));
}
//********************************************************************

function OnlyAlphanumeric(inputval)
//returns false in case of chars other than below given string
{
return(validateValues(inputval,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',''));
}
//**********************************************************************

function Allow250charsonly(objelement)
//checks for the length of input value and returns false in case of length exceeding 250 characters
{
var characters = objelement.value
	if (characters.length > 250)
		{
		return(false);
		}
		return(true);
}		

