我正在尝试运行一个脚本,看看我是否可以验证文本框中的前4个字符。文本框告诉我字符不在那里但是当我正确地放入前4个数字时它仍然给我同样的错误。此外,文本框最少包含15个字符,最多16个字符。
这是脚本:
var check_card_no = function (event){
check_digits = card_no.substring(0, 4);
if(check_digits=="9999"){
return true;
}
else{
alert("please enter valid Credit Card Number");
patron_card_no.value="";
patron_card_no.focus();
return false;
}
}
这样每个人都能看到完整的代码就是剧本的全部内容:
<script type="text/javascript">
// Caspio form elements kkw
var Ticket_PriceField = document.getElementById('InsertRecordTicket_Price');
var Number_of_TicketsField = document.getElementById('InsertRecordNumber_of_Tickets');
var totalField = document.querySelectorAll('span[class^="cbParamVirtual4"]')[0];
var patron_card_no = document.getElementById('InsertRecordPatron_Credit_Card_Number');
var card_no=patron_card_no.value;
var card_date=document.getElementById('InsertRecordPatron_Credit_Card_Exp');
var caspioForm = document.getElementById('caspioform');
// Event handler
var calculateTotal = function (event)
{
// TODO: Do something on value change -->
totalField.innerHTML = Ticket_PriceField.value * Number_of_TicketsField.value;
}
// Run total calculation on input
Number_of_TicketsField.addEventListener('input', calculateTotal);
// credit card no check
var check_card_no = function (event){
check_digits = card_no.substring(0, 4);
if(check_digits=="9999" ){
return true;
}
else{
alert("please enter valid Credit Card Number");
patron_card_no.value="";
patron_card_no.focus();
return false;
}
}
patron_card_no.addEventListener('change', check_card_no);
function check_credit_card_date(){
var credit_card_date=card_date.value;
var card_month=credit_card_date.substring(0, 2);
var card_year=credit_card_date.substring(2, 4);
if(credit_card_date.length==4 && (card_month > 0 && card_month < 13) && (card_year > 16 && card_month < 31) ){
return true;
}
else
{
alert("please enter valid Credit Card Exp. Date ");
card_date.value="";
card_date.focus();
return false;
}
}
card_date.addEventListener('change', check_credit_card_date);
</script>
答案 0 :(得分:0)
除非你有某种框架在视图和你的javascript之间提供绑定,否则当你更改它时,你必须再次阅读<input>
。
答案 1 :(得分:0)
假设您将String
输入传递给您的函数。
var check_card_no = function (card_no){
check_digits = card_no.substring(0, 4);
if(check_digits=="9999"){
return true;
}
else{
//alert("please enter valid Credit Card Number");
//patron_card_no.value="";
//patron_card_no.focus();
return false;
}
}
console.log('Valid Card test= '+check_card_no('99992222'));
console.log('Invalid Card test= '+check_card_no('2288888'));
&#13;