1. Problem : Write validators for Visa Credit Cards
Visa credit card numbers are composed of 16 digits and they start with digit 4
answer :
var patt = /^4[0-9]{15}$/;
Explanation :
a. 4 means the string starts with '4'
b. [0-9]{15} means any digit between 0 to 9 should appear 15 times
New Visa cards have 16 digits while the old cards used to have 13 digits. Let's write a REGEXP which captures both the old and new valid cards.
var patt = /^4[0-9]{12}([0-9]{3})?$/;
Explanation :
a. 4 means the card number must start with '4'
b. [0-9]{12} means, any digit between 0 to 9 should appear 12 times. So, up this point, already 13 digits have been tested for old cards
c. ([0-9]{3})? means, a sequence of 3 digits may or may not appear. ? means either zero or one occurence. This point would validate if this is a new 16 digits card number.
2. Problem : Write validators for MasterCard Credit Cards
Visa credit card numbers are composed of 16 digits and they start with numbers 51 thorugh 55
answer :
var patt = /^5[1-5][0-9]{14}$/;
Explanation :
a. 5[1-5] means the string starts with '5' and then anything between 1 and 5. This ensures the number always have 51,52,53,54 or 55 at the beginning.
b. [0-9]{14} means any digit between 0 to 9 should appear 14 times
3. Problem : Write validators for AMEX [American Express] Credit Cards
American Express credit card numbers are composed of 15 digits and they start with numbers 34 or 37
answer :
var patt = /^(34|37)[0-9]{13}$/;
Explanation :
a. (34|37) means the number must start with '34' or '37'
b. [0-9]{13} means any digit between 0 to 9 should appear 13 times
We can write the above REGEXP slight differently as shown below.
var patt = /^3[47][0-9]{13}$/;
Explanation :
a. 3 means the number must start with '3'
b. [47] means the next digit should be either '4' or '7'
c. [0-9]{13} means any digit between 0 to 9 should appear 13 times
Check the next part of this article here
Visa credit card numbers are composed of 16 digits and they start with digit 4
answer :
var patt = /^4[0-9]{15}$/;
Explanation :
a. 4 means the string starts with '4'
b. [0-9]{15} means any digit between 0 to 9 should appear 15 times
New Visa cards have 16 digits while the old cards used to have 13 digits. Let's write a REGEXP which captures both the old and new valid cards.
var patt = /^4[0-9]{12}([0-9]{3})?$/;
Explanation :
a. 4 means the card number must start with '4'
b. [0-9]{12} means, any digit between 0 to 9 should appear 12 times. So, up this point, already 13 digits have been tested for old cards
c. ([0-9]{3})? means, a sequence of 3 digits may or may not appear. ? means either zero or one occurence. This point would validate if this is a new 16 digits card number.
2. Problem : Write validators for MasterCard Credit Cards
Visa credit card numbers are composed of 16 digits and they start with numbers 51 thorugh 55
answer :
var patt = /^5[1-5][0-9]{14}$/;
Explanation :
a. 5[1-5] means the string starts with '5' and then anything between 1 and 5. This ensures the number always have 51,52,53,54 or 55 at the beginning.
b. [0-9]{14} means any digit between 0 to 9 should appear 14 times
3. Problem : Write validators for AMEX [American Express] Credit Cards
American Express credit card numbers are composed of 15 digits and they start with numbers 34 or 37
answer :
var patt = /^(34|37)[0-9]{13}$/;
Explanation :
a. (34|37) means the number must start with '34' or '37'
b. [0-9]{13} means any digit between 0 to 9 should appear 13 times
We can write the above REGEXP slight differently as shown below.
var patt = /^3[47][0-9]{13}$/;
Explanation :
a. 3 means the number must start with '3'
b. [47] means the next digit should be either '4' or '7'
c. [0-9]{13} means any digit between 0 to 9 should appear 13 times
Check the next part of this article here
No comments:
Post a Comment