Here are some descriptions of what is done in the above script:
21.1. Retrieving Form Data and Removing Leading and Trailing Whitespaces
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"));
21.2. 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:
|
|
21.3. 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:
|
|
Previous Page | Page 67 of 71 | Next Page |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data