Skip to content

Instantly share code, notes, and snippets.

@vinayakkulkarni
Created March 21, 2017 09:38
Show Gist options
  • Save vinayakkulkarni/b8de0ce6c2f61aaaa16aadb8669143b3 to your computer and use it in GitHub Desktop.
Save vinayakkulkarni/b8de0ce6c2f61aaaa16aadb8669143b3 to your computer and use it in GitHub Desktop.
Validation.js
/*
***************************************************************************************************
The following methods are implemented in this file
1. function Blanks_Validation(sValue)
2. function IsPure_Numeric(sValue)
3. function IsNumeric(sValue)
4. function IsValid_US_Phone_Or_Fax(sValue)
5. function IsAlpha_Numeric(sValue)
6. function IsValid_Email(sValue)
7. function IsValid_Name(sValue)
NOTE : All functions DO NOT ignore leading and trailing spaces. Hence any leading and trailing spaces will be
treated as a part of the string. If you want the function to ignore these spaces, pass the trimmed values
as arguments.
Description:
0. IsAmount()
Args: svalue (any string)
Returns: boolean value of true or false
Conditions: Returns false if the passed string is not a valid amount.
A valid amount can contain commas and one period and a dollar sign at the beginning. The following conditions return false:
1. If after the period, there are more than two digits.
2. If there are any commas after the period.
3. If there are any consecutive commas.
4. If the amount starts with a comma
1. Blanks_Validation()
Args: svalue (any string)
Returns: boolean value of true or false
Conditions: Returns false if the passed string is blank or contains only spaces
2. IsPure_Numeric()
Args : svalue (any string)
Returns : boolean true or false
Conditions : Returns true if the passed value is PURE numeric. Returns false if it encounters hyphens or leading/trailing
spaces or periods. So the following values will return false: "98.78","-67,"," 78","62 "
3. IsNumeric()
Args : svalue (any string)
Returns : boolean true or false
Conditions : Returns true if the passed value is numeric. A value is considered as Numeric even if it contains
periods, leading/trailing spaces or hyphens in the beginning(as in negative numbers).
4. IsValid_US_Phone_Or_Fax()
Args : sValue (any string)
Returns : boolean true or false
Conditions: Please see the function for the new rules.
5. IsAlpha_Numeric()
Args : sValue (any string)
Returns : boolean true or false
Conditions: Returns true if the string is made of only AlphaNumeric characters. Returns false for any wild card
Characters too.
6. IsValid_Email()
Args : sValue (any string)
Returns : boolean true or false
Conditions : Returns true if the passed value is a valid email id. Please read the function
to see the conditions that are being checked to validate the email id.
7. IsValid_Name()
Args : sValue (any string)
Returns : boolean true or false
Conditions : Returns false if the passed value has any leading spaces or starts with any other character other
than a-z (or A-Z).
8. stripAmount()
Args : sValue (any string)
Returns : returns String
Conditions : This method strips the amount field of all commas and $ signs and returns you a
a pure numeric value. It retains the period though. Use this to perform mathematical or
comparitive operations. Pass only those arguments that have passed the isAmount() test.
***************************************************************************************************
*/
function stripAmount(sValue)
{
stripChars = /^\$|,/g;
// remove "$" and ","
dollarSign = "$";
commaSign = ",";
sValue = sValue.replace(dollarSign, "");
sValue = sValue.replace(commaSign, "");
sValue = sValue.replace(stripChars, "");
return sValue;
}
function IsAmount(sValue)
{
// Check for valid characters in the amount field
let strValidChars = "0123456789.,$";
let strChar;
let isAmount = true;
for (i = 0; i < sValue.length && isAmount == true; i++)
{
strChar = sValue.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
isAmount = false;
}
}
// Check for valid characters in the amount field after the period
let dotIndex = sValue.indexOf(".");
if (dotIndex != -1)
{
let iLen = sValue.length;
let mant = sValue.substring(dotIndex + 1,iLen);
if ( (mant.length > 2) || (!IsNumeric(mant)) )
{
isAmount = false;
}
if (dotIndex != 0)
{
let nextChar = sValue.charAt(dotIndex -1);
if (nextChar == ",")
{
isAmount = false;
}
}
}
// Check to see if the numeric value starts with a comma.
let commaIndex = sValue.indexOf(",");
if (commaIndex == 0)
{
isAmount = false;
}
// Check to see if the numeric value contains consecutive commas
let twoCommaIndex = sValue.indexOf(",,");
if (twoCommaIndex != -1)
{
isAmount = false;
}
// Check to see if the numeric value has a dollar sign any where other than the beginning
let dollarIndex = sValue.indexOf("$");
if (dollarIndex > 0)
{
isAmount = false;
}
return isAmount;
}
function Blanks_Validation(sValue)
{
let iLen = sValue.length; // Stores the length of the passed parameter.
let pad = " "; // Variable to store a value composed of only spaces and equal to the length
// of the passed parameter.
for (i=1 ; i < iLen ; i++)
{
pad = pad + " ";
}
if ((sValue != "") && (sValue != pad))
{
return true;
}
else
{
return false;
}
}
//***************************************************************************************************
function IsPure_Numeric(sValue)
{
// A pure numeric number (for eg. A US Zip Code should not contain periods,braces,hyphens or spaces.
if (isNaN(sValue) || (sValue.indexOf(".") != -1 ) || (sValue.indexOf("-") != -1 ) || (!Blanks_Validation(sValue)) || (sValue.indexOf(" ") != -1 ))
{
return false;
}else return true;
}
//***************************************************************************************************
function IsNumeric(sValue)
{
// A numeric value can include decimal points,leading spaces and hyphens to indicate negative values
if (isNaN(sValue))
{
return false;
}else return true;
}
//***************************************************************************************************
function IsValid_US_Phone_Or_Fax(sValue)
{
// Updated to reflect new rules for phone numbers as per Julie Haga in the tracker item no.817
// submitted on Jan 4th 2001. The rule is reproduced below as is:
// -----The phone number entered into SVB.com can be any number of charters.
// This can include, but is not limited to, numbers, letters, dashes, parenthesis, etc.
// The only thing that would be invalid would be nothing entered or all spaces----"
// Hence, only checking to see if all are blanks. Modified on Jan 18th 2001 by Bipin
if (Blanks_Validation(sValue))
{
return true;
}
else
{
return false;
}
}
//***************************************************************************************************
/*function IsAlpha_Numeric(sValue)
{
let Alpha_Numeric_Set = /[a-zA-Z0-9]/;
if ((sValue.search(Alpha_Numeric_Set) >= 0) && (sValue.charAt(0) != " "))
{
return true;
}else
{
return false;
}
}*/
function IsAlpha_Numeric(sValue)
{
let Alpha_Numeric_Set = /[^\w\s]/;
if ((sValue.search(Alpha_Numeric_Set) < 0) && (sValue.charAt(0) != " "))
{
return true;
}else
{
return false;
}
}
//***************************************************************************************************
/*
* Function to check if the email is valid
* args sValue String email
* It 'exclude' checks 5 conditions:
a) characters that should not be in the address
b) characters that should not be at the start
c) & d) characters that shouldn't be together
e) there's not more than one '@'
'check' checks there's at least one '@', later followed by at least one '.'
'checkend' checks the address ends with a period followed by 2 or 3 alpha characters
*/
function IsValid_Email(sValue)
{
if (sValue.indexOf("@") == -1 )
{
return false;
}
sLeft = sValue.substring(0,sValue.indexOf("@"));
sRight = sValue.substring(sValue.indexOf("@"));
sLeft = sLeft.replace("_","");
sValue = sLeft + sRight;
let exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
let check=/@[\w\-]+\./;
let checkend=/\.[a-zA-Z]{2,3}$/;
if((sValue.search(exclude) != -1)||(sValue.search(check) == -1)||(sValue.search(checkend) == -1))
{
return false; //Incorrect email address
}
else
{
return true; //Email address format OK.
}
}
//***************************************************************************************************
function IsValid_Name(sValue)
{
let Alphabet_Set = /[a-zA-Z]/;
if ((sValue != "") && (sValue.charAt(0).search(Alphabet_Set) != -1))
{
return true;
}else
{
return false;
}
}// end of IsValid_Name(sValue)
//**********************************************************************************************************************
/*
* Function to trim leading and/or trailing spaces from a string
* arg The value to be trimmed..
* func = "left" for Ltrim(), "right" for RTrim() or "both" for Trim()
*/
function trim(arg,func)
{
let trimvalue = "";
arglen = arg.length;
if (arglen < 1) return trimvalue;
if (func == "left" || func == "both") {
i = 0;
pos = -1;
while (i < arglen) {
if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
pos = i;
break;
}
i++;
}// end of while
}
if (func == "right" || func == "both") {
let lastpos = -1;
i = arglen;
while (i >= 0) {
if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
lastpos = i;
break;
}
i--;
}// end of while
}
if (func == "left") {
trimvalue = arg.substring(pos,arglen-1);
}
if (func == "right") {
trimvalue = arg.substring(0,lastpos+1);
}
if (func == "both") {
trimvalue = arg.substring(pos,lastpos + 1);
}
return trimvalue;
}// end of trim()
//***************************************************************************************************
/*
* Function to remove spaces leading , Trailing and in between
*/
function removeChar(sValue, sChar)
{
while (!(sValue.search(sChar) < 0) ){
sValue= sValue.replace(sChar,"");
}
return sValue;
}
//***************************************************************************************************
/*
* Function to avoid submitting the page twice upon double click with in 2000 milli seconds
*/
let isSubmitted = false;
let timeStamp;
function SetTimeStamp()
{
if (!isSubmitted) {
timeStamp = new Date();
isSubmitted = true;
return true;
} else {
let timeStampNew = new Date();
let diff = timeStampNew - timeStamp;
if( diff > 2000 ) {
return true;
} else {
return false;
}
}
}
//***************************************************************************************************
/*
* Function which disables the submit button once the submit button is clicked
*/
let isSubmitted1 = false;
let timeStamp1;
function SetdoubleClick()
{
if (!isSubmitted1) {
timeStamp1 = new Date();
isSubmitted1 = true;
return true;
} else {
return false;
}
}
//*****************************************************************************************************
function isNumber(currentNumber) {
if(currentNumber.value.length == 0 || currentNumber.value.length != 4)
return false;
let value = "0123456789";
for (i=0; i < currentNumber.value.length; i++) {
x = currentNumber.value.charAt(i);
if (value.indexOf(x,0) == -1)
return false;
}
return true;
}
function isValidNumber(currentNumber) {
if(currentNumber.value.length == 0 ){
return false;
}else if(currentNumber.value.length != 19 ){
if(currentNumber.value.length != 16){
return false;
}
}
let value = "0123456789";
for (i=0; i < currentNumber.value.length; i++) {
x = currentNumber.value.charAt(i);
if (value.indexOf(x,0) == -1)
return false;
}
isLuhnCheckValid =luhn_check(currentNumber.value);
if(isLuhnCheckValid){
return true;
}else{
return false;
}
}
function isValidCvvNumber(currentNumber) {
if(currentNumber.value.length == 0 ||
(currentNumber.value.length != 3 ))
return false;
let value = "0123456789";
for (i=0; i < currentNumber.value.length; i++) {
x = currentNumber.value.charAt(i);
if (value.indexOf(x,0) == -1)
return false;
}
return true;
}
function luhn_check(number) {
let number=number.replace(/\D/g, '');
let number_length=number.length;
let parity=number_length % 2;
let total=0;
for (i=0; i < number_length; i++) {
let digit=number.charAt(i);
if (i % 2 == parity) {
digit=digit * 2;
if (digit > 9) {
digit=digit - 9;
}
}
total = total + parseInt(digit);
}
if (total % 10 == 0) {
return true;
} else {
return false;
}
}
// end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment