/**
* Jquery Form validation helper
* @version 1.0
* @author Satyam Kumawat <satyam2707@gmail.com>
* @package Jquery validation helper
* This program is free software.
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this helper to newer
* versions in the future. If you wish to customize this helper for your
* needs please refer to satyam2707@gmaail.com.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
*/
function form_validation(fields,form)
{
this.fields = fields;
this.alpha = /^[a-z ]{1,15}$/;
this.email = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
this.numeric = /^[0-9]{6,10}$/;
this.fieldvalue = "";
this.minlength ;
this.count =0;
this.current;
this.auto_error_div_generation;
this.error_class;
this.error_field_class;
this.returns = new Array();
this.validation = function (){
for(var i=0; i < fields.length; i++)
{
this.current = i;
switch(fields[i][0])
{
case 'password':
var new_array = fields[i].slice(1);
this.get_info(new_array);
break;
case 'radio' :
var new_array = fields[i].slice(1);
this.radio_validation(new_array);
break;
case 'checkbox':
var new_array = fields[i].slice(1);
this.chkbox_validation(new_array);
break;
case 'checkbox_array':
var new_array = fields[i].slice(1);
this.chkbox_array_validation(new_array);
break;
case 'select':
var new_array = fields[i].slice(1);
this.get_info(new_array);
break;
case 'text':
var new_array = fields[i].slice(1);
this.get_info(new_array);
break;
case 'file':
var new_array = fields[i].slice(1);
this.fileupload_validation(new_array);
break;
}
}
var return_string = this.returns.toString();
var position = return_string.search('false');
if(position == -1)return true;
else return false;
} //END OF VALIDATION FUNCION
/* GET INFO FUNCTION */
this.get_info = function (newarray){
this.fieldname = newarray[0];
this.fieldvalue = $('#'+this.fieldname).val();
var required_id = newarray.indexOf('required');
if(required_id!= -1)
{
newarray.splice(required_id,1);
this.chkblank(this.fieldname);
for(var j=1; j < newarray.length;j++)
{
this.error(this.fieldname,newarray[j]);
}
}
}//End of get_info function
this.error = function(field,rule)
{
switch(rule)
{
case 'alpha':
if(this.fieldvalue!="" && !this.alpha.test(this.fieldvalue))
this.show_error(field,'Please Enter Alpahbatic Value');
break;
case 'minlenght':
if(this.fieldvalue!="" && this.fieldvalue.length < this.minlength)
this.show_error(field,'Password should have atleast 6 character');
break;
case 'email':
if(this.fieldvalue!="" && ! this.email.test(this.fieldvalue))
this.show_error(field,'Please enter a valid email address');
break;
case 'numeric':
if(this.fieldvalue!="" && ! this.numeric.test(this.fieldvalue))
this.show_error(field,'Please enter numeric values');
break;
}
//SWITCH CASE END
}
this.chkblank = function(field){
//alert(field);
if(this.fieldvalue == "")
{
this.show_error(field,'This is a required field');
}else
{
this.hide_error(field);
}
}// END CHKBLNAK FUNCTION
// FUNCTION TO SHOW ALL TYPE OF VALIDATION ERROR (FIELD NAME, ERROR_MSG)
this.show_error= function(field,error_msg){
//alert(this.auto_error_div_generation);
if(this.auto_error_div_generation=== undefined){
$('#error_'+field).remove();
var error_div =$(document.createElement('div')).attr('id','error_'+field).html(error_msg).css('color','red').addClass(this.error_class);
error_div.insertAfter('#'+field);
$('#'+field).addClass(this.error_field_class);
}
else
{
$('#'+field+'_error').html(error_msg).addClass(this.error_class);
$('#'+field).addClass(this.error_field_class);
}
this.returns[this.current] = false;
}
// END SHOWERROR FUNCTION
this.hide_error = function(field)
{
($('#error_'+field).html()===null)?$('#'+field+'_error').html(''):$('#error_'+field).html('');
$('#'+field).removeClass(this.error_field_class);
this.returns[this.current] = true;
}
// FUNCTION FOR RADIO TYPE FIELD VALIDATION
this.radio_validation = function(radiodata)
{
var node_list = eval("document."+ form+ "."+radiodata[0]);
for(x=0; x<node_list.length; x++)
{
if(node_list[x].checked)
{
this.count++;
}
}
if(this.count==0)
{
this.show_error(radiodata[0],'This is required field');
}else
{
//alert(radiodata[0]);
$('#'+radiodata[0]+'_error').html('');
$('#error_'+radiodata[0]).html('');
this.returns[this.current] = true;
}
}
// END OF RADIO VALIDATION FUNCTION
/* FUNCTION FOR CHECK BOX VALIDATION */
this.chkbox_validation = function(fieldinfo)
{
var chkboxelement = eval("document."+form +'.'+fieldinfo[0]);
if(chkboxelement.checked){
$('#'+fieldinfo[0]+'_error').html('');
$('#error_'+fieldinfo[0]).html('');
//this.returns = true;
this.returns[this.current] = true;
}else
this.show_error(fieldinfo[0],'Please Accept Term And Condtion');
}
/* END OF CHECKBOX VALIDATION FUNCTION */
/* Only For array type checkbox validation */
this.chkbox_array_validation = function (fieldinfo)
{
this.counts = 0;
var field = fieldinfo[0].replace('[]',"");
var frm = eval('document.'+ form);
for(var i=0;i<frm.elements[fieldinfo[0]].length;i++)
{
if(frm.elements[fieldinfo[0]][i].checked){
this.counts++;}
}
//alert(this.count);
if(this.counts==0)
this.show_error(field,'This is required field');
else{
//$('#error_'+field).remove();
$('#'+field+'_error').html('');
}
}
/* End of chkbox_array_validation function */
this.fileupload_validation = function (fileinfo)
{
//alert(fileinfo[0]);
this.fieldvalue = $('#'+fileinfo[0]).val();
for($i=0;$i<fileinfo.length ;$i++)
{
if(fileinfo[$i]=='required')
{
this.chkblank(fileinfo[0]);
//fileinfo = fileinfo.splice(fileinfo.indexOf('required'));
//alert(fileinfo);
}else if($.isArray(fileinfo[$i]) && this.fieldvalue !="")
{
$filename = this.fieldvalue;
$filename = $filename.substr($filename.indexOf('.'));
if($.inArray($filename,fileinfo[$i])== -1)
{
this.show_error(fileinfo[0],'please upload only '+ fileinfo[$i] + ' type files');
}
else
{
$('#'+fileinfo[0]+'_error').html('');
$('#'+fileinfo[0]).html('');
this.returns[this.current] = true;
}
}
}
}
}
<!-- END OF FORM VALIDATION CLASS-->
|