js日期验证问题 [英] js date validation problem
本文介绍了js日期验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
re = /^(\d{1,2})-(\d{1,2})-(\d{4})*$/;
if((form.dob.value="") || (!re.test(form.dob.value.match))) {
alert("Error:enter Valid date of birth!");
form.dob.focus();
form.dob.style.background = "yellow";
return false;
}
当我输入日期10-10-2010或suh类型时,这是我的代码。它不接受它
this is my code when i enter date 10-10-2010 or suh type. it does not accept it
推荐答案
/;
if ((form.dob。 value = )||(!re.test(form.dob。 value )。匹配))){
alert( 错误:输入生效日期!) ;
form.dob.focus();
form.dob.style.background = yellow;
return false ;
}
/; if((form.dob.value="") || (!re.test(form.dob.value.match))) { alert("Error:enter Valid date of birth!"); form.dob.focus(); form.dob.style.background = "yellow"; return false; }
当我输入日期10-10-2010或suh类型时,这是我的代码。它不接受
this is my code when i enter date 10-10-2010 or suh type. it does not accept it
观察到相当多的错误,仅举几例:
1.语法错误如同等比较应==不=,
2.正则表达式错误:为什么在重新模式的末尾有一个*,*表示没有或更多,这意味着它将接受10-10-20142012 以及10-10-
查看此示例:
Quite a number of errors observed, just to mention a few:
1. syntax error like equal comparison should "==" not "=",
2. regex error: why having an "*" towards the end of the re pattern, "*" means none or more, which means it will accept "10-10-20142012" as well as "10-10-"
Check out this example:
<!DOCTYPE html>
<html>
<body>
<form onsubmit="return validate();" action="">
Date of Birth: <input type="text" name="dob" value=""><br>
<input type="submit" value="Submit">
</form>
<script>
function validate()
{
var dob = document.forms[0].dob;
re = /^(\d{1,2})-(\d{1,2})-(\d{4})
/ ;
if((dob.value ==)||(!dob.value.match(re))){
alert(错误:输入生效日期! );
dob.focus();
dob.style.background =黄色;
返回false;
}
}
< / script >
< / body >
< / html >
/; if((dob.value == "") || (!dob.value.match(re))) { alert("Error:enter Valid date of birth!"); dob.focus(); dob.style.background = "yellow"; return false; } } </script> </body> </html>
这篇关于js日期验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文