国外客户定制商场的验证部分JS代码

大猪 发布于 2010/06/21 20:58
阅读 167
收藏 3
var iPassword = "The password is invalid. \n"
var iOr = " or "
var iPasswordCharacters = "Password must be at least 5 characters long. \n"
var iPasswordConfirm = "The verify password is invalid. \n"
var iPasswordHint = "Password hint cannot be the same as password. \n"
var iExistingPassword = "The Existing Password is invalid. \n"
var iNewPassword = "The New Password is invalid. \n"
var iFirstName = "First name is required. \n"
var iLastName = "Last name is required. \n"
var iAddress = "A valid street address is required. \n"
// var iPOBox = "The address is not PO Box (like PO Box). \n"
var iPOBox = "Cannot ship to a PO Box. Use a street address instead. \n"
var iCity = "The city field cannot be empty. \n"
var iUSZIPCode = "ZIP code field must be a 5 digit ZIP Code (like 94043). \n"
var iUSPhone = "Phone number is invalid.  Use a 10 digit phone number such as 415-555-1212. \n"
var iEmail = "Email address is invalid. \n"
var iCreditCard = "The credit card number is invalid. \n"
var iCreditCardDate = "The credit card expiration date is invalid. \n"

var reInteger = /^\d+$/;
var ZIPCodeDelimiters = "-";
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var digitsInUSPhoneNumber = 10;
var phoneNumberDelimiters = "()- ";

function checkemail (email) {
	// a very simple email validation checking. 
	// you can add more complex email checking if it helps 
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) 
      	return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) 
	    	return false;
      }// if
      return true;
    }
	return false;
}

function checkspace (checkstr) {
	var str = '';
	for(i = 0; i < checkstr.length; i++) {
		str = str + ' ';
	}
	return (str == checkstr);
}

function checklen (str) {
	var i,sum;
	sum=0;
	for(i=0;i<str.length;i++)
	{
		if ((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255))
			sum=sum+1;
		else
			sum=sum+2;
		}
	return sum;
}

// strings to check from Greg's program
// "P.O. B"
// "P.o.B"
// "P.O B"
// "PO. B"
// "P O B"
// "PO B"
// "P.0. B"
// "P0 B"
function checkpobox (str) {

    if (str.indexOf("P.O. B") != -1) return true;
    if (str.indexOf("P.O.B") != -1) return true;
    if (str.indexOf("P.O B") != -1) return true;
    if (str.indexOf("P O B") != -1) return true;
    if (str.indexOf("PO B") != -1) return true;
    if (str.indexOf("POBox") != -1) return true;
    if (str.indexOf("POB") != -1) return true;
    if (str.indexOf("PO#") != -1) return true;
    if (str.indexOf("PO #") != -1) return true;
    
    if (str.indexOf("p.o. b") != -1) return true;
    if (str.indexOf("p.o.b") != -1) return true;
    if (str.indexOf("p.o b") != -1) return true;
    if (str.indexOf("p o b") != -1) return true;
    if (str.indexOf("po b") != -1) return true;
    if (str.indexOf("pob") != -1) return true;
    if (str.indexOf("pobox") != -1) return true;
    if (str.indexOf("po#") != -1) return true;
    if (str.indexOf("po #") != -1) return true;

    // now with 0's for them sneaky folks
    if (str.indexOf("p.0. b") != -1) return true;
    if (str.indexOf("p.0.b") != -1) return true;
    if (str.indexOf("p.0 b") != -1) return true;
    if (str.indexOf("p 0 b") != -1) return true;
    if (str.indexOf("p0 b") != -1) return true;
    if (str.indexOf("p0box") != -1) return true;
    if (str.indexOf("p0#") != -1) return true;
    if (str.indexOf("p0 #") != -1) return true;
    
    if (str.indexOf("P.0. B") != -1) return true;
    if (str.indexOf("P.0.B") != -1) return true;
    if (str.indexOf("P.0 B") != -1) return true;
    if (str.indexOf("P 0 B") != -1) return true;
    if (str.indexOf("P0 B") != -1) return true;
    if (str.indexOf("P0Box") != -1) return true;
    if (str.indexOf("P0#") != -1) return true;
    if (str.indexOf("P0 #") != -1) return true;
    
    return false;
}


// checkZIPCode (TEXTFIELD theField)
// Check that string theField.value is a valid ZIP code.
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkZIPCode (str) {
	var normalizedZIP = stripCharsInBag(str, ZIPCodeDelimiters);
	if (!isZIPCode(normalizedZIP))
		return false;
	else return true;
}

