/* This small collection of functions are meant to be used in conjunction with an AWS Lambda (Node.js) function. How to use one of the functions is included at the very bottom as indicated by the header "EXAMPLE OF USING ONE OF THE FUNCTIONS IN YOUR MAIN AWS LAMBDA FUNCTION". */ /* Utility: Return randomly generated Uuid representation */ exports.utilityGenerateUuid = function() { var totalCharacters = 39; // length of number hash; in this case 0-39 = 40 characters var txtUuid = ""; do { var point = Math.floor(Math.random() * 10); if (txtUuid.length === 0 && point === 0) { do { point = Math.floor(Math.random() * 10); } while (point === 0); } txtUuid = txtUuid + point; } while ((txtUuid.length - 1) < totalCharacters); return txtUuid; }; /* Utility: Return randomly generated integer representation that may be useful for "atomic" primary key integer values. In this case the maximum physical "character" size is 16 digits: 9,999,999,999,999,999 */ exports.utilityGeneratePk = function() { var totalCharacters = 15; // length of number hash; in this case 0-15 = 16 characters var txtPk = ""; do { var point = Math.floor(Math.random() * 10); if (txtPk.length === 0 && point === 0) { do { point = Math.floor(Math.random() * 10); } while (point === 0); } txtPk = txtPk + point; } while ((txtPk.length - 1) < totalCharacters); return Number(txtPk); }; /* Utility: Return Long Timestamp in the format YYYY-MM-DD hh:mm:ss */ exports.utilityTimestamp = function() { var date = new Date(); var TimestampLong = ""; var TimestampYear = date.getFullYear(); var TimestampMonth = date.getMonth() + 1; var TimestampDay = date.getDate(); var TimestampHour = date.getHours(); var TimestampMinute = date.getMinutes(); var TimestampSecond = date.getSeconds(); /* Generate Timestamp */ TimestampLong = TimestampYear + "-"; if (TimestampMonth < 10) { TimestampLong = TimestampLong + "0" + TimestampMonth; } else { TimestampLong = TimestampLong + TimestampMonth; } TimestampLong = TimestampLong + "-"; if (TimestampDay < 10) { TimestampLong = TimestampLong + "0" + TimestampDay; } else { TimestampLong = TimestampLong + TimestampDay; } TimestampLong = TimestampLong + " "; if (TimestampHour < 10) { TimestampLong = TimestampLong + "0" + TimestampHour; } else { TimestampLong = TimestampLong + TimestampHour; } TimestampLong = TimestampLong + ":"; if (TimestampMinute < 10) { TimestampLong = TimestampLong + "0" + TimestampMinute; } else { TimestampLong = TimestampLong + TimestampMinute; } TimestampLong = TimestampLong + ":"; if (TimestampSecond < 10) { TimestampLong = TimestampLong + "0" + TimestampSecond; } else { TimestampLong = TimestampLong + TimestampSecond; } return TimestampLong; }; /* Validation: Email */ exports.validateEmail = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/)) { result = false; } else { result = true; } return result; }; /* Validation: Regularstring */ exports.validateRegularstring = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[a-zA-Z]+$/)) { result = false; } else { result = true; } return result; }; /* Validation: Metastring */ exports.validateMetastring = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[._;:&@=#%~,\!\$\*\^\?\(\)\-\+\[\]\{\}\/a-zA-Z0-9 ']+$/)) { result = false; } else { result = true; } return result; }; /* Validation: Wholenumber */ exports.validateWholenumber = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[0-9]+$/)) { result = false; } else { result = true; } return result; }; /* Validation: Longphonenumber */ exports.validateLongphonenumber = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[-()\+a-zA-Z0-9]+$/)) { result = false; } else { result = true; } return result; }; /* Validation: Companyname */ exports.validateCompanyname = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[._;:&@=#%~,\!\$\*\^\?\(\)\-\+\[\]\{\}\/a-zA-Z0-9 ']+$/)) { result = false; } else { result = true; } return result; }; /* Validation: Lastname */ exports.validateLastname = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[-.,a-zA-Z0-9 ']+$/)) { result = false; } else { result = true; } return result; }; /* Validation: Firstname */ exports.validateFirstname = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[-.a-zA-Z0-9 ']+$/)) { result = false; } else { result = true; } return result; }; /* Validation: IP Address */ exports.validateIp = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[._:\-a-zA-Z0-9]+$/)) { result = false; } else { result = true; } return result; }; /* Validation: Pwd */ exports.validatePwd = function(data, lengthmin, lengthmax) { var result = true; if (data === undefined) { result = false; } else if (data.length < lengthmin) { result = false; } else if (data.length > lengthmax) { result = false; } else if (!data.match(/^[._;:&@=#%~,\!\$\*\^\?\(\)\-\+\[\]\{\}\/a-zA-Z0-9 ']+$/)) { result = false; } else { result = true; } return result; }; /* EXAMPLE OF USING ONE OF THE FUNCTIONS IN YOUR MAIN AWS LAMBDA FUNCTION */ exports.handler = function(event, context) { /* Get email address sent to the function */ var UserEmail = event.UserEmail.trim(); /* Validate */ var validationCheck = true; if(!exports.validateEmail(UserEmail, 5, 45)) { /* Email address syntax was incorrect */ validationCheck = false; } /* Do Other Things */ };