信用卡号以 1 和 15 位数字开头
Credit card numbers starts with 1 & 15 digits long
我编写了以下 jquery/javascript 函数来验证 15 位长且必须以 digit1 开头的信用卡。
那么,我将如何验证数字是否以数字 1 开头。
if ($("#ddlCreditCardType" + i).val() == 'TP') {
if (!($("#txtCreditCardNo" + i).val().length == 15)) {
$("#err" + i).html('Credit Card Number should be 15 digit');
status = 0;
}
else {
$("#err" + i).html('');
status = 1;
}
您可以取值的第一个字母并将其与 1
:
进行比较
if ($("#ddlCreditCardType" + i).val() == 'TP') {
var value = $("#txtCreditCardNo" + i).val();
if (value.length !== 15 || value[0] !== '1') {
$("#err" + i).html('Credit Card Number should be 15 digit and start with 1');
status = 0;
}
}
使用 JavaScript 的 charAt 函数,它允许您读取第一个字符。
所以在你的代码中你可以这样写:
if ($("#txtCreditCardNo" + i).val().charAt(0) == '1'){
// your code after validation
}
如果您只需要支持最现代的浏览器,您可以执行以下操作
var ccNum = $("#txtCreditCardNo" + i).val();
if(ccNum.startsWith('1')){
//Do something cause it starts with 1
}
如果您必须支持旧版浏览器,您可以执行以下操作,它适用于新旧浏览器:
var ccNum = $("#txtCreditCardNo" + i).val();
if(ccNum.split('')[0] == '1'){
//Do something cause it starts with 1
}
还有其他选项,但其中任何一个都可以。
您也可以使用正则表达式。
var c1='123456789012345';
var c2='234567890123456';
var regex=/^1\d{14}/; //regular expressions starting with '1' and the 14 digits at the end
regex.exec(c1); //returns the c1
regex.exec(c2); //returns null
建议使用正则表达式验证信用卡。
检查这个表达式:
^1[0-9]{14}$
描述:
Options: case insensitive; ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the character “1” literally «1»
Match a single character in the range between “0” and “9” «[0-9]{14}»
Exactly 14 times «{14}»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
用法示例:
var credit = "123456789123456"; // lingth 15
alert(credit.match(/^1[0-9]{14}$/)); // will success
credit = "1234567891234567"; // lingth 16
alert(credit.match(/^1[0-9]{14}$/)); // will fail
credit = "5234567891234567"; // lingth 15, start with 5
alert(credit.match(/^1[0-9]{14}$/)); // will fail
我编写了以下 jquery/javascript 函数来验证 15 位长且必须以 digit1 开头的信用卡。 那么,我将如何验证数字是否以数字 1 开头。
if ($("#ddlCreditCardType" + i).val() == 'TP') {
if (!($("#txtCreditCardNo" + i).val().length == 15)) {
$("#err" + i).html('Credit Card Number should be 15 digit');
status = 0;
}
else {
$("#err" + i).html('');
status = 1;
}
您可以取值的第一个字母并将其与 1
:
if ($("#ddlCreditCardType" + i).val() == 'TP') {
var value = $("#txtCreditCardNo" + i).val();
if (value.length !== 15 || value[0] !== '1') {
$("#err" + i).html('Credit Card Number should be 15 digit and start with 1');
status = 0;
}
}
使用 JavaScript 的 charAt 函数,它允许您读取第一个字符。
所以在你的代码中你可以这样写:
if ($("#txtCreditCardNo" + i).val().charAt(0) == '1'){
// your code after validation
}
如果您只需要支持最现代的浏览器,您可以执行以下操作
var ccNum = $("#txtCreditCardNo" + i).val();
if(ccNum.startsWith('1')){
//Do something cause it starts with 1
}
如果您必须支持旧版浏览器,您可以执行以下操作,它适用于新旧浏览器:
var ccNum = $("#txtCreditCardNo" + i).val();
if(ccNum.split('')[0] == '1'){
//Do something cause it starts with 1
}
还有其他选项,但其中任何一个都可以。
您也可以使用正则表达式。
var c1='123456789012345';
var c2='234567890123456';
var regex=/^1\d{14}/; //regular expressions starting with '1' and the 14 digits at the end
regex.exec(c1); //returns the c1
regex.exec(c2); //returns null
建议使用正则表达式验证信用卡。 检查这个表达式:
^1[0-9]{14}$
描述:
Options: case insensitive; ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the character “1” literally «1»
Match a single character in the range between “0” and “9” «[0-9]{14}»
Exactly 14 times «{14}»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
用法示例:
var credit = "123456789123456"; // lingth 15
alert(credit.match(/^1[0-9]{14}$/)); // will success
credit = "1234567891234567"; // lingth 16
alert(credit.match(/^1[0-9]{14}$/)); // will fail
credit = "5234567891234567"; // lingth 15, start with 5
alert(credit.match(/^1[0-9]{14}$/)); // will fail