我的注冊表需要幫助。我正在使用他們的網站鏈接、電子郵件和密碼注冊用戶。我需要一些 jQuery 腳本來禁止用戶更改他們電子郵件的前綴。例如,他們為他們的網站設定了https://google.com =>,這會自動只允許在電子郵件輸入中寫下@google.com電子郵件。再舉一個例子:
網站:https : //youtube.com
允許的電子郵件:[email protected]
不允許的電子郵件:[email protected]
我試過:
<script>
$("#email").on('keyup input change', function() {
var val = $(this).val();
if(val.indexOf("@outlook.com") != 1) {
$(this).val(val '@outlook.com');
}
});
</script>
但這絕對不是我所期望的。除了一切,我需要想辦法只從鏈接中獲取域..
有任何想法嗎?
提前致謝!
uj5u.com熱心網友回復:
下面的示例按照您的要求進行操作,我添加了一些錯誤檢查和可用性改進。希望這大致是您所需要的,并且可以針對您的具體情況進行調整。
它根據此處的答案將提供的任何域轉換為主機名。有效地使用使用a標簽的技巧。
用戶對電子郵件輸入的更改會自動添加正確的域作為后綴,.val()使用.substr()或收集或設定值并使用或操作字串.replace()。用戶的位置不會丟失,因此他們可以在使用 輸入用戶名時繼續輸入.setSelectionRange()。
對電子郵件輸入執行錯誤檢查,盡管這不是必需的,因為我們通過自動添加正確的主機名后綴將用戶引導到正確的輸入。它將拒絕不以正確的主機名結尾的輸入,或者通過使用具有多個“@”的輸入.split("@").length(因為我們知道格式正確的電子郵件的結果陣列應該只有兩個元素)。
如果有錯誤(通過CSS樣式和.show()),將顯示一條訊息通知用戶,一旦顯示,該訊息將持續存在,否則它將通過自動更正自動隱藏。訊息中提供了所需的特定主機名,并隨著用戶輸入域的更改而更新。
該代碼已完全注釋,因此應該是不言自明的。
示范
// Setup variables to help simplify code later
var username = "";
var domain = "";
var emailDomain = "";
// Load initial values (not needed in reality unless you want to suggest a domain to users)
domain = url_domain($("#domain").val());
emailDomain = "@" domain;
$("#hostname").text(emailDomain);
// Key track of current domain
$("#domain").on('keyup input change', function() {
// Update variables
domain = url_domain($(this).val());
emailDomain = "@" domain;
// Update error message
$("#hostname").text(emailDomain);
});
// Check user input on email
$("#email").on('keyup input change', function() {
// Get username (by removing auto added domain if present)
username = $(this).val().replace(emailDomain, "");
// Add domain back / initially
$(this).val(username emailDomain);
// Set selection to appropriate place on username
// Improves user experience
document.getElementById('email').setSelectionRange(username.length, username.length);
// Prepare for error checking
val = $(this).val();
// Ensure the correct domain is at the end of the address, and that two domains are not provided.
if (val.substring(val.length - emailDomain.length) != emailDomain || val.split("@").length > 2) {
// Show warning if error present
$("#email-warning").show();
// Remove extra /incorrect domain
$(this).val(val.substr(0, val.indexOf("@")));
}
});
// Gets hostname from domain passed to it
// Original answer - https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string
function url_domain(data) {
var a = document.createElement('a');
a.href = data;
return a.hostname;
}
#email-warning {
color: red;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="domain" value="http://google.com">
<input id="email" placeholder="E-mail">
<p id="email-warning">You can only register with an email linked to your domain (<span id="hostname"></span>).</p>
uj5u.com熱心網友回復:
只需將您的鏈接包裝到 URL 建構式中,它將為您提供有關該鏈接的詳細資訊,然后只需使用字串方法includes()來檢查它的真偽
let host = new URL("https://youtube.com").hostname;
let input = $("input").val()
if(!input.includes(`@${host}`)) return false
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/358728.html
標籤:查询
