HOWTO: validate a string with email adresses in it

OK, maybe not a beaty, maybe not too difficult but still, it took me 30 minutes to put together and wanted to give these 30 minutes as a gift to the forum members.

So, below you find a method to check a dataprovider with one or more email adresses in it…

Have fun with it (and maybe improve it?)

if (emailvalue != null && emailvalue != “”)
{
emailvalue = utils.stringReplace(emailvalue,‘,’,‘;’);

var addressCount = utils.stringPatternCount(emailvalue,‘;’);
if (addressCount != 0)
{var addresses = emailvalue.split(“;”);}
else
{
var addresses = new Array(0);
addresses[0] = emailvalue;
}

var addressMisMaches = “”;

for (var i = 0 ; i < addressCount+1; i++)
{
var match = addresses_.match(/^\w+([.-]?\w+)@\w+([.-]?\w+)(.\w{2,3})+$/);_

  • if (match == null)*
  • {*
  • if (addressMisMaches > “”)*
  • {addressMisMaches = addressMisMaches + ", "}*
    _ addressMisMaches = addressMisMaches + “'” + addresses + “'”;_
    * }*
    * }*
    * if (addressMisMaches != “”)*
    * {*
    * if (utils.stringPatternCount(addressMisMaches,‘,’) == 0)*
    * {plugins.dialogs.showErrorDialog( “Let op…”, “The following emailaddress is wrong:\n” + addressMisMaches, “OK”);}*
    * else*
    * {plugins.dialogs.showErrorDialog( “Let op…”, “The following emailaddresses are wrong:\n” + addressMisMaches, “OK”);}*
    * elements.emailAddress.requestFocus();*
    * }*
    }

Just a simple email check (one in each field) you can use this:

attach it to onDataChange property of field: email

controller.saveData();

if(email == "" ||
utils.stringPatternCount(email,",") == 0
&& utils.stringPatternCount(email," ") == 0
&& utils.stringPatternCount(email,"@") == 1
&& utils.stringPatternCount(email,".") >= 1)

{
	return;
}
else
{
	plugins.dialogs.showInfoDialog('Info', 'Email notation is NOT right!','OK');	
	elements.email.requestFocus()
}

It accepts an empty field and it refuses: comma, spaces. There has te be one “@” and at least one “.”