JavaScript表单校验生效时提交按钮失效,如何实现日期大小校验
问题分析与修复方案
一、现有代码错误点
- 校验函数返回逻辑错误:所有字段校验通过后,函数最终返回
return false,而onsubmit事件接收到false返回值会直接阻止表单默认提交行为,这是校验通过后点击提交无响应的核心原因。 - 日期输入框ID存在冗余空格:到达日期、离开日期输入框的ID属性末尾多写了空格(
id="arrival "、id="departure "),如果后续通过ID获取元素会匹配失败。 - 单选框label关联错误:信用卡选项的label的
for属性值为Credit,对应单选框的ID为cred,二者不匹配,点击label无法选中对应单选框。
二、修复实现(含到达日期早于离开日期校验逻辑)
以下是修复后完整的代码,调整了校验返回逻辑,补充了日期校验规则,修正了上述错误:
<!DOCTYPE html> <head> <title>Book Accomodations</title> <link rel="stylesheet" href="style.css"> <script> function validate(){ var x = document.forms["myform"]["fname"].value; var y = document.forms["myform"]["lname"].value; var email = document.forms["myform"]["email"].value; var arrivalVal = document.forms["myform"]["adate"].value; var departureVal = document.forms["myform"]["ddate"].value; var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; var getSelectedValue = document.querySelector('input[name="payment"]:checked'); if (x == "" || x == null) { alert("First Name must be filled out"); return false; } else if (y == "" || y == null) { alert("Last Name must be filled out"); return false; } else if (!email.match(filter)) { alert("Enter Proper Email ID"); return false; } else if(document.getElementById("country").value == "") { alert("Please select a country"); return false; } else if(getSelectedValue == null) { alert("Select Payment Mode"); return false; } else if(!arrivalVal || !departureVal) { alert("Please select arrival and departure date"); return false; } // 日期校验逻辑 const arrivalDate = new Date(arrivalVal); const departureDate = new Date(departureVal); if(arrivalDate >= departureDate){ alert("Arrival date must be earlier than departure date"); return false; } // 所有校验通过,返回true允许提交 return true; } </script> </head> <body> <div class="form"> <form name ="myform" action="function.php" onsubmit="return validate();" id="form" method="POST"> <label for="fname">First Name:</label> <input type="text" id="fname" name="fname" /><br> <label for="lname">Last Name:</label> <input type="text" id="lname" name="lname" /><br> <label for="email">Email:</label> <input type="email" id="email" name="email" /><br> <label for="arrival">Arrival Date:</label> <!-- 修正ID多余空格 --> <input type="date" id="arrival" name="adate" ><br> <label for="departure">Departure Date:</label> <!-- 修正ID多余空格 --> <input type="date" id="departure" name="ddate" /><br> <label for="country">Choose a Country:</label> <select id="country" name="country" form="myform"> <option disabled selected value> -- select an option -- </option> <option value="India">India</option> <option value="U.S.A.">U.S.A.</option> <option value="Nepal">Nepal</option> <option value="Bangladesh">Bangladesh</option> <option value="Germany">Germany</option> <option value="Spain">Spain</option> <option value="Italy">Italy</option> <option value="Sri Lanka">Sri Lanka</option> <option value="China">China</option> </select> <p>Payment Mode:</p> <input type="radio" id="deb" name="payment" value="Debit" /> <label for="deb">Debit Card</label> <input type="radio" id="cred" name="payment" value="Credit"/> <!-- 修正for属性值和单选框ID匹配 --> <label for="cred">Credit Card</label> <br> <input type="submit" id="submit" name="submit" value="submit" style="width: 100px;"/> <input type="reset" value="Reset" style="width: 100px; "/> </form> </div> </body>
内容的提问来源于stack exchange,提问作者shubham2708
相关产品推荐
相关产品推荐