// isZIPCode (STRING s)
// isZIPCode returns true if string s is a valid 
// U.S. ZIP code.  Must be 5 or 9 digits only.
function isZIPCode (s) {
	return (isInteger(s) && ((s.length == digitsInZIPCode1) || (s.length == digitsInZIPCode2)));
}

// isInteger test
function isInteger (s) {
    return reInteger.test(s);
}

// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag) {
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.x
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isIntegerInRange (s, a, b)
{   if (checkspace(s)) return false;
    if (!isInteger(s)) return false;
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}


// checkUSPhone (TEXTFIELD theField)
// Check that string theField.value is a valid US Phone.
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function checkUSPhone (str) {
	var normalizedPhone = stripCharsInBag(str, phoneNumberDelimiters)
	if (!isUSPhoneNumber(normalizedPhone))
		return false;
	else return true;
}

// isUSPhoneNumber (STRING s)
// isUSPhoneNumber returns true if string s is a valid U.S. Phone
// Number.  Must be 10 digits.
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isUSPhoneNumber (s) {
	return (isInteger(s) && s.length == digitsInUSPhoneNumber);
}

// Validate credit card info.
function checkCreditCard (CardType, CardNumbers, theField) {
	var creditCardDelimiters = " "
	var normalizedCCN = stripCharsInBag(CardNumbers, creditCardDelimiters)
    if (!isCardMatch(CardType, normalizedCCN))
    	return false;
    else {
    	theField.value = normalizedCCN
    	return true
    }
}

// checkDate (year, month)
// Check that yearField.value, monthField.value
// form a valid date.
function checkDate (year, month) {

	var intYear = year;
    var intTestMonth = month;
    var intDay = "01";
    if(intTestMonth >= 10)	{
		var intMonth = intTestMonth;
	} else {
		var intMonth = "0"+intTestMonth;
	}
	
    nowDate = new Date();
    var nowYear = nowDate.getFullYear();
    var nowTestMonth = nowDate.getMonth()+1;
    var nowDay = "01";
    if(nowTestMonth >= 10)	{
		var nowMonth = nowTestMonth;
	} else {
		var nowMonth = "0"+nowTestMonth;
	}
	
	//if(nowIntDay >= 10) {
		//var nowDay = nowIntDay;
	//} else {
		//var nowDay = "0" + nowIntDay;
	//}
	date1=new Date(intMonth+"/"+intDay+"/"+intYear);
    date2=new Date(nowMonth+"/"+nowDay+"/"+nowYear);
    
    if (date1>=date2){
       return true;
    }else return false
}

/*  ================================================================
    FUNCTION:  isCardMatch()
    INPUT:    cardType - a string representing the credit card type
	      cardNumber - a string representing a credit card number
    RETURNS:  true, if the credit card number is valid for the particular
	      credit card type given in "cardType".		    
	      false, otherwise
    ================================================================ */
function isCardMatch (cardType, cardNumber) {
	var doesMatch = true;
	if ((cardType == "Visa") && (!isVisa(cardNumber)))
		doesMatch = false;
	if ((cardType == "MasterCard") && (!isMasterCard(cardNumber)))
		doesMatch = false;
	if ((cardType == "AmericanExpress") && (!isAmericanExpress(cardNumber)))
		doesMatch = false;
	if ((cardType == "Discover") && (!isDiscover(cardNumber)))
		doesMatch = false;
	//if ((cardType == "JCB") && (!isJCB(cardNumber)))
		//doesMatch = false;
	//if ((cardType == "DinersClub") && (!isDinersClub(cardNumber)))
		//doesMatch = false;
	//if ((cardType == "CarteBlanche") && (!isCarteBlanche(cardNumber)))
		//doesMatch = false;
	//if ((cardType == "EnRoute") && (!isEnRoute(cardNumber)))
		//doesMatch = false;
	return doesMatch;
}

/*  ================================================================
	Credit card verification functions
    Originally included as Starter Application 1.0.0 in LivePayment.
    20 Feb 1997 modified by egk:
           changed naming convention to initial lowercase
                  (isMasterCard instead of IsMasterCard, etc.)
           changed isCC to isCreditCard
           retained functions named with older conventions from
                  LivePayment as stub functions for backward 
                  compatibility only
           added "AMERICANEXPRESS" as equivalent of "AMEX" 
                  for naming consistency 
    ================================================================ */


/*  ================================================================
    FUNCTION:		isCreditCard(st)
    INPUT:			st - a string representing a credit card number
    RETURNS:		true, if the credit card number passes the Luhn Mod-10 test.
    				false, otherwise
    ================================================================ */
