• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

8. Dynamic Forms > 8.3. Preventing Form Submission upon Validation Failure

Preventing Form Submission upon Validation Failure

NN 2, IE 3

Problem

You want a validation function that detects incorrect data entry to halt the submission of the form until the user corrects the data entry.

Solution

Batch validation checking typically operates from the onsubmit event handler of the form element. Submission is aborted if the event handler evaluates to return false. Include the return statement in the event handler assignment, and let the validation function supply the Boolean value based on its findings:

<form ... onsubmit="return validateForm(this)">

Discussion

You can implement batch validation by way of a master function that calls the individual validation functions as needed. To demonstrate, let’s create a small form with numerous control types in it. Text fields execute real-time validation, while the form’s onsubmit event handler performs the batch validation. Both validation types use the validation functions shown in Recipe 8.2 (either the string parsing or regular expression varieties). Here’s the form’s HTML:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint