我正在尝试编写一个阻止用户提交个人电子邮件的 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;
}
});
关于javascript - 在 webflow 表单提交时返回 False 并发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657278/