function isCreditCard(st) {
	// Encoding only works on cards with less than 19 digits
	if (st.length > 19)
		return (false);
	sum = 0; mul = 1; l = st.length;
	for (i = 0; i < l; i++) {
		digit = st.substring(l-i-1,l-i);
		tproduct = parseInt(digit ,10)*mul;
		if (tproduct >= 10)
			sum += (tproduct % 10) + 1;
		else
			sum += tproduct;
		if (mul == 1)
			mul++;
		else
			mul--;
	}
	// Uncomment the following line to help create credit card numbers
	// 1. Create a dummy number with a 0 as the last digit
	// 2. Examine the sum written out
	// 3. Replace the last digit with the difference between the sum and
	// the next multiple of 10.
	// document.writeln("<BR>Sum = ",sum,"<BR>");
	// alert("Sum = " + sum);
	if ((sum % 10) == 0)
		return (true);
	else
		return (false);
}

/*  ================================================================
    FUNCTION:		isVisa()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid VISA number.
    				false, otherwise
    Sample number:	4111 1111 1111 1111 (16 digits)
    ================================================================ */
function isVisa(cc) {
	if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4))
		return isCreditCard(cc);
	return false;
}

/*  ================================================================
    FUNCTION:		isMasterCard()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid MasterCard number.
    				false, otherwise
    Sample number:	5500 0000 0000 0004 (16 digits)
    ================================================================ */
function isMasterCard(cc) {
	firstdig = cc.substring(0,1);
	seconddig = cc.substring(1,2);
	if ((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
		return isCreditCard(cc);
	return false;

}

/*  ================================================================
    FUNCTION:		isAmericanExpress()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid American Express number.
    				false, otherwise
    Sample number:	340000000000009 (15 digits)
    ================================================================ */
function isAmericanExpress(cc) {
	firstdig = cc.substring(0,1);
	seconddig = cc.substring(1,2);
	if ((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
		return isCreditCard(cc);
	return false;
}

/*  ================================================================
    FUNCTION:		isDinersClub()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid Diner's Club number. 
    				false, otherwise
    Sample number:	30000000000004 (14 digits)
    ================================================================ */
function isDinersClub(cc) {
	firstdig = cc.substring(0,1);
	seconddig = cc.substring(1,2);
	if ((cc.length == 14) && (firstdig == 3) 
	&& ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
		return isCreditCard(cc);
	return false;
}

/*  ================================================================
    FUNCTION:		isCarteBlanche()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid Carte Blanche number.
    				false, otherwise
    ================================================================ */
function isCarteBlanche(cc) {
	return isDinersClub(cc);
}

/*  ================================================================
    FUNCTION:		isDiscover()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid Discover card number.
    				false, otherwise
    Sample number:	6011000000000004 (16 digits)
    ================================================================ */
function isDiscover(cc) {
	first4digs = cc.substring(0,4);
	if ((cc.length == 16) && (first4digs == "6011"))
		return isCreditCard(cc);
	return false;
}

/*  ================================================================
    FUNCTION:		isEnRoute()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid enRoute card number.
    				false, otherwise
    Sample number:	201400000000009 (15 digits)
    ================================================================ */
function isEnRoute(cc) {
	first4digs = cc.substring(0,4);
	if ((cc.length == 15) && ((first4digs == "2014") || (first4digs == "2149")))
		return isCreditCard(cc);
	return false;
}

/*  ================================================================
    FUNCTION:		isJCB()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is a valid JCB card number.
    				false, otherwise
    ================================================================ */
function isJCB(cc) {
	first4digs = cc.substring(0,4);
	if ((cc.length == 16) 
	&& ((first4digs == "3088") || (first4digs == "3096") 
	|| (first4digs == "3112") || (first4digs == "3158") 
	|| (first4digs == "3337") || (first4digs == "3528")))
		return isCreditCard(cc);
	return false;
}

/*  ================================================================
    FUNCTION:		isAnyCard()
    INPUT:			cc - a string representing a credit card number
    RETURNS:		true, if the credit card number is any valid credit card 
    				number for any of the accepted card types.
    				false, otherwise
    ================================================================ */
function isAnyCard(cc) {
	if (!isCreditCard(cc))
		return false;
	if (!isMasterCard(cc) 
	&& !isVisa(cc)
	&& !isAmericanExpress(cc) 
	&& !isDinersClub(cc) 
	&& !isDiscover(cc) 
	&& !isEnRoute(cc) 
	&& !isJCB(cc)) {
		return false;
	}
	return true;
}

 

加载中
0
kevinwu
kevinwu

不错,收藏!

返回顶部
顶部