Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing

Example 16.8:
Not Too Long, Nor Too Short

This is supposed to be a 3-digit number, but type something longer, then try something shorter:

Now click on this button in the second form:
You'll see the text in the third form:


This is the script we used. In the HEAD... function Length() { var sCheck = document.fred.elements[0].value if (sCheck.length > 3 || sCheck.length < 3) { alert("You must type 3 characters: no more, no less") } else { Pass() } } function Pass() { var sCheck = document.fred.elements[0].value nCheck = parseInt(sCheck, 10) if (nCheck <=99 || nCheck == 0) { alert("This is not a 3-digit number.") } else { if (nCheck >= 100) { if (nCheck >=201) { alert("You entered a number that's too high. You must enter a number within the range of 100 to 200 inclusive.") } else { document.joe.elements[0].value = nCheck } } } } //--> </SCRIPT> Then these forms: <FORM NAME="fred"> <input type="text" size="3"><P> </FORM> <I>Now click on this button in the second form:</I> <FORM> <input type="button" value="Check the Text and Pass if Between 100 and 200" onclick="Length()"> </FORM> <I>You'll see the text in the third form:</I> <FORM NAME="joe"> <input type="text" size="30"><P> </FORM> Also, at the bottom of the form we moved focus to the text box with this: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- document.fred.elements[0].focus() //--> </SCRIPT>
Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing