sLvalidate v.1

So I had to write a validation script for sLquantity. The idea is simply that if any items
javascripthover edges to scroll
 
//////////////////////////////////////////////////////////////////////////////////////////////////
/* sLvalidate - v1.0 - 5:16 PM 20/03/2007
	@author: kc@slajax.com
	@validates entries for sLquantity before posting to update cookie
			var sLvalidateOpts = { 
					items:'select', // boxes to gather
					submitBtn: 'button', // id of submit button
					valid: true // this must be set to true for validation to occur
					};	
*/
//////////////////////////////////////////////////////////////////////////////////////////////////
 
var sLvalidate = new Class({
		initialize:function(options){
			this.setOptions(options);
			this.href(options);
		},
		setOptions:function(options){
			this.options = Object.extend( { }, options || { } );
		},
		gather:function(options){
			this.resetErrors(options);
			this.itemArr = [];
			this.itemObj = {};
			
		$$(options.items).each(function(el,i){
					this.itemArr[i] = $(el).options[$(el).selectedIndex].value;
					this.itemObj[i] = $(el);
				}.bind(this) );
				
			this.validate(options);
		},
		validate:function(options){				
			this.itemArr.each(function(el,i){ 				
				if ( this.itemArr[i] == 0 )
							this.throwError(options, i);							
				}.bind(this) );
 
		 if( options.valid ) document.forms[0].submit()
		},
		throwError:function(options, i){
			options.valid = false;
				this.itemObj[i].getParent().getParent().setStyle('background-color','#FF0000');
				this.itemObj[i].getParent().getParent().setStyle('padding','2px 0px 2px 2px');
				
		},
		resetErrors:function(options){
			options.valid = true;
			$$(options.items).each(function(el,i){
					$(el).getParent().getParent().setStyle('background-color','#FFFFFF');		
					$(el).getParent().getParent().setStyle('padding','0');
			}.bind(this) );
		},
		href:function(options){
					$(options.submitBtn).addEvent('click', function(){
								this.gather(options);	
						}.bind(this) );		
		}
});
 
var sLvalidateOpts = { 
			items:'select', // boxes to gather
			submitBtn: 'button', // id of submit button
			valid: true // this must be set to true for validation to occur
			};	
 
		var validate = new sLvalidate(sLvalidateOpts);
At some point I’ll extend this to cover all form validation needs.

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post

23 queries in 0.577 seconds | Code is Poetry | sLajax.com