Validating Email Address

Preferences - Do not show ads

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:


(validateFormEg1.wmls)

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

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