在提交网络流表单时返回 False 并带有警报



我正在尝试编写一个JS函数来阻止用户提交个人电子邮件。这是代码。当我删除"警报"行时,用户被阻止成功提交表单。但是没有提示他们输入业务电子邮件的警报。

$("form").submit(function(){
// Get the email value from the input with an id="Email-2"
var email_addr = $('#Email-2').val();
// The regex to check it against
var re = '[a-zA-Z_\.-]+@((hotmail)|(yahoo)|(gmail))\.[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
    window.alert("Enter Business Email");
    return false;
} else {
    // Email ok
    // Allow the form to be submitted
    return true;
}});

下面是发生错误的地方。我是Javascript的新手,所以很可能是一个语法问题。

window.alert("Enter Business Email");
return false;

我找到了一个有效的解决方案,将代码更改为以下内容:

$('#wf-form-Book-Demo-Form').submit(function(){ 
var email = $('#Email-2').val();
var reg = /^([w-.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!yahoo.co.in)(?!aol.com)(?!abc.com)(?!xyz.com)(?!pqr.com)(?!rediffmail.com)(?!live.com)(?!outlook.com)(?!me.com)(?!msn.com)(?!ymail.com)([w-]+.)+[w-]{2,4})?$/;
if (reg.test(email)){
return 0;
}
else{
alert('Please Enter Business Email Address');
return false;
}
});

最新更新