我有一個 JavaScript,它的功能是確保用戶在我的表單上提供的電子郵件地址在我的表單提交之前與我的 JavaScript 中的結果(電子郵件地址)匹配。
(自動回復)我使用這個 JavaScript 是因為我設計了我的表單來將每個用戶回復的副本發送到他們的電子郵件地址。
我擁有的 JavaScript 只能驗證一個用戶的輸入,這意味著我必須為每個用戶創建一個單獨的表單,并放置包含每個用戶電子郵件地址的 JavaScript。
如何在表單提交之前僅在單個表單上使用一個 JavaScript 來驗證多個用戶的電子郵件地址?
下面是我用于單個表單代碼的單個 JavaScript;
<script type="text/javascript">
function check_email()
{
var email = document.getElementById("email").value;
if(email.localeCompare("[email protected]")) {
alert("ERROR. Email does not match.");
return false;
}
return true;
}
<form action='' method='POST' enctype="multipart/form-data" onsubmit="return check_email();">
<div class='item'>
<p><b><span style="color: red;">Email Address</span></b><span class='required'>*</span></p>
<input type="email" name="email" placeholder="ba******o@***.com" required='' id="email"/>
</div>
<div class='question'>
<center><p>Privacy Policy<span class='required'>*</span></p></center>
<div class='question-answer checkbox-item'>
<div>
<center><label class='check' for='check_1'><span>By submitting this form you agree to the terms of service <a href='https://www.google.com/p/privacy-policy.html' target="_blank">privacy policy.</a></span></label></center>
</div>
</div>
</div>
<div class='btn-block'>
<center> <button href='/' type='submit' id="submitForm">Submit</button></center>
</div></form>
下面是我嘗試過但沒有作業的 JavaScript 示例;
<script type="text/javascript">
function check_email()
{
var email = document.getElementById("email").value;
if(email.localeCompare == "[email protected]" || email.localeCompare == "[email protected]")) {
alert("ATTENTION! \nThe email address that you provided did not match with the email that is associated with your account.");
return false;
}
return true;
}
uj5u.com熱心網友回復:
你可以這樣做:
const EMAIL_LIST = ["[email protected]", "[email protected]"];
function check_email() {
const email = document.getElementById("email").value;
if (!EMAIL_LIST.includes(email)) {
alert(
"ATTENTION! \nThe email address that you provided did not match with the email that is associated with your account."
);
return false;
}
return true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/321763.html
標籤:javascript 形式 验证
下一篇:如何在python中進行驗證?
