Saturday, September 4, 2010
 

JQuery simple input validation plugin

Just simple jQuery form input validation plugin witch uses regular expressions for validations. Main different from others validations plugins that this plugins does validate in real time.

Plugin code:
 

jQuery.fn.rval = function(regExp)
{
	regularExpression = regExp;
	valueCurrent = "";
	blockInput = false;
			
	this.keypress(function(e){
	    if (blockInput == false)
		{
		      valueCurrent = this.value;
		      blockInput = true;
		}
	})
	
	this.keyup(
		function(e)
		{		   
		    instance = this;
		    var allOk = false;		    
		    $.each(
            regularExpression,
            function( intIndex, objValue ){ 
                    var re = new RegExp(objValue);                                        
        			if((re.exec(instance.value) || instance.value == '') && allOk == false)
        			{
        			    allOk = true;        			    		    
        			} 
                 }
            );            
            if (allOk != true) {
        			     instance.value = valueCurrent;	
        	}        			 
            blockInput = false; 
		}
	)
	
	return this;
}

Plugin can be called using syntax like this.

$("#only_int_float").rval([/^([0-9]{0,6})$/,/^[0-9]{1,6}[\.|,]{1}(|[0-9]{0,2})$/]);

In example above i accept only integer and float numbers. Decimal part can be separated by "," or by ".".
Example: http://remdex.info/examples/validator/index.html
 

Back »

Comments: 0

Leave a reply »

 
  • Leave a Reply
    Your gravatar
    Your Name
     
     
     
     
     
 
About Remdex site

Simple site for simple peoples.

Get in touch

E-mail: remdex@gmail.com