|
||
| Inside Technique : Custom Input Validation : The Code - Part II Now we are ready to examine the code that tests the user's input. The code examines the following information:
Doing all of these tests is actually fairly complicated. Since we are working with the
text selection, we use the TextRange object for all our manipulations.
The first step is to get the current selection. The selection is exposed through the createRange()
method on the document's selection object. This method returns a TextRange that represents the selected
text or the location of the insertion point in the text box. We also create a TextRange that stores the
entire value of the text box. This is retrieved using the createTextRange method that is exposed on the
text box itself:
As we continue through the code, the most important point to remember about the TextRange object is that there is no concept of character positions. If you look at all the tests, they all talk about comparing the position of the decimal point in relation to the user's input. With character positions, this would be relatively simple as you would compare two numbers. However, with TextRange objects, you can only test the position relative to another range using the built-in methods. Therefore, the remainder of this code works fairly abstractly by examining whether text ranges are before, after, or within other text ranges. The rest of the code revolves around testing the input against the decimal point. For example, if a decimal point exists in the text box, and a new decimal point is typed, it can only be accepted if the existing decimal point is selected by the user so it is replaced. We perform this test by moving the TextRange to the existing decimal point. Then we call the inRange() method to see if the range representing the existing decimal point is contained within the range representing the user's selection. If a number is entered, we perform a different comparison. The current insertion point is compared against the position of the decimal point. If the decimal point is before the insertion point, we need to test if there are any decimal places left to fill. If the decimal point follows the insertion point, the user's input is automatically accepted. If no decimal point already exists and a decimal point is entered, it is tested to make sure that inserting the decimal point does not cause too many decimal places to be created. If not, the decimal point is inserted. Last, if no decimal point exists and a number is typed, it is automatically accepted. The explanation for these steps is actually longer than the code! Below is the code fragment that
performs all the actual testing:
The last step is to return a boolean to the event handle signifying whether the typed
key is to be accepted. If you have been following the code closely, you will notice that if
either the variable checkDot or allowDigit is true, then the typed key is accepted:
That concludes the code. We recommend you play with the code and the TextRange object to better familiarize yourself with these techniques and concepts. On the next page, we provide some parting thoughts and provide a brief explanation on why you can't write a cross-browser version of this code. Page 1:Custom Input Validation © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |