you are here: codestackercodes [RSS] → tag prototype [RSS]

explode function like php

String.prototype.explode = function(separator:String) {
	var string = this;
	var list = new Array();

	if (separator == null) return false;
	if (string == null) return false;

	var currentStringPosition = 0;
	while (currentStringPosition<string.length) {
		var nextIndex = string.indexOf(separator, currentStringPosition);
		if (nextIndex == -1) break;
		var word = string.slice(currentStringPosition, nextIndex);
		list.push(word);
		currentStringPosition = nextIndex+1;
	}
	if (list.length<1) {
		list.push(string);
	} else {
		list.push(string.slice(currentStringPosition, string.length));
	}
	return list;
}

var mystring = "test1 - test2";
exploded = mystring.explode(" - ");
trace (exploded[0]); // returns 'test1'

realtime email validation

requires prototype and script.aculo.us
demo: http://ajaxorized.com/examples/scriptaculous/email.html

var isValid = false;

validateEmail = function(e) {
	if(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(e.value)) {
		if(!isValid) {
			$(e).morph('border-color:#00FF00', {duration:.3});
			isValid = true;
		}
	} else {
		if(isValid) {
			$(e).morph('border-color:#FF0000', {duration:.3});						
			isValid = false;					
		}
	}	
}

// <input type = "email" id = "myinput" onKeyUp = "validateEmail(this)" />

[prototype] adding input:focus functionality to ie

Event.observe(window, 'load', function() { 
var fields = $$("input"); 
for (var i = 0; i fields[i].onfocus = function() {this.className += ' focused';} 
fields[i].onblur = function() {this.className = this.className.replace('focused', '');} 
} 
}); 

// in css, paste

// input:focus, /* works in FF without javascript */ 

// input.focused /* used by js */ 

// { background-color: #f7cd72; } 

passing extra parameters to prototype observer handlers

a simple listener: $('myForm').observe('submit', validateMyForm) dont support extra params in the function (in the case, validateMyForm. the solution:

$('myForm').observe('submit', function(event) {  
    validateMyForm(event, param1, param2);  
}); 
Displaying all 4 Code