SWE 642 JavaScript example: Form Validation 2

Text version Next example Back to index

This is another example of using JavaScript to validate forms data. This example checks each fields sequentially when the form is submitted. This is how to do data validation on the client side before going to the server side.

The FORM attribute onSubmit can return a boolean value. If it returns false, the form data is not sent to the web server CGI program.

parseInt converts a string to an integer and parseFloat converts a string to a floating point number. JavaScript 1.1 returns NaN if the string is not a valid number, which can be checked by the isNaN function.

Class registration form

Student Number: