我有以下jquery脚本,用于根据某些值检查条件语句。
function is_numeric(val) {
status = $.isNumeric(val);
console.log("Our numeric status => " + status);
if (status) {
return true;
} else {
return false;
}
}
function hasValue(elem) {
return $(elem).filter(function() {
return $(this).val();
}).length > 0;
}
function validate_KRA_Pin() {
var nationality = $(".nationality").val();
console.log("Nationality => " + nationality);
if (nationality == '110') {
var validate_DoB = hasValue(".Date_of_Birth");
if (validate_DoB) {
var client_age = calculate_age();
if (client_age >= 18) {
$(".KRA_Pin_Error").show();
$(".kra_pin_div").show();
var pin_no = hasValue(".pin_no");
if (pin_no) {
var kra_pin_val = $(".pin_no").val();
var first_char = (kra_pin_val.charAt(0));
var second_char = is_numeric(kra_pin_val.charAt(1));
var third_char = is_numeric(kra_pin_val.charAt(2));
var fourth_char = is_numeric(kra_pin_val.charAt(3));
var fifth_char = is_numeric(kra_pin_val.charAt(4));
var sixth_char = is_numeric(kra_pin_val.charAt(5));
var seventh_char = is_numeric(kra_pin_val.charAt(6));
var eigth_char = is_numeric(kra_pin_val.charAt(7));
var ninth_char = is_numeric(kra_pin_val.charAt(8));
var letters = /^[A-Za-z]+$/;
var last_char = kra_pin_val.slice(-1);
if ((kra_pin_val.length == 11) && (second_char == true) && (third_char == true) && (fourth_char == true) && (fifth_char == true) && (sixth_char == true) && (seventh_char == true) && (eigth_char == true) && (ninth_char == true)) {
console.log("TRUE CONDITIONS MET....");
if (first_char.match(letters) && last_char.match(letters)) {
$(".KRA_Pin_Error").hide();
return true;
} else {
$(".KRA_Pin_Error").show();
return false;
}
} else {
$(".KRA_Pin_Error").show();
return false;
}
} else {
return false;
}
} else {
$(".KRA_Pin_Error").hide();
$(".kra_pin_div").hide();
return true;
}
}
} else {
return true;
}
}
该脚本应满足以下条件:
以下验证有效:
if ((kra_pin_val.length == 11){
console.log("TRUE CONDITIONS MET....");
}
但是,如果我执行以下操作,它将失败:
if ((kra_pin_val.length == 11) && (second_char == true) && (third_char == true) && (fourth_char == true) && (fifth_char == true) && (sixth_char == true) && (seventh_char == true) && (eigth_char == true) && (ninth_char == true)) {
console.log("TRUE CONDITIONS MET....");
if (first_char.match(letters) && last_char.match(letters)) {
$(".KRA_Pin_Error").hide();
return true;
} else {
$(".KRA_Pin_Error").show();
return false;
}
}
如何验证以下值: A123456789P:这是正确的,而A123456TR9D:这是错误的