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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.