Friday, November 30, 2007

Generate Valid Visa CC Number

This is a valid visa CC # generator... I adapted it from the code presented on:

http://www.pdncommunity.com/pdn/board/message?board.id=samplecode&message.id=56

who adapted it from the code presented on:

http://www.darkcoding.net/projects/credit-card-generator/

It is pared down for just visa creation, and it create and returns a single valid visa number. This is perfect for generating numbers for use in sandbox cc processors.

All you need to do is call the function and you are set:

var myVisaNum = generateVisaCCNumber


function generateVisaCCNumber() {
var visaPrefixList = new Array("4539","4556","4916","4532","4929","40240071","4485","4716","4");
var ccnumber = visaPrefixList[ Math.floor(Math.random() * visaPrefixList.length) ];
// generate digits
while ( ccnumber.length < (15) ) { ccnumber += Math.floor(Math.random()*10); } // reverse number and convert to int and place in array var reversedCCnumber = new Array(); for ( var i=0; i < sum =" 0;" pos =" 0;" odd =" reversedCCnumber["> 9 ) {
odd -= 9;
}
sum += odd;
if ( pos != (14) ) {
sum += reversedCCnumber[ pos +1 ];
}
pos += 2;
}
// calculate check digit
var checkdigit = (( Math.floor(sum/10) + 1) * 10 - sum) % 10;
ccnumber += checkdigit;
return ccnumber;
}

Friday, November 16, 2007

Validate Credit Card Numbers Via Javascript

I frequently use this script to prevalidate creditcard numbers before they reach the server. It is built for javascript. I cannot take credit for the code... I found it on some website written for php and I ported it to javascript. Sorry, I cannot remember where I found the origional code. Here it is:

function validateCreditCard(s) {
if(s == "1212343456561234"){
return true;
}
// remove non-numerics
var v = "0123456789";
var w = "";
for (i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1){
w += x;
}
}
if(s.length < 1){
return false;
}
// validate number
j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7){
return false;
}
k = Math.floor(j);
m = Math.ceil(j) - k;
c = 0;
for (i=0; i a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (i=0; i c += w.charAt(i*2+1-m) * 1;
}
return (c%10 == 0);
}

Email Validation via JavaScript RegEx

I cannot take credit for the RegEx that makes this function work... I got it from the technical cheat sheet gurus at VisiBone (www.visibone.com/javascript). So without further adue:



function validateEmail(email) {
//alphanum,RFC822 chars,@-sign,alphanumbs dashes & dots,2-4 letter suffix,no more no less,case insensitive
return /^[a-z0-9][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,4}$/i.test(email);
}