Validate list of email addresses separated by commas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2015 03:05 PM
I need to validate a textbox that will accept a list of email addresses that are separated by commas. The code is onChange Catalog Client Script.
The list of email addresses is in the variable "group_members".
The validation works for the FIRST email address only. The second email address in the list is showing as invalid even though I have tested with a list of valid addresses. For example:
Testa@example.com, testb@example.com
testa will show up as valid but testb will be invalid. I know it is not the regex because the same email address will return valid as long as it is the FIRST in the list. I think there is something wrong with looping over the array or perhaps the way that the comma separated list is being split to the array. Can you check my logic here to see what is wrong? I appreciate the help.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (!isLoading && newValue !='') {
var group_members = g_form.getValue('u_group_members');
var member_split = group_members.split(',');
for (var n = 0; n < member_split.length; n++) {
var member_info = member_split[n];
var validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
if (member_info.search(validRegExp) == -1){
g_form.showFieldMsg('u_group_members', "Group Members contains an invalid email address. " , 'error');
}
else if (member_info.search(validRegExp) == 0) {
g_form.showFieldMsg('u_group_members', "Group Members email addresses are valid");
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2015 03:11 PM
looks right to me i would throw up an alert before the if statement and alert member_info
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2015 03:16 PM
Leslie,
I have checked the above code in demo instance and it works correctly there, can you try it another browser
The code I have used is :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//str1 is the name of the variable
var group_members = g_form.getValue('str1');
var member_split = group_members.split(',');
for (var n = 0; n < member_split.length; n++) {
var member_info = member_split[n];
var validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
if (member_info.search(validRegExp) == -1){
g_form.showFieldMsg('str1', "Group Members contains an invalid email address. " , 'error');
}
else if (member_info.search(validRegExp) == 0) {
g_form.showFieldMsg('str1', "Group Members email addresses are valid");
}
}
//Type appropriate comment here, and begin script below
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2015 07:14 PM
Hello the issue I was having was there was a space in between my test email addresses. I just needed to trim the whitespace out of the string in the array and it works now. Thank you everyone for the responses!
I added this trim to my code.
var member_info = trim ? member_split[n].trim() : member_split[n];