Quick Javascript/jQuery functions


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $("form").validator({
  2. position: 'top left',
  3. offset: [-5, 120],
  4. message: '<div></div>' // em element is the arrow
  5. });
  6. $.tools.validator.localize("en", {
  7. '[required]' : 'Required'
  8. });
  9. $("form").bind("onFail", function(e, errors) {
  10.  
  11. // we are only doing stuff when the form is submitted
  12. if (e.originalEvent.type == 'submit') {
  13.  
  14. // loop through Error objects and add the border color
  15. $.each(errors, function() {
  16. var input = this.input;
  17. input.css({borderColor: '#a71116'}).focus(function() {
  18. input.css({borderColor: '#444'});
  19. });
  20. });
  21. }
  22. });
  23.  
  24. /////
  25.  
  26. // function to create and read cookies:
  27. 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=/"}
  28. 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}
  29.  
  30. // Running this after the rest of the form fields have validated using jQuery Tools:
  31. $("form").bind("onSuccess", function (e, erros) {
  32. // it will find the value of something like: <input type="text" id="dollar" />
  33. var dollaramount = $("input#dollar").val(); // "Capture a dollar amount from a form field"
  34. var D = (dollaramount * 1.062 + Math.random() * 10); // "Run a formula on that dollar amount"
  35. if (D < 300) D = 300; // "whichever is greater"
  36. createCookie("dollaramount", D.toFixed(2)); // Store the calculated value in a cookie after the rest of the form fields have validated using jQuery Tools
  37. return true;
  38. });
  39.  
  40. // Retrieve the cookie value on a later form...
  41. var dollaramount = readCookie("dollaramount");
  42. $("p").first().html(dollaramount); // ... and display it on screen in an HTML paragraph
  43.  
  44.  
  45. // Remove the comments (everything after double-slash) if you need.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.