HOWTO: validate a string with email adresses in it

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

HOWTO: validate a string with email adresses in it

Postby IT2Be » Mon Nov 10, 2003 12:43 pm

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[i].match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);

if (match == null)
{
if (addressMisMaches > "")
{addressMisMaches = addressMisMaches + ", "}
addressMisMaches = addressMisMaches + "'" + addresses[i] + "'";
}
}

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();
}
}
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany

Postby Harjo » Mon Nov 10, 2003 2:23 pm

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

attach it to onDataChange property of field: email

Code: Select all
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 "."
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands


Return to How To

Who is online

Users browsing this forum: No registered users and 8 guests

cron