[英]How do I validate my form before redirecting to another page?
我正在創建一個注冊表單,我希望在將用戶發送到 login.html 頁面之前對其進行驗證。
有沒有辦法用這段代碼做到這一點? 我希望所有這些字段都超過 2 個字符,現在在#firstName
字段中寫入 2 個字符就足夠了,然后它會重定向。
$('#regForm').on('submit', function() {
if($('#firstName, #lastName, #address, #city, #zip, #phoneNumber, #email, #inputPassword, #confirmPassword').val().length >= 2)
{
window.location.replace("login.html");
} else {
alert('You must fill in all the required fields.')
}
})
您可以在回調函數中使用event.preventDefault()
:
$('#regForm').on('submit', function(event) {
event.preventDefault();
if($('#firstName, #lastName, #address, #city, #zip, #phoneNumber, #email, #inputPassword, #confirmPassword').val().length >= 2)
{
window.location.replace("login.html");
}
else {
alert('You must fill in all the required fields.');
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.