Here are some descriptions of what is done in the above script:


Retrieving Form Data and Removing Leading and Trailing Whitespaces

Preferences - Do not show ads

The form data entered by the user is stored in five WML variables: username, password, email, name and birthday. Before validation can be done, we have to retrieve the value of these WML variables. Any leading and trailing whitespaces should be removed since we do not want the user name, the password, etc, to be a few space characters:


  var form_username = String.trim(WMLBrowser.getVar("username"));
  var form_password = String.trim(WMLBrowser.getVar("password"));
  var form_email = String.trim(WMLBrowser.getVar("email"));
  var form_name = String.trim(WMLBrowser.getVar("name"));
  var form_birthday = String.trim(WMLBrowser.getVar("birthday"));


Checking Whether All Required Fields Has been Filled

Then we use the following WMLScript code to check whether the user has filled in all the required fields. The WMLScript code is very straightforward. If any of the variables contains an empty string, we will assign an error message to the WML variable errorMsg, refresh the WML browser and quit the validate() function:


  if (""==form_username){
    WMLBrowser.setVar("errorMsg", "The User Name field must not be empty.");
    WMLBrowser.refresh();
    return;
  }

  if (""==form_password){
    WMLBrowser.setVar("errorMsg", "The Password field must not be empty.");
    WMLBrowser.refresh();
    return;
  }

  if (""==form_email){
    WMLBrowser.setVar("errorMsg", "The Email field must not be empty.");
    WMLBrowser.refresh();
    return;
  }


The following screenshots show the error message that you will see if you do not fill in the username field:



Sony Ericsson T68i


Nokia Mobile Browser 4.0


Checking the Length of Password

Next, the length() function of the String standard library is used to check the number of characters contained in the password variable. If it is fewer than eight, the WML browser will display an error message to notify the user of the problem:


  if (String.length(form_password) < 8){
    WMLBrowser.setVar("errorMsg", "The password must contain at least 8 characters since a short password is less secure.");
    WMLBrowser.refresh();
    return;
  }


The following screenshots show the error message that you will see if you enter a password that contains fewer than eight characters:






Sony Ericsson T68i


Nokia Mobile Browser 4.0


Page 67 of 71 Previous Page | Next Page
Contents at a Glance
Print this Web Page | | Back to Top

Feedback Form (Expand)

What do you think about this web page?






WMLScript Tutorial Table of Contents WMLScript Tutorial Contents at a Glance Preferences - Change Color Scheme Preferences - Do Not Show Ads Previous Page Next Page