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:


(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


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.