|
|
Q1412 How can I validate a form field using onBlur and if necessary halt the submission of the form if the user has clicked on the submit button - which caused the original onBlur event to be fired?
irt.org | Knowledge Base | JavaScript | Event | Q1412 [ previous next ] Q1412 How can I validate a form field using onBlur and if necessary halt the submission of the form if the user has clicked on the submit button - which caused the original onBlur event to be fired?Two events have been fired: onBlur and onSubmit. I'm not sure which one is fired first - it may be impossible to predict. You need to be able to cancel the onsubmit event from the onBlur event. The only way to do this is by creating a global boolean variable that the onSubmit event handler checks to see if the combination of and onFocus/onBlur event have validated the field data. If not then the onSubmit handler needs to return false (effectively cancelling the form submission). It then gets a bit more complicated, as once the onBlur event handler has validated the field data, it may need to start the form submission over again, so it need to know that there was an attempt to submit the form. Therefore we need yet another global boolean variable that the onSubmit handler sets to true just before returning false. Hopefully the following example code does all the above:
|
-- div -->
|