问题描述
我经营一个商业网站,并试图在客户端防止某些免费电子邮件地址不在我的网站上注册(服务器端将单独处理)。我查看了一些帖子,并在 https://stackoverflow.com/a/8277306/857629 上找到了 Skinsey 的答案。
我必须对代码进行的唯一真正更改是在输入框的 focusout
上触发检查,然后显示消息并清除输入框。
我对 JQuery 很陌生,无法弄清楚为什么当输入框失去焦点时下面的代码没有触发。
有问题的元素是:
<input type="email" name="email" id="inputEmail" placeholder="your contact email">
我的JQuery如下:
jQuery('#inputEmail').focusout(function(){
// Get the email value from the input with an id="inputEmail"
var email_addr = jQuery('#inputEmail').val();
// The regex to check it against
var re = '[a-zA-Z_\.-]+@((hotmail)|(yahoo)|(gmail)|(outlook)|(protonmail)|(aol))\.[a-z]{2,4}';
// Check if the email matches
if(email_addr.match(re)){
// Email is on the filter list
// Return false and don't submit the form,or do whatever
jQuery('#inputEmail').value = "";
jQuery('#inputEmail').append("<div>To prevent SPAM,free email addresses are no longer accepted on registration. Please enter a valid coorporate email address.</div>");
return false;
} else {
// Email ok
// Allow the form to be submitted
return true;
}
});
</script>
我有一种感觉,我错过了一些简单的东西。
任何帮助将不胜感激。
解决方法
你可以试试这个
$('#inputEmail').on( "focusout",function(){
console.log('Focus Removed')
} );
,
$(document).on("focusout","#inputEmail",function(){
alert("Focus Removed");
});