21.4. Validating Email Address
After checking the length of the password, the isEmailValid() function is called to validate the email address. For simplicity, the isEmailValid() function will not check all the syntax rules that define a valid email address. It just makes sure there is a single @ character that divides the string into two parts. Its code is shown below:
function
isEmailValid(emailAddr)
{
if (String.elements(emailAddr, "@")
!= 2)
return false;
var element_1 =
String.elementAt(emailAddr, 0, "@");
var element_2 =
String.elementAt(emailAddr, 1, "@");
if (""==element_1
|| ""==element_2)
return false;
return
true;
}
Inside isEmailValid(), we first check whether the @ character divides the string into two elements. If the @ character does not exist in emailAddr, the value returned by elements() will be 1. If there are more than one @ character, the value returned by elements() will be greater than 2. For example, the string "alan@andrew@somedomain.com" can be broken into three elements: "alan", "andrew" and "somedomain.com". In these cases, the isEmailValid() function will return the value false.
Next we check if the @ character is the first or last character of the email address. If the @ character is the first character, the first element (i.e. element_1) will be an empty string; if the @ character is the last character, the second element (i.e. element_2) will be an empty string. In these cases, the isEmailValid() function will return the value false.
Previous Page | Page 68 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