Skip to content

Instantly share code, notes, and snippets.

View mildfuzz's full-sized avatar

John Farrow mildfuzz

View GitHub Profile
@mildfuzz
mildfuzz / max_length.js
Created November 2, 2012 11:30
Ensures a max length is adhered to in older browsers
var textareas = document.getElementById('user_summary');
for (var i = textareas.length; i--;) {
if (textareas[i].getAttribute('maxlength') && !textareas[i].maxlength) {
var max = textareas[i].getAttribute('maxlength');
textareas[i].onkeypress = function(event) {
var k = event ? event.which : window.event.keyCode;
if(this.value.length >= max) if(k>46 && k<112 || k>123) return false;
}
}
@mildfuzz
mildfuzz / Load jQuery bookmarklet
Created October 19, 2012 09:24
Load the lastest 1.x version of jQuery into any webpage
javascript:(function()%7Bif(typeof%20jQuery%20%3D%3D%20'function')%7Bdelete%20jQuery%3B%7Dvar%20newscript%20%3D%20document.createElement('script')%3Bnewscript.type%20%3D%20'text%2Fjavascript'%3Bnewscript.async%20%3D%20true%3Bnewscript.src%20%3D%20'https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fjquery%2F1%2Fjquery.min.js'%3B(document.getElementsByTagName('head')%5B0%5D%7C%7Cdocument.getElementsByTagName('body')%5B0%5D).appendChild(newscript)%3B%7D)()%3B
$.fn.validateImageUpload = function(size){
//Accepts form file input fields $('input[type=file]').validateImageUpload();
//Also accepts size validation (in K), default is 700. Set to false in order to skip check
if(typeof size != "number" && size%1 != 0){size = 700}
var validate = function(x){
var files = $(x).get(0).files, that = x;
for (i = 0; i < files.length; i++){
if(!files[i].type.match(/(gif|jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)/)){
alert('Must be an image file.');
@mildfuzz
mildfuzz / getobjectbyattribute.js
Created October 17, 2012 14:30
getObjectByAttribute
Array.prototype.getObjectByAttribute = function(value, attribute){
var catcher = [], i = 0;
while(!catcher && this[i]){
if(this[i].attribute == value){
catcher.push(this[i]);
}
i++;
}
return catcher.length == 1 ? catcher[0] : catcher.length ? catcher : false;
}
@mildfuzz
mildfuzz / toggleHTML.js
Created October 16, 2012 12:46
toggleHTML.js
$.fn.toggleHTML = function(a,b){
//toggles two HTML values of given object, defaults to A unless equal to A
$(this).html(($(this).html() == a ? b : a));
}
@mildfuzz
mildfuzz / isCVV.js
Created October 11, 2012 09:40
Detect if a string input is a valid CVV Credit Card Security number
String.prototype.isCVV = function(){
return (this.length >= 3 && this.length <=4) && Number(this) ? true : false
}
@mildfuzz
mildfuzz / isCreditCard.js
Created October 11, 2012 09:14
isCreditCard.js
String.prototype.isCreditCard = function()
{
//Credit - https://sites.google.com/site/abapexamples/javascript/luhn-validation
var luhnArr = [[0,2,4,6,8,1,3,5,7,9],[0,1,2,3,4,5,6,7,8,9]], sum = 0;
this.replace(/\D+/g,"").replace(/[\d]/g, function(c, p, o){
sum += luhnArr[ (o.length-p)&1 ][ parseInt(c,10) ];
});
return (sum%10 === 0) && (sum > 0);
};
@mildfuzz
mildfuzz / facebook_test_user.js
Created October 3, 2012 14:00
Fetch Test Facebook User
var APP_ID = prompt("Input your App Id");
var APP_SECRET = prompt("Input your App Secret");
$.ajax({
url: "https://graph.facebook.com/oauth/access_token?client_id="+APP_ID+"&client_secret="+APP_SECRET+"&grant_type=client_credentials",
type: "GET",
success: function(data){
window.location = "https://graph.facebook.com/"+APP_ID+"/accounts/test-users?installed=false&locale=en_UK&permissions=read_stream&method=post&access_token="+data.replace('access_token=','');
}
})
@mildfuzz
mildfuzz / plugins.js
Created September 21, 2012 15:12
My Library
/*
A collection of useful scripts
*/
//Collect all form data into a string
$.fn.getFormData = function(){
var str = "";
var areas = $(this).find('input').not('input[type=submit]').not('input[type=button]').add($(this).find('textarea')).add($(this).find('select'));
areas.each(function(i){
@mildfuzz
mildfuzz / geolocal.js
Created October 24, 2011 12:30
geoLocal JS
var scripts = {};
var w3cGeo = {};
w3cGeo.api = '95b5c8ffb1d0891fe5a1eed66ce0d2d5';
var gLocalSearch = new GlocalSearch();
google.load("search", "1");
google.load("maps", "2", {"callback" : w3cGeo.mapsLoaded});