don't give up yet! You're so close..
Since the onclick event listener is attached to a submit field rather than just a button, the form will be submitted no matter what. Your onclick event listerner will most likely not complete execution.
What you can do is change the field to a type="button" instead of type="submit".
Then the form will not be submitted unless you submit it via javascript with the form.submit() method.
However, a better way of doing it is to attach an event Listener to the form submit method, as Matt said.
This allows a user without javascript on their browser to submit the form, but users with javascript to have the form validation.
You could do it like:
document.formx.onsubmit = function() {
// paste the form validation code you have here..
// then use "return false;" to not send the form, and "return true;" to send it.
};
A better way to attach event listerners is to use the W3C DOM method: addEventListener()
http://developer.mozilla.org/en/docs...dEventListener
and use the M$ implementation, attachEvent(), where needed.
Heres one implementation:
addEvent = function(el, evType, fn, useCapture) {
if (el.addEventListener) {
el.addEventListener(evType, fn, useCapture);
return true;
}
else if (el.attachEvent) {
var r = el.attachEvent('on' + evType, fn);
return r;
}
else {
el['on' + evType] = fn;
}
}
that way you can attach multiple events, and also not overwrite any events that are attached by other parts of the script.
Off course when you use JS form validation, it should be considered as helping the form user, nothing more.
I wrote something on this last night:
http://fijiwebdesign.com/content/view/93/77/ (its not finished)
JS form validation is very useful in helping the user out however since close to 100% of users have JS on their browser.