Jaun Lloyd
Jaun Lloyd

Reputation: 7

Validate email string after split

I have the following string:

var proc = new SAPayslips();
proc.RuleCustomValue = "document.xml|[email protected];[email protected];name@domain,co.za";

The first value is the name of a xml document, and the rest are emails I would like to utilize.

I can successfully split them and use them but I have a problem with the validation. I would like to throw an exception if the email address doesn't contain an @ char.

// retrieves document name
customValues = _ruleCustomValue.Split('|');
// retrieves emails
emails = customValues[1].Split(';');

if(!customValues[1].Contains("@"))
    throw new System.InvalidOperationException("Invalid Email adress,");

It doesn't throw the exception when there is no @

Upvotes: 1

Views: 1539

Answers (5)

Adil
Adil

Reputation: 148150

You need to check emails to search in array of emails instead of customValues[1] that is a string. Calling Contains on customValues[1] will return true if it contains only one @.

You need to iterate through array of find if any of array element does not contain @ in it.

foreach (var email in emails)
  if(!email.Contains("@"))
  { 
      throw new System.InvalidOperationException("Invalid Email adress,");
  }

You can also use linq, using Enumerable.Any

if(emails.Any(email=>email.indexOf("@") == -1))
    throw new System.InvalidOperationException("Invalid Email adress,");

Upvotes: 4

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

Try this

// retrieves document name
customValues = _ruleCustomValue.Split('|');
// retrieves emails
emails = customValues[1].Split(';');
foreach(var email in emails)
{
    if (!EmailValidated(email))
    {
       throw new System.InvalidOperationException("Invalid Email adress,");
    }
}


private static bool EmailValidated(string emailAddress)
{
    const string pattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
                                   + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                                   + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                                   + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
    var match = Regex.Match(emailAddress.Trim(), pattern, RegexOptions.IgnoreCase);
    return match.Success;
}

Upvotes: 0

sulhadin
sulhadin

Reputation: 516

Checking whether or not there is "@" inside is not the exact solution for determining that it is an email adress, I think you are going to need regex pattern for this,

example;

function isEmail(email) {
    var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
    return pattern.test(email);
};

check it and throw an exception;

if( !isEmail("[email protected]") ) { *here we go! throw exception!*}

here more information about this; link

I hope it will be helpful.

Upvotes: 2

User12345
User12345

Reputation: 1582

You are validating that any email contains a @, not that each email contains a @.

You should get each e-mail and validate through that each e-mail NOT for items in customvalues.

You can try this code:

// retrieves document name
        string[]customValues = _ruleCustomValue.Split('|');
        // retrieves emails
        string[] emails = customValues[1].Split(';');

        foreach (string email in emails)
        {
            if (!email.Contains("@"))
            {
                throw new System.InvalidOperationException("Invalid Email adress,");
            }
        }

Upvotes: 0

Ben
Ben

Reputation: 2523

Firstly, it will only throw this in one location (because you have only specified one location: customValues[1])

Secondly, the item you have specified is actually the second item in the array, as all collections start at 0.

What you may want to do instead is go through a loop, and check each email string:

foreach (string s in customValues)
{
    if (!s.Contains("@"))
        // throw exception
    else
        // do stuff...
}

Upvotes: 0

Related Questions