我发现很难验证 2 个文本框以将其与 2012-13 等学术年的模式匹配。我为两个文本框提供了不同的 id,n 试图单独验证,但它没有发生。你们中的任何人都可以帮忙吗?我是一个新鲜人。我附上了我的js代码。如果两个文本框都找不到模式,我想要一条错误消息。
<html>
<body>
<script type="text/javascript">
window.onchange=function check_it() {
var ay = document.getElementById("ayear").value;
var fy=document.getElementById("fyear").value;
var pattern = /\d{4}-\d{2,4}$/;
if(pattern.test(ay) && pattern.test(fy)){
return true;
} else {
window.alert ("invalid");
return false;
}
}
</script>
<form >
<p> This Report is prepared for the Current Acadamic Year(<input type="text" size="10" id="ayear" >) and the Current Financial Year (<input type="text" size="10" id="fyear" >) on behalf of the Institution.</p>
</form>
</body>
</html>