WMLScript Developer's Guide

[Cover] [Previous Section] [Next Section] [Index]

Current chapter: Chapter 9 - WMLScript Examples
Section 49 out of 57 total sections , Section 2 out of 4 sections in this chapter


Numeric Range Validation Example

This example demonstrates how to perform numeric range validation using WMLScript and WML



What it Does

The file validate.wml uses three cards: one to prompt the user for the input value for range validation, two to display the results. The file validator.wmls tests the input value to see first if it is a valid whole number and then to see if it falls within the specified range, and returns the results to the UP.Browser. If the number falls within the specified range, the valid card displays the results. If the number does not fall within the specified range, the invalid card displays the results. A complete code listing follows:


Validate.wml

<?xml version="1.0"?>  
    <!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.1//EN"  
        "http://www.phone.com/dtd/wml11.dtd">  
 
<wml>  
    <card id="first">  
        <onevent type="onenterforward">  
            <refresh>  
                <setvar name="num" value=""/>  
            </refresh>  
        </onevent>  
 
    <p>  
        <do type="accept" label="valid">  
            <go href="validator.wmls#validate()"/>  
        </do>  
        Enter a whole number between 25 and 50  
        <input type="text" name="num" format="*N"/>  
    </p>  
    </card>  
 
    <card id="invalid">  
    <p>  
        <do type="accept" label="again">  
            <go href="#first">  
                <setvar name="num" value=""/>  
            </go>  
        </do>  
 
    $(num) is not in the range 25 to 50  
    </p>  
 
    </card>  
 
    <card id="valid">  
    <p>  
        <do type="accept" label="again">  
            <go href="#first">  
                <setvar name="num" value=""/>  
            </go>  
        </do>  
 
    $(num) is in the range 25 to 50  
    </p>  
 
    </card>  
</wml>  

Validator.wmls

extern function validate(){  
    var validNum = WMLBrowser.getVar("num");  
    var validNumAsInt = Lang.parseInt(validNum);  
    var max = 50;  
    var min = 25;  
    if (validNumAsInt) {  
        if ((validNumAsInt < min) || (validNumAsInt > max)) {  
            WMLBrowser.go("validate.wml#invalid");  
        }  
        else { WMLBrowser.go("validate.wml#valid");}}  
    else {  
        Dialogs.alert("The number " + validNum + "is not a             whole number");  
        WMLBrowser.setVar("num", "");  
        WMLBrowser.go("validate.wml#first");  
    }  
}  


How it Works

The initial card in validate.wml performs two tasks. First, it prompts the user for a whole number between 25 and 50, assigns the input value to the variable num, and calls the external function validate in validator.wmls:

<go href="validator.wmls#validate()"/>  

Secondly, if the user arrived at the initial card from either of the other two cards (valid or invalid), the card initializes the num variable, preparing the numeric range validator for another cycle.

What happens next depends on the value the user entered. If the input number is a whole number, the validate function tests to see if it is between 25 and 50. If it is, the validate function loads the valid card into the UP.Browser. If the input number is not between 25 and 50, the validate function loads the invalid card into the UP.Browser. Both the valid and invalid cards prompt the user to test another number. If the user chooses to do so, these cards initialize the variable num and return the user to the initial card:

<do type="accept" label="again">  
    <go href="#first">  
        <setvar name="num" value=""/>  
    </go>  
</do>  

The range validation takes place in validator.wmls, which then loads the appropriate card into the UP.Browser to display the results. This WMLScript declares one external function that serves as the entry point for the WML card, as described above:

extern function validate()  

The validate function initializes its own variables to the value the user set using the WML cards with the WMLBrowser library function getVar:

var validNum = WMLBrowser.getVar("num");  
var validNumAsInt = Lang.parseInt(validNum);  

The last line in this block converts the string value derived from num to an integer value that validate can use.

The next two variables set the upper and lower limits for the range validation test:

    var max = 50;  
    var min = 25;  

The next section in validate tests validNumAsInt (derived from num) to make sure the user entered a whole number, if not, it displays an error message to inform the user that the value entered is invalid and then prompts the user to enter a new value:

if(curVal){  
    if (validNumAsInt) {  
...  
    else {  
        Dialogs.alert("The number " + validNum + "is not a             whole number");  
        WMLBrowser.setVar("num", "");  
        WMLBrowser.go("validate.wml#first");  
    }  

If validNumAsInt is a whole number, validate then tests the input value against the minimum and maximum allowable values. If validNumAsInt falls outside the specified range, validate loads the invalid card into the UP.Browser. If validNumAsInt falls within the specified range, validate loads the valid card into the UP.Browser.

        if ((validNumAsInt < min) || (validNumAsInt > max)) {  
            WMLBrowser.go("validate.wml#invalid");  
        }  
        else { WMLBrowser.go("validate.wml#valid"); }  

[Cover] [Previous Section] [Next Section] [Index]


Part Number DKDS-41-002, UP.SDK Release 4.1, December 2000

Copyright © 1994-2000 Openwave Systems Inc. All rights reserved.
Please send comments and questions to sdk-doc-comments@openwave.com.