Character Validation
In this web form,
a JavaScript function validates text entered by the user.
Ponder the source code and observe:
- a for loop
- use of charAt() to extract characters from strings
- use of the focus() method to put the cursor into the field
- the break statement
- the onLoad
and onChange events
- the this object
- the field is cleared when an "error" is detected
- the && comparison operator (logical AND)
JavaScript has the usual repertory of programming language statements, including:
- Conditionals: if, else, switch
- Loops: for, while, do while, break, continue
- Object Manipulation: for...in, new, this, with
- Comments: single-line (//) and multi-line (/*...*/)
Lab
Your task:
- Make a copy of the example form and modify it as follows.
- Change the instructions and the Javascript logic so that the
text field accepts numbers only.
- You may find the || (logical OR) operator useful.
Solution
Don't peek unless you're stuck!