简体   繁体   中英

Return False with Alert upon webflow form submission

I am trying to write a JS function which blocks users from submitting a personal email. Here is the code. When I remove the "alert" line, the user is blocked from a successful form submission. But there is no alert that prompts them to enter a business email.

$("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;
}});

Below is where there error is occuring. I'm new to Javascript so it very likely could be a syntax issue.

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

I found a solution that worked, changed the code to the following:

$('#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;
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM