我有三个输入用于以 HH:MM:SS 格式输入时间,其中我在jsp页面中分别为每个输入三个文本字段。我希望我的分钟(MM)为 00 或 30(例如:12:00:00 或 12:30:00)。
这是我在此验证中完成的条件javascript:
if(time_mm!=00 || time_mm!=30)
{
alert("Enter a valid \"Time: Minutes(MM)\"");
document.getElementById("time_mm").focus();
document.getElementById("time_mm").value = "";
return false;
}
Run Code Online (Sandbox Code Playgroud)
在这里,如果输入为00,则第一个条件time_mm!=00将为false,因此它不会进入以下过程并从语句中出来,这很好。当输入为 时30,问题就出现了,其中第一个条件time_mm!=00为真,进入以下过程,而其他条件time_mm!=30保持不变。
所以我的问题是我收到警报“输入有效的“时间:分钟”,即使我的输入是30.
我的条件有问题吗??有什么建议???
使用三重等于检查类型也===和'00' , '30'作为字符串:
if(time_mm === "00" || time_mm === "30")
{
alert("Valid Time Minutes");
}else{
alert("Enter a valid \"Time: Minutes(MM)\"");
document.getElementById("time_mm").focus();
document.getElementById("time_mm").value = "";
return false;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
if(time_mm === "00" || time_mm === "30")
{
alert("Valid Time Minutes");
}else{
alert("Enter a valid \"Time: Minutes(MM)\"");
document.getElementById("time_mm").focus();
document.getElementById("time_mm").value = "";
return false;
}
Run Code Online (Sandbox Code Playgroud)
$('body').on('click', '#validate', function(){
var time_mm = document.getElementById("time_mm").value.split(':')[1];
if(time_mm ==="00" || time_mm === "30")
{
console.log("Valid Time Minutes");
}else{
console.log("Enter a valid \"Time: Minutes(MM)\"");
document.getElementById("time_mm").focus();
document.getElementById("time_mm").value = "";
return false;
}
});Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8779 次 |
| 最近记录: |