title: CRM: Javascript to Check Email Validation and Prevent Saving if not Valid
body: <p>On CRM 2013 on-premise, I'm working to write a javascript that checks for email validation. The field can contain a list of email address, however if the email is not valid, it will not let the users save the form.</p>
  
  <p>I got the splitting and validating to work fine now.</p>
  
  <p>However I continue to have problems to prevent users from saving the form.</p>
  
  <p>On the OnChange, I check the box on the "Pass execution context as first parameter"</p>
  
  <p>I user the preventDefault() function as suggested by an MSDN article however I keep getting error message "Unable to get property 'preventDefault' of undefined or null reference".</p>
  
  <p>Any idea is appreciated. Here's my code:</p>
  
  <pre><code>function EmailTest(EmailField)
  {
  var Email = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
  if(Email.test(EmailField))
      {
      return true;
      }
  else
      {
      return false;
      }
  } 
  
  function CheckEmailString(context)
  {
  try
  {
      var EmailString = context.getEventSource().getValue();
      if (EmailString != null)
      {
          var separator = [" ", ",", ";", "|"];
          var EmailArray = EmailString.split(separator);
          var Flag = true;
  
          for(var i = 0;i &lt; EmailArray.length;i++)
          {
              if(!EmailTest(EmailArray[i]))
              {
                  Flag = false;
                  break;          
              }
          }
          if(Flag != true)
          {
              alert("The list of emails entered contain invalid email format. Please re-enter");
              context.getEventArgs().preventDefault();
          }
      }
  }
  catch(err)
  {
      alert(err.message);
  }
  </code></pre>
  
  <p>}</p>