Blog: Tag Archives: validation

Javascript – Basic Credit Card Validation

Posted On:

unction check(frmPayment){

var csv_number = /^\d{3}$/;
var letter = /^[a-zA-Z\s]+$/;
var today = new Date();
var start_date = new Date(frmPayment.numCardStartYear.options[frmPayment.numCardStartYear.selectedIndex].value, frmPayment.numCardStartMonth.options[frmPayment.numCardStartMonth.selectedIndex].value-1);
var expiry_date = new Date(frmPayment.numCardExpiryYear.options[frmPayment.numCardExpiryYear.selectedIndex].value, frmPayment.numCardExpiryMonth.options[frmPayment.numCardExpiryMonth.selectedIndex].value);
var CD_Card_Number = document.frmPayment.CD_Card_Number; //Card number
var CD_Card_Number = CD_Card_Number.replace(/ /g, "");
var msg = 'The credit card number you entered does not\n appear to be valid. Please check the number and try again.';

//########## Check Dates & Name ##########
		if(start_date > today){
			alert('Your credit card is not yet valid!');
			frmPayment.numCardStartYear.focus();
	return false;
	}
		if(expiry_date < today){
			alert('Your credit card has expired!');
			frmPayment.numCardExpiryYear.focus();
	return false;
	}

		if (!csv_number.test(frmPayment.CD_Card_SecurityNumber.value)){
			window.alert("Please enter the 3 digit security code on your card.");
			frmPayment.CD_Card_SecurityNumber.focus();
	return false;
	}

//########## Check Visa ##########
	if(CD_Card_Type.indexOf('Visa') !=-1){
		if ((CD_Card_Number.length == 13 || CD_Card_Number.length == 16) && (CD_Card_Number.substring(0,1) == 4)){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}
//########## Check Delta ##########
	if(CD_Card_Type.indexOf('Delta') !=-1){
		if ((CD_Card_Number.length==16) && (CD_Card_Number.match(/^(413733|413734|413735|413736|413737|4462|453978|453979|454313|454313|454432|454433|454434|454435|454742|456725|456726|456727|456728|456729|45673|456740|456741|456742|456743|456744|456745|46583|46584|46585|46586|46587|484409|484410|49096|49097|492181|492182|498824)/))){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}
//########## Check Solo ##########
	if(CD_Card_Type.indexOf('Solo') !=-1){
		if ((CD_Card_Number.length==16 || CD_Card_Number.length==18 || CD_Card_Number.length==19) && (CD_Card_Number.match(/63345|63346|63347|63348|63349|6767/))){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}
//########## Check Mastercard ##########
	if(CD_Card_Type.indexOf('Mastercard') !=-1){
		var firstdig=CD_Card_Number.substring(0,1);
		var seconddig=CD_Card_Number.substring(1,2);
		if ((CD_Card_Number.length == 16 || CD_Card_Number.length == 19) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}
//########## Check Diners ##########
	if(CD_Card_Type.indexOf('Diners') !=-1){
		firstdig = CD_Card_Number.substring(0,1);
		seconddig = CD_Card_Number.substring(1,2);
		if ((CD_Card_Number.length == 14 || CD_Card_Number.length == 17)  && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8))){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}
//########## Check Amex ##########
	if(CD_Card_Type.indexOf('Amex') !=-1){
		firstdig = CD_Card_Number.substring(0,1);
		seconddig = CD_Card_Number.substring(1,2);
		if (((CD_Card_Number.length == 15)  || (CD_Card_Number.length == 18)) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7))){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}
//########## Check Switch ##########
	if(CD_Card_Type.indexOf('Switch') !=-1){
		if ((CD_Card_Number.length==16) && (CD_Card_Number.match(/^(491101|491102|564182|633300|633302|633303|633304|633305|633306|633307|633308|633309|633310|633311|633312|633313|633314|633315|633316|633317|633318|633319|633320|633321|633322|633323|633324|633325|633326|633327|633328|633329|633330|633331|633332|633333|633334|633335|633336|633337|633338|633339|633340|633341|633342|633343|633344|633345|633346|633347|633348|633349|675900|675902|675903|675904|675906|675907|675908|675909|675910|675911|675912|675913|675914|675915|675916|675917|675919|675920|675921|675922|675923|675924|675925|675926|675927|675928|675929|675930|675931|675932|675933|675934|675935|675936|675937|675941|675942|675943|675944|675945|675946|675947|675948|675949|675963|675964|675965|675966|675967|675968|675969|675970|675971|675972|675973|675974|675975|675976|675977|675978|675979|675980|675981|675982|675983|675984|675985|675986|675987|675988|675989|675990|675991|675992|675993|675994|675995|675996|675997|675999)/))||
		(CD_Card_Number.length==18) && (CD_Card_Number.match(/^(675938|675939|675940|490302|490303|490304|490305|490306|490307|490308|490309|490335|490336|490337|490338|490339|491174|491175|491176|491177|491178|491179|491180|491181|491182)/))||
		(CD_Card_Number.length==19) && (CD_Card_Number.match(/^(4936|633301|675901|675905|675918|675950|675951|675952|675953|675954|675955|675956|675957|675958|675959|675960|675961|675962|675998)/))){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}
//########## Check JCB ##########
	if(CD_Card_Type.indexOf('jcb') !=-1){
		if ((CD_Card_Number.length==16) && (CD_Card_Number.match(/^(3088|3096|3112|3158|3337|3528)/))){
	return true
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
		}
	}
//########## Check Discover ##########
	if(CD_Card_Type.indexOf('Discover') !=-1){
		var firstfour = CD_Card_Number.substring(0, 4);
	if ((CD_Card_Number.length == 16) && (firstfour == 6011)){
	return true;
	}
		else{
			alert(msg);
			frmPayment.CD_Card_Number.focus();
	return false;
	}
}

}