The information in this article applies to:
SUMMARYThis article explains where to validate data when submitting forms. The best option is to validate all the form data in the onSubmit event handler of the form, though there is a catch when using Internet Explorer 3.x. You can also use onChange and onBlur events to validate data. This article points out some things to watch out for when doing so. MORE INFORMATIONUsing onSubmitInternet Explorer versions 3.x and later support the onSubmit event for form elements. The onSubmit event is the best place to perform data validation.If the data is invalid, then for Internet Explorer 4.x or later, set the return value to false (for example, event.returnValue=false) to cancel the form submission.
Since Internet Explorer 3.x does not support the event object, the following script can be used to cancel the event.
Using onChangeOften developers use the onChange event handlers for individual input elements to perform validation. The onChange event for a text input field is called only when the following conditions are satisfied:
Test 1Click in the text field and then click Submit without entering any text. Notice that the onChange event is not called resulting in an empty value being submitted with the form.Test 2If false isn't returned from the event handler, invalid data may be submitted. Comment out the event.returnValue = false line for this test. If a user enters invalid data and the onChange event does not reset the contents of the text input and return false, the user can click the Submit button the second time. The onChange event handler will not be called because the contents have not changed. Thus invalid data will be submitted with the form.Note that when the handler returns false, the user will not be able to move out of that input field until a valid value is entered. Using onBlurAs mentioned in "Test 1" above, onChange may not be called in certain cases. The alternative is to use the onBlur event handler. But in this case you have to set the focus back to the Input control. Otherwise, once the focus is lost from the control, the user will be able to click the Submit button and invalid data will be submitted.When using the onChange or onBlur event handlers, the focus may not be on the control when the page is first loaded. In this case, if the user clicks the Submit button, the page will be submitted without any validation. So using the onSubmit event handler is the best and safest place to perform data validation. REFERENCES
Documentation about the HTML and DHTML features discussed in this article can be found at the MSDN Web Workshop. Q172064 HOWTO: Submitting Data from an ActiveX Control in a Form Additional query words:
Keywords : kbIE300 kbIE301 kbIE401 kbScript kbIE302 kbIE401sp1 kbIE401sp2 kbGrpInet kbIE500 |
Last Reviewed: May 25, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |