24.6. Using JavaScript to Validate HTML Forms

Another important application of JavaScript is to check the format of form fields before the form is submitted to the server. Contacting the server can be expensive, especially over a slow connection, and simple tasks like checking that all required fields are filled out or making sure textfields that should contain numbers don't have strings should be performed on the client if at all possible. The document.forms property contains an array of Form entries contained in the document. As usual in JavaScript, named entries can be accessed by name instead of by number; moreover, named forms are automatically inserted as properties in the document object, so any of the following formats would be legal ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.