How to do a Yup email validation on space separated values?

I have a string data e.g. str="[email protected] [email protected]" I need to perform the yup validation on this string how can I do it.

I have found an answer for comma separated value here github. I tried to tweak this answer by changing split(/[s,]+/) to str.split("\s+") but It is not working.

Answer

You can split the emails from the string with the following regex:

// this will split the emails regardless of how many white-spaces there are between emails
str.split(/[s]+/);

Check and run the following Code Snippet to see the above regex split the white-space separated emails:

// emails with random amount of white-spaces between them
const str="[email protected] [email protected]   [email protected]       [email protected]";

str.split(/[s]+/).map(e=> {
    console.log("Email: " + e);
})

However, if there is equal white-space between the emails in your string, you can just specify that in the split() method without using regex like this:

// change the number of spaces between the quotes according to the number of white-spaces between the emails in your string
str.split(' ');

Check and run the following Code Snippet for a practical example of the above approach:

const str="[email protected]    [email protected]    [email protected]    [email protected]";

str.split('    ').map(e=> {
    console.log("Email: " + e);
})

Leave a Reply

Your email address will not be published. Required fields are marked *