我正在使用时间验证,我对如何validate
使用&& 感到困惑。到目前为止我有这个代码:start_time
end_time
var re = /^(\d{1,2}):(\d{2})([ap]m)?$/;
//Start Time
if($('.start_time').val() != '') {
if(regs = $('.start_time').val().match(re)) {
if(regs[3]) {
// 12-hour value between 1 and 12
if(regs[1] < 1 || regs[1] > 12) {
$('.start_time_error').html('<div>Invalid value for hour(s)</div>');
$('.start_time').focus();
return false;
}
} else {
if(regs[1] > 12){
$('.start_time_error').html('<div>Invalid value for hour(s)</div>');
return false;
}
}
// minute value between 0 and 59
if(regs[2] > 59) {
$('.start_time_error').html('<div>Invalid value for minute(s)</div>');
$('.start_time').val().focus();
return false;
}
} else {
$('.start_time_error').html('<div>Invalid time format</div>');
$('.start_time').focus();
return false;
}
$('.start_time_error').html('<div>Checked</div>');
return true;
}else{
$('.start_time_error').html('<div>Please fill up</div>');
return false;
}
//End time----------
if($('.end_time').val() != '') {
if(regs = $('.end_time').val().match(re)) {
if(regs[3]) {
// 12-hour value between 1 and 12
if(regs[1] < 1 || regs[1] > 12) {
$('.end_time_error').html('<div>Invalid value for hour(s)</div>');
$('.end_time').focus();
return false;
}
} else {
if(regs[1] > 12){
$('.end_time_error').html('<div>Invalid value for hour(s)</div>');
return false;
}
}
// minute value between 0 and 59
if(regs[2] > 59) {
$('.end_time_error').html('<div>Invalid value for minute(s)</div>');
$('.end_time').val().focus();
return false;
}
} else {
$('.end_time_error').html('<div>Invalid time format</div>');
$('.end_time').focus();
return false;
}
$('.end_time_error').html('<div>Checked</div>');
return true;
}else{
$('.end_time_error').html('<div>Please fill up</div>');
return false;
}
我试过类似的东西:
if(regs = $('.start_time').val().match(re) && regss == $('.end_time').val().match(re) )
但它发送和错误对我不起作用regss is not defined
。关于如何做到这一点的任何替代方案?谢谢!