我贈送免費門票給學生免費進入培訓班,我在他們的門票上寫下每個學生的電話號碼,以防止另一個學生使用偷來的門票進入。
我正在嘗試設計一個表格,讓學生注冊為候選人并獲取他們的資料。
在表格上,我希望他們輸入他們的電話號碼和印在我贈送給他們的票上的唯一代碼。
我試圖讓只有持票的學生才能提交表格。
當電話號碼和票號不匹配時,如何防止使用javascript提交表單?
下面是我用來防止電子郵件不匹配時提交表單的 javascript 示例。
我知道下面的示例不是最好的解決方案,但我仍然需要解決方案。謝謝。
代碼示例;
function check_email()
{
var email = document.getElementById("email").value;
if(email.localeCompare("[email protected]" || "[email protected]")) {
alert("ATTENTION! \nThe email address that you provided did not match with the email that is associated.");
return false;
}
return true;
}
<form action='' onsubmit="return check_email();">
<input type="password" name="email" placeholder="go*****b@*****.com" required='' id="email"/>
<button href='/' type='submit' id="submitForm">Submit</button> <button class="button" type="reset" value="Cancel">Reset</button>
</form>
uj5u.com熱心網友回復:
從評論中,我承認你已經在一本書中很好地撰寫了配對資料。然后,您可以將它們全部寫入對陣列(json 陣列)。
var data = [{phone: '08012345678', ticket: 'ACTF'}, {phone: '08012345679', ticket: 'ACTG'}];
// set false default
document.querySelector('form').addEventListener('submit', function (e)
{
var phNumber = document.getElementById("phone").value;
var scTicket = document.getElementById("ticket").value;
var isMatch = false;
// check if any same
for(var i=0; i<data.length; i ) {
// If same, proceed submit.
if (data[i].phone === phNumber && scTicket === data[i].ticket){
isMatch = true;
return true;
}
}
//if the validator check fails, prevent the form from being submitted
if(isMatch == false){
e.preventDefault();
alert('Not match!');
}
});
<form action='post' >
<input type="text" name="phone" placeholder="Phone Number" required='true' id="phone"/>
<input type="password" name="ticket" placeholder="Secret Ticket Code" required='true' id="ticket"/>
<button href='/' type='submit' id="submitForm">Submit</button> <button class="button" type="reset" value="Cancel">Reset</button>
</form>
uj5u.com熱心網友回復:
您可能想使用event.preventDefault()(檢查這個問題:JavaScript code to stop form submit
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464301.html
標籤:javascript 形式
上一篇:HTML表單成功后防止重新提交
