Usefull Jquery snippets from https://tympanus.net/codrops/2010/01/05/some-useful-javascript-jquery-snippets/
// Validate email address
var email = 'info@tympanus.net'
if(!(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email)))
alert('Invalid Email');
// Disable right mouse click
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
//Run a function 5 times with intervals of 20 seconds
var count = 0;
function myFunction() {
count++;
if(count > 5) clearInterval(timeout);
//do something
}
var timeout = setInterval(myFunction, 20000);
//Check if an element exists
if ($("#elementid").length) {
//it does!
}
//Cancel an ajax request
var req = $.ajax({
type: "POST",
url: "someurl",
data: "id=1",
success: function(){
//something
}
});
//Cancel the Ajax Request
req.abort()
// Toggle all checkboxes
var tog = false; // or true if they are checked on load
$('a').click(function() {
$("input[type=checkbox]").attr("checked",!tog);
tog = !tog;
});
// Validate date of birth
$("#lda-form").submit(function(){
var day = $("#day").val();
var month = $("#month").val();
var year = $("#year").val();
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);
var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
if ((currdate - mydate) < 0){
alert("Sorry, only persons over the age of " + age + " may enter this site");
return false;
}
return true;
});
// Center an element on the screen
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
// Use the above function as:
$(element).center();
//Select all checkboxes
$(document).ready(function(){
$("#checkboxall").change(function(){
var checked_status = this.checked;
$("input[name=checkall]").attr("checked", checked_status);
});
});
// Get the current URL
$(document).ready(function() {
var pathname = window.location.pathname;
});
// Check if and which key was pressed
$(function() {
$(document).keypress(function(e){
switch(e.which){
// "ENTER"
case 13:
alert('enter pressed');
break;
// "s"
case 115:
alert('s pressed');
break;
(...)
}
});
});
// How to hide all children div except a specific one with jQuery?
$('#target div:not(#exclude)').hide();
//or
$('#target').children().filter(':not(#exclude)').hide();
// Get IP address
$.getJSON("http://jsonip.appspot.com?callback=?",function(data){
alert( "Your ip: " + data.ip);
// Count number of textarea lines
var text = $("#textareaId").val();
var lines = text.split(/\r|\r\n|\n/);
alert(lines.length);
});
//Validate Credit Card
function isCreditCard( CC ){
if (CC.length > 19)
return (false);
sum = 0; mul = 1; l = CC.length;
for (i = 0; i < l; i++){
digit = CC.substring(l-i-1,l-i);
tproduct = parseInt(digit ,10)*mul;
if (tproduct >= 10)
sum += (tproduct % 10) + 1;
else
sum += tproduct;
if (mul == 1)
mul++;
else
mul–;
}
if ((sum % 10) == 0)
return (true);
else
return (false);
}
// Distinguish left and right mouse click
$("#element").live('click', function(e) {
if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) {
alert("Left Button");
}
else if(e.button == 2)
alert("Right Button");
});
// How to get the native image size
var img = $('#imageid');
var theImage = new Image();
theImage.src = img.attr("src");
alert("Width: " + theImage.width);
alert("Height: " + theImage.height);