Created
April 25, 2018 07:40
-
-
Save optimistanoop/13553f0db6799f9ed0d374e86126d9a9 to your computer and use it in GitHub Desktop.
web form validator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function trim(str) { | |
str = str.replace(/\s+/g, ' '); | |
str = str.replace(/^\s|\s$/g, ''); | |
return str; | |
} | |
function isValidPassword(str) { | |
let returnValue = false; | |
// check for null string | |
if(str === null) { | |
return returnValue; | |
} | |
str = trim(str); | |
// check for empty or for spaces | |
if((str === '') || (str.search(/\s/) !== -1)) { | |
return returnValue; | |
} | |
// check for password length | |
if(str.length >= 6) { | |
return true; | |
} else { | |
return returnValue; | |
} | |
} | |
function isValidEmailId(str) { | |
let returnValue = false; | |
// check for null string | |
if(str === null || str === undefined) { | |
return returnValue; | |
} | |
str = trim(str); | |
// check for empty or for spaces | |
if((str === '') || (str.search(/\s/) !== -1)) { | |
return returnValue; | |
} | |
// check for valid email pattern | |
let pEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; | |
let matches = str.match(pEmail); | |
if(matches === null || matches[0] !== str) { | |
return returnValue; | |
} | |
return true; | |
} | |
function isValidEmailIdMobile(str) { | |
if(this.isValidEmailId(str)) { | |
return true; | |
} | |
if(this.isValidMobileNumber(str,'91')) { | |
return true; | |
} | |
return false; | |
} | |
function isValidCountryCode(countryCode) { | |
let returnValue = false; | |
let matches; | |
if(countryCode === null || countryCode === undefined) { | |
return returnValue; | |
} | |
countryCode = trim(countryCode); | |
if((countryCode === '') || (countryCode.search(/\s/) !== -1)) { | |
return returnValue; | |
} | |
// allowing user to enter "+" charecter in other ISD code | |
if(countryCode.charAt(0) === '+') { | |
countryCode = countryCode.replace('+', ''); | |
} | |
matches = countryCode.match(/^[0-9]{1,4}$/); | |
if(matches === null || matches[0] !== countryCode) { | |
return returnValue; | |
} | |
return true; | |
} | |
function isValidMobileNumber(mobile, countryCode) { | |
let returnValue = false; | |
let matches; | |
if(countryCode && !isValidCountryCode(countryCode)) { | |
return returnValue; | |
} | |
if(countryCode === null && !isValidCountryCode(countryCode)) { | |
return returnValue; | |
} | |
// check for null string | |
if(mobile === null || mobile === undefined) { | |
return returnValue; | |
} | |
mobile = trim(mobile); | |
// check for empty or for spaces and length <= 10 | |
if((mobile === '') || (mobile.search(/\s/) !== -1) || (mobile.length > 10)) { | |
return returnValue; | |
} | |
// check mobile number | |
let validMobilePatterns = []; | |
validMobilePatterns['91'] = /^[9876][0-9]{9}$/; | |
validMobilePatterns['001'] = /^[0-9]{10}$/; | |
validMobilePatterns['44'] = /^[987][0-9]{9}$/; | |
validMobilePatterns['971'] = /^5[0-9]{8,9}$/; | |
let validMobileRegExp = validMobilePatterns[countryCode]; | |
if(!validMobileRegExp) { | |
validMobileRegExp = /^[1-9][0-9]{7,10}$/; | |
} | |
matches = mobile.match(validMobileRegExp); | |
if(matches === null || matches[0] !== mobile) { | |
return returnValue; | |
} | |
return true; | |
} | |
function isValidName(name, allowNumeric) { | |
if(name === null || name === undefined) { | |
return false; | |
} | |
name = trim(name); | |
if(name.toLowerCase() === 'name' || name.toLowerCase() === 'full name') { | |
return false; | |
} | |
if(name === '') { | |
return false; | |
} | |
let namePattern = /^[a-zA-Z\s\.]+$/; | |
if(allowNumeric) { | |
namePattern = /^[a-zA-Z\s\.0-9]+$/; | |
} | |
let matches = name.match(namePattern); | |
if(matches === null || (matches[0] !== name)) { | |
return false; | |
} | |
return true; | |
} | |
function isNumeric(input) { | |
if(input === null || input === undefined) { | |
return false; | |
} | |
let matches = input.match(/^[0-9]+$/); | |
return matches !== null && matches[0] === input; | |
} | |
function isDecimal(input) { | |
if(input === null || input === undefined) { | |
return false; | |
} | |
// upto 3 decimal | |
let matches = input.match(/^\d+\.\d{0,3}$/); | |
return matches !== null && matches[0] === input; | |
} | |
function isNumericDecimal(input) { | |
if(input === null || input === undefined) { | |
return false; | |
} | |
let matchNumeric = input.match(/^[0-9]+$/); | |
let matchDecimal = input.match(/^\d+\.\d{0,3}$/); | |
return (matchNumeric !== null && matchNumeric[0] === input) || (matchDecimal !== null && matchDecimal[0] === input); | |
} | |
function isEmpty(str) { | |
let returnValue = false; | |
if(str === null || str === undefined || str == null) { | |
return true; | |
} | |
str = str.trim(); | |
if(str === '') { | |
returnValue = true; | |
} | |
return returnValue; | |
} | |
function isNotEmpty(str) { | |
let returnValue = false; | |
if(str === null || str === undefined) { | |
return false; | |
} | |
str = str.trim(); | |
if(str !== '') { | |
returnValue = true; | |
} | |
return returnValue; | |
} | |
function isRegXMatching(regxStr, str) { | |
if(typeof regxStr === 'string') { | |
regxStr = new RegExp(regxStr); | |
} | |
let returnValue = true; | |
if(!isEmpty(str)) { | |
let matches = str.match(regxStr); | |
if(matches === null || (matches[0] !== str)) { | |
returnValue = false; | |
} | |
} else { | |
returnValue = false; | |
} | |
return returnValue; | |
} | |
function isValidDate(dateStr) { | |
let date = new Date(dateStr); | |
return !isNaN(date.valueOf()); | |
} | |
function dateComparision(date1, date2, condition) { | |
let returnValue = false; | |
if(!isEmpty(date1) && !isEmpty(date2) && !isEmpty(condition)) { | |
let d1 = new Date(date1); | |
let d2 = new Date(date2); | |
if (condition === 'less' && d1 < d2) { | |
returnValue = true; | |
} | |
else if (condition === 'greater' && d1 > d2) { | |
returnValue = true; | |
} | |
} | |
return returnValue; | |
} | |
function numberComparision(num1, num2, condition) { | |
let returnValue = false; | |
if(!isEmpty(num1) && !isEmpty(num2) && !isEmpty(condition)) { | |
if(isNumeric(num1) && isNumeric(num2)) { | |
if (condition === 'less' && num1 < num2) { | |
returnValue = true; | |
} | |
else if (condition === 'greater' && num1 > num2) { | |
returnValue = true; | |
} | |
} | |
} | |
return returnValue; | |
} | |
function isValidPercentNumber(num) { | |
let returnValue = false; | |
if(!isEmpty(num) && (isNumeric(num) || isDecimal(num) )) { | |
if(num >= 0 && num <= 100) { | |
returnValue = true; | |
} | |
} | |
return returnValue; | |
} | |
function isValidImage(imgFullPath) { | |
if(imgFullPath === null || imgFullPath === undefined) { | |
return false; | |
} | |
let allowedExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp']; | |
let fileExtension = imgFullPath.split('.').pop().toLowerCase(); | |
let isValidFile = false; | |
for(let index in allowedExtension) { | |
if(fileExtension === allowedExtension[index]) { | |
isValidFile = true; | |
break; | |
} | |
} | |
return isValidFile; | |
} | |
function isAlphaNumeric(input) { | |
if(input === null || input === undefined) { | |
return false; | |
} | |
let matches = input.match(/^[a-z0-9]+$/i); | |
return matches !== null && matches[0] === input; | |
} | |
function isValidImageSize(imgFullPath, imgSize) { | |
if(imgSize === undefined) { | |
imgSize = (2 * 1024 * 1024); | |
} | |
// TODO : Add logic here | |
} | |
function isValidString(input) { | |
let returnValue = true; | |
if(typeof(input) != undefined && input !='') { | |
let matches = input.match(/^[0-9]+$/); | |
returnValue = (matches != null ? false : true); | |
} | |
return returnValue; | |
} | |
module.exports = (function() { | |
return { | |
'dateComparision': dateComparision, | |
'isDecimal': isDecimal, | |
'isEmpty': isEmpty, | |
'isNotEmpty': isNotEmpty, | |
'isNumeric': isNumeric, | |
'isRegXMatching': isRegXMatching, | |
'isValidCountryCode': isValidCountryCode, | |
'isValidDate': isValidDate, | |
'isValidImage': isValidImage, | |
'isValidImageSize': isValidImageSize, | |
'isValidMobileNumber': isValidMobileNumber, | |
'isValidName': isValidName, | |
'isValidPercentNumber': isValidPercentNumber, | |
'numberComparision': numberComparision, | |
'isValidEmailId': isValidEmailId, | |
'isValidPassword': isValidPassword, | |
'isAlphaNumeric': isAlphaNumeric, | |
'trim': trim, | |
'isNumericDecimal':isNumericDecimal, | |
'isValidEmailIdMobile': isValidEmailIdMobile, | |
'isValidString': isValidString | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment