Return to Snippet

Revision: 36112
at November 18, 2010 04:56 by romans


Initial Code
$("form").validator({ 
	position: 'top left', 
	offset: [-5, 120],
	message: '<div></div>' // em element is the arrow
});
$.tools.validator.localize("en", {
	'[required]' 	: 'Required'
});
$("form").bind("onFail", function(e, errors)  {

	// we are only doing stuff when the form is submitted
	if (e.originalEvent.type == 'submit') {

		// loop through Error objects and add the border color
		$.each(errors, function()  {
			var input = this.input;
			input.css({borderColor: '#a71116'}).focus(function()  {
				input.css({borderColor: '#444'});
			});
		});
	}
});

/////

// function to create and read cookies:
function createCookie(name,value,days){if(days){var date=new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires="; expires="+date.toGMTString()}else var expires="";document.cookie=name+"="+value+expires+"; path=/"}
function readCookie(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length)}return null}

// Running this after the rest of the form fields have validated using jQuery Tools:
$("form").bind("onSuccess", function (e, erros) {
	// it will find the value of something like: <input type="text" id="dollar" />
	var dollaramount = $("input#dollar").val(); // "Capture a dollar amount from a form field"
	var D = (dollaramount * 1.062 + Math.random() * 10); // "Run a formula on that dollar amount"
	if (D < 300) D = 300; // "whichever is greater"
	createCookie("dollaramount", D.toFixed(2)); // Store the calculated value in a cookie after the rest of the form fields have validated using jQuery Tools
	return true;
});

// Retrieve the cookie value on a later form...
var dollaramount = readCookie("dollaramount");
$("p").first().html(dollaramount); // ... and display it on screen in an HTML paragraph


// Remove the comments (everything after double-slash) if you need.

Initial URL


Initial Description


Initial Title
Quick Javascript/jQuery functions

Initial Tags


Initial Language
JavaScript