我试图写一个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;发布于 2019-06-19 14:05:01
我找到了一个有效的解决方案,将代码更改为:
$('#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;
}
});https://stackoverflow.com/questions/56657278
复制相似问题