Validate multiple email id in a field present in catalog form separated by commas

AKASH BANERJEE
Mega Guru

Please let me know how we can check if the value entered in a catalog form field is multiple email id seperated by commas. How we can implement using onchange client script.

6 REPLIES 6

Aman Kumar S
Kilo Patron

You can simply use, split function to get it.

Now lets say you have a field with list of values which are separated by comma.

Sample script:

var a = "a,b,c";
var count = a.split(',');
gs.info(count.length);

Output : 3

 

You can similarly in a client script:

var emails = g_form.getValue("name_of_the_email_field");

var c = emails.split(',');

if(c.length > 1){

//single email

}

else{

//multiple emails

}

 

Best Regards
Aman Kumar

Hi @Aman Kumar S ,

 

The code should validate the multiple email id's

For example  if user enter test1@example.com,tes2@example.com then it is correct if he enters test1@example.com,test2 then it s...

Ah! My bad, it is not simple, apologies for my ignorance.

Found something that should assist you.

https://www.servicenow.com/community/developer-forum/validate-multiple-email-addresses-separated-by-...

 

Best Regards
Aman Kumar

AnubhavRitolia
Mega Sage
Mega Sage

Hi @AKASH BANERJEE 

 

Lets assume that your variable name is 'multiple_email_id'.

You can use below onChange Client Script to check if each value is email Id or not:

 

var emailIDs = g_form.getValue('multiple_email_id');
var emailIDList = emailIDs.toString().split(','); 
var regex = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$";  // Regalar Expression for Email ID validation

for(i=0; i<emailIDList.length ; i++)
{

      var result = emailIDList[i].matches(regex);
      if(result) {
         alert(emailIDList[i] + "is a valid email ID"); // Modify this part according to your requirement
      } else {
         alert(emailIDList[i] + "is not a valid email ID"); // Modify this part according to your requirement
      }

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023