Forum Moderators: open
<form onsubmit="blah()">)
I cannot use "return false" from the event handler function. I need some sort of other way to cancel the form submit if the input was invalid.
Simplified HTML
<form action="mail.cgi" method="get" id="newsletter_form">
<input type="text" name="email" id="email" />
<button type="submit">Sign Up</button>
</form>
JS - some functions are from lib.js by Caio Chassot
addLoadEvent(newsletterSubmit);
function newsletterSubmit() {
return addEvent(getElem('newsletter_form'), 'submit', newsletterValidateEmail);
}
function newsletterValidateEmail() {
emailAddress = getElem('email');
if (!isEmail(emailAddress.value)) {
alert('The email address you entered is invalid.');
//INVALID EMAIL
}
}
function isEmail(string) {
if (string.search(/^\w+((-\w+)¦(\.\w+))*\@[A-Za-z0-9]+((\.¦-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!= -1)
return true;
else
return false;
}
What I basically need is some sort of JS statement to abort the form submission at the point where it says "INVALID EMAIL" in bold. Something like:
getElem('newsletter_form').abort()
The library functions are somehow interfering with your use of return false.
I guess this would work (to cover standards browsers & IE, but not NS4 )
function newsletterValidateEmail(e) {
.....
window.event? event.returnValue = false : e.preventDefault(); /* IE : standards */
}
BUT (in the standards case) you will have to make sure that the event object (e) is being passed down the chain of functions into this function. If the library is any good, it should be doing that anyway.
To test (not in IE, since it's not necessary anyway) put in a parameter, and this at the top line of the function:
function newsletterValidateEmail([blue]e[/blue]) {
([blue]alert(e && e.constructor==Event);[/blue]
Then again, if there is an event object getting through, the fix should be working anyway.