我看到過類似的問題,但它們的提問方式與我想提出的問題不一樣。
我如何使用javascript來確保用戶提供的電子郵件與我希望他們在用戶點擊提交按鈕后的表單提交前提供的電子郵件相匹配。
闡釋:
正確的電子郵件地址是[email protected]。
我的表單輸入占位符顯示[email protected]作為提示。
如果用戶輸入的任何電子郵件與[email protected]不匹配,我希望他們得到錯誤資訊。 我希望他們得到一個錯誤資訊,并且表單不應該被提交。
我怎樣才能做到這一點?
我怎樣才能用javascript來實作這個目標呢?
請看我下面的代碼。
。 < form action=''/span> method='POST' enctype="multipart/form-data" >
<div class='item'>
<p>><b> <span style="color: red;">電子郵件地址</span></b><。 span class='required'> *</span></p>
< input type="email" name="email" placeholder="ba_00o@***. com" required=''/>。
</div>/span>
<div class='question'>
<center><p> 隱私政策<span class='required'/span>> *</span>< p></center>>
<div class='question-answer checkbox-item'>/span>
<div>
<center>< label class='check'/span> for='check_1'/span>> <span>通過提交此表格,您同意服務條款< a href='https: //www. google.com/p/privacy-policy.html' target="_blank">privacy policy. </a></span> </label></center>
</div>
</div>/span>
</div>/span>
<div class='btn-block'>
<center>/span> < button href='/' type='submit' id="submitForm"> 提交</button></center>
</div></form>
<iframe name="sif1" sandbox="allow-form allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
如果我是正確的,你需要添加和改變一些東西,這樣它就會作業。
首先,給電子郵件輸入欄位添加一個'id'屬性,就像這樣(這樣我們就可以在以后將表單與javascript代碼聯系起來):
input type="email" name="email" placeholder="ba******o@***.com" required=' id="email" />
然后,在頁面的開頭(和標簽之間)添加以下腳本。這個腳本會比較輸入的電子郵件,如果它們不匹配,會顯示一條資訊。
< script type="text/javascript"/span>>
function check_email()
{
var email = document.getElementById("email").value。
if(email.localeCompare("[email protected]") ) {
alert("ERROR. Email does not match." )。
return false。
}
return true;
最后,像這樣給標簽添加 "onsubmit "屬性(如果檢查電子郵件的函式回傳錯誤,那么表單將不會被發送):
< form action='' method='POST' enctype="multipart/form-data" onsubmit="return check_email(); ">
我希望這對你有用 :)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326104.html
標籤:
下一篇:Python中的張量總變異正則化
