Saturday, December 13, 2014

Applying Validation to the Fields in MS-CRM 
with JScript 



 Validating Fields in MS-CRM

 1 ) E-mail Field Validation

function checkEmail(emailField)
{
var email=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([azA-Z])+/;
if(email.test(emailField))
{
// alert("true");
return true;
}
}

The checkEmail() function we have just presented is meant to be used
as a generic function we can call for any field we need to validate as 
e-mail. In order to validate the field we have just created we can create
a new function, retrieve the value of the field, and pass it into
our function.


2) For Multiple Email Container Field

function checkInput(inputField)
{
var flag = true;
var emailArray = inputField.split(",");
for(var i = 0; i < emailArray.length; i++)
{
if(!checkEmail(emailArray[i]))
{
flag = false;
break;
}
}
return flag;
}

3) Handling erroneous input

alert("You have entered an incorrect email address!");

var field = Xrm.Page.ui.controls.get("fieldName");
field.setFocus();

4) URL Field Validation

function ValidateLinkedIn()
{
var _url = Xrm.Page.getAttribute("new_linkedin").getValue();
var _regex = /^http:\/\/www.linkedin.com\/profile\/view\?.*/;
if(!_url.match(_regex))
{
alert("The URL entered is not a LinkedIn profile!");
}
}

5) Ticker Field Validation

function CheckTicker()
{
var tickerValue = Xrm.Page.getAttribute("new_ticker").getValue();
if(!CheckTicker(tickerValue))
{
alert("The Ticker symbol provided does not exist!");
}
}

6) Formatting PhoneNumber Field Validation


// call this function to format any north american phone number(10 digit)
// in the following format: (xxx) xxx-xxxx
// pass context to the function
function FormatPhoneNo(context) {
try {
var nvsField = context.getEventSource().getValue();
var nvsTmp = nvsField;
if (typeof (nvsField) != "undefined" && nvsField != null)
{
nvsTmp = nvsField.replace(/[^0-9]/g, "");
switch (nvsTmp.length) {
case 10:
nvsTmp = "(" + nvsTmp.substr(0, 3) + ") " + nvsTmp.substr(3, 3) + "-" + nvsTmp.substr(6, 4);
break;
default:
alert("Phone must contain 10 numeric digits.");
break;
}
}
context.getEventSource().setValue(nvsTmp);
}
catch (err) {
}
}


7) Formatting Postal Field Validation

// Function to format postal code
// for both Canadian and US postal codes
function FormatPostalCode(context)
{
var oField = context.getEventSource().getValue();
var sTmp;
if(typeof(oField) != "undefined" && oField != null)
{
// check for US ZIP code
if(oField.match(/^[0-9]{5}$/))
{
context.getEventSource().setValue(oField);
return true;
}
// check for Canadian postal code
sTmp = oField.toUpperCase();
if (sTmp.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/))
{
sTmp = sTmp.substr(0,3) + " " + sTmp.substr(3,3);
context.getEventSource().setValue(sTmp);
return true;
}
if (sTmp.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/))

{
context.getEventSource().setValue(sTmp);
return true;
}
// code is invalid
// alert("Incorrect ZIP/Postal Code format.");
// code could be any other country, so leave as is
}
}



No comments:

Post a Comment