Skip to content

Instantly share code, notes, and snippets.

Avatar

Asim Mittal asimmittal

View GitHub Profile
@asimmittal
asimmittal / gist:e047901590325639fb1e
Last active Feb 12, 2018
Access values in a Google sheet
View gist:e047901590325639fb1e
/********************************************************
* grab all the rows from the sheet and POST to the DB
********************************************************/
function grabRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
//these labels will be used to mark the fields in
View phoneField_1.js
var filter = [];
//since we're looking for phone numbers, we need
//to allow digits 0 - 9 (they can come from either
//the numeric keys or the numpad)
const keypadZero = 48;
const numpadZero = 96;
//add key codes for digits 0 - 9 into this filter
for(var i = 0; i <= 9; i++){
View phoneField_2.js
/*******************************************************
* formatPhoneText
* returns a string that is in XXX-XXX-XXXX format
*******************************************************/
function formatPhoneText(value){
value = this.replaceAll(value.trim(),"-","");
if(value.length > 3 && value.length <= 6)
value = value.slice(0,3) + "-" + value.slice(3);
else if(value.length > 6)
View phoneField_3.js
/*******************************************************
* validatePhone
* return true if the string 'p' is a valid phone
*******************************************************/
function validatePhone(p){
var phoneRe = /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/;
var digits = p.replace(/\D/g, "");
return phoneRe.test(digits);
}
View phoneField_4.js
/*******************************************************
* onKeyUp(e)
* when a key is pressed up, grab the contents in that
* input field, format them in line with XXX-XXX-XXXX
* format and validate if the text is infact a complete
* phone number. Adjust the border color based on the
* result of that validation
*******************************************************/
function onKeyUp(e){
var input = e.target;
View phoneField_5.js
/*******************************************************
* setupPhoneFields
* Now let's rig up all the fields with the specified
* 'className' to work like phone number input fields
*******************************************************/
function setupPhoneFields(className){
var lstPhoneFields = document.getElementsByClassName(className);
for(var i=0; i < lstPhoneFields.length; i++){
var input = lstPhoneFields[i];
@asimmittal
asimmittal / sequentialLoad.js
Created Mar 8, 2017
Sequentially load JS files
View sequentialLoad.js
(function(){
//three JS files that need to be loaded one after the other
var libs = [
'https://code.jquery.com/jquery-3.1.1.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/underscore.string/3.3.4/underscore.string.js'
];
var injectLibFromStack = function(){
View jqueryGetGoogle.js
$.get("http://google.com",function(googPageHtml){
console.log("-->",googPageHtml)
});
View basicPromise.js
var promise = new Promise(function(resolve,reject){
/*
* Do things here (synchronous or asynchronous)
* some examples:
* -- run loops
* -- perform ajax requests
* -- count sheep!
*/
View promiseChain.js
/*
* ajaxGET
* -- Custom wrapper around the Jquery GET method
* -- when the request is complete, it returns the response using
* the 'finish' callback
*/
var ajaxGET = function(url, finish){
$.get(url).done(function(data){
finish(data);
});