Microsoft Dynamics CRM – Javascript – 6 – Email Validation

In this post we are going to look at email validation in CRM 2013 using Javascript. There is the option to set the field format while creating the field as Email and some entities also come with a pre-defined Email field on their forms.
But sometimes there is a need to create a separate email field on the form to lets say store a secondary email address or there might be the need to store more than one email addresses in a multi-line text field in which the email addresses are separated by a key symbol (e.g. a string of email addresses separated by commas ‘,’ or semi-colon ‘;’).
Also, on top of that in CRM 2013 (or also 2011) even if you set the field format as Email it fails to flag emails without a proper domain part as incorrect, i.e. in CRM 2011/13 a field with format set to ‘Email’ will allow an email like “test@test” and will not raise any error (try for yourself). In cases like these we will need to write our own email validation scripts that make sure that the email entered is in the proper ‘Fully Qualified Domain Address’ format.
In order to validate the email we will make use of regular expressions in javascript and the test() method(more information on RegExp and test() method can be found here: Regex and test() method.

Here is a very simple Email Validation function:

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;
	}
}

I know there may be cases where the Regular Expression given will not work and give incorrect results but this is for demonstration purposes only and works generally well with all email addresses. We can modify the RegEx to validate an email address consistent with our implementation and business objectives.

Now, the above function can be called during an ‘OnChange’ event on a custom or an existing email field. Below is an example of calling the email validation function ‘EmailTest’:

function EmailFieldOnChange()
{	
	try{
		var email = Xrm.Page.getAttribute("new_otheremail").getValue();
		var Result = EmailTest(email);
		alert(Result);
		
		if(Result== true)
		{
			Xrm.Page.getAttribute("new_otheremail").setValue(email);
		}
		else
		{
			Xrm.Page.getAttribute("new_otheremail").setValue(null);
			alert("Wrong Email format");
		}
	}
	catch(err)
	{
		alert(err.message);
	}
}

A couple of things to note about the above code:

1) Using the try and catch blocks will help you isolate any errors in the program quickly allowing to fix them easily.
2) Notice the use of setFocus() method. This can be useful if you want to keep the focus on the email field in case the user enters the wrong email address.

Now, we might also come across situations where we might be asked to validate multiple email addresses in a single-line or a multi-line box separated by a key symbol (like a comma or a semi-colon). In that case we should follow some simple steps to extract all the email addresses individually and validate them.

Here are the steps to do so:

1) Take the multi-line/single-line input.
2) Separate individual string elements on a key symbol.
3) Load the individual string elements in an array.
4) Validate all the individual elements of the array against some email validation function

In our case we will use the ‘EmailTest’ function demonstrated at the start of this post for testing each array element. Here is a sample program:

function SeparateEMail(Input)
{
	var InputEmails = Xrm.Page.getAttribute(Input).getValue();
	var EmailArray = InputEmails.split(",");
	var Flag = true;
	
	for(var i = 0;i < 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");
		Xrm.Page.getControl(Input).setFocus();
	}
}

function CheckEmailString()
{
	try
	{
		//var EmailString = Xrm.Page.getAttribute("new_allemails").getValue();
		//alert(EmailString);
		SeparateEMail("new_allemails");
	}
	catch(err)
	{
		alert(err.message);
	}
}

In the code snippet given above the ‘SeparateEMail’ function takes the name of the input field as the argument.
The ‘InputEmails’ variable stores the value returned from the input field as string and then the ‘EmailArray’ splits the the string stored in ‘InputEmails’ on the key symbol (in this case a comma) and then validates each and every array element against the ‘EmailTest’ function and if an email is incorrect then the function changes the ‘Flag’ false and exists out of the loop but retains focus on the multi-line or single-line field on which it was called.

The ‘SeparateEmail’ function is called from within the ‘CheckEmailString’ function which is triggered on an ‘OnChange’ event on the email field.

This was a small post on email validation in Microsoft Dynamics CRM using Javascript. We looked at simple validation techniques using Regular Expressions and we also came across some new methods like test() and setFocus() (we can call setFocus() during say form load in case we want the user to fill in some important information first and so on).

Hope this post helps you in your CRM journey. Have a great time and

function Happiness(){
    alert(Code Lazily);
}

1 thought on “Microsoft Dynamics CRM – Javascript – 6 – Email Validation

  1. Getting the knowledge of Microsoft Dynamics CRM – Javascript – 6 – Email Validation is quite helpful in learning how to work with a ease.

    Like

Leave a comment

search previous next tag category expand menu location phone mail time cart zoom edit close