Formatting Fields
- Javascript's most important use is client-side processing
of data entry fields.
- This usually means string validation and formatting.
- Javascript has methods and properties for strings:
- Javascript has a String object, but we don't use that here.
Here's an example and here's its
source code.
Observe:
- The length property is used.
- The substring() method is applied.
- A local scalar variable is declared as a var.
- The document object is used (see diagram.)
- The form sub-object (named socsecno) is used.
- The field sub-sub-object (named T1) is used.
- The value property is used.
- The content of the data entry field is modified by the Javascript code.
Fields can be modified, but text on the web page cannot.
Lab
- Write a form to elicit a ten-digit phone number without punctuation or spaces,
e.g. 5555555555.
- Add Javascript code that
surrounds the area code with parentheses and inserts a hyphen before the last four
digits, e.g. (555)555-5555.
Solution
Extra for Experts