SSN Validation


While Dealing with the forms which contains personal information of Person or contact, we have to write validations for the fields in which we want to take input from user.

1) SSN Validation: –

SSN is the Social Security Number which is unique identification number for American Citizen

So the format of Social Security Number is: -SSN is 9-digit Integer

XXX-XX-XXXX

Eg: -123-45-6789

JavaScript to Implement validation of SSN on Field: –

Paste the following Scripts in Web Resource and Set OnChange Event

function SSNValidation()
{
     var value = Xrm.Page.getAttribute(‘AttributeName’).getValue();   
     var pattern =/^(?!(000|666|9))\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;

    if(!value.match(pattern) )         
     {           
         alert(“Please enter numeric value only (enter your 9 digit social security number)”);
         Xrm.Page.getControl(‘ AttributeName ‘).setNotification(“Invalid social Security number”);    
         Xrm.Page.getAttribute(‘ AttributeName ‘).setValue(null);          
         return false;
     }      
     if(value!=null)
     {     
         var  v1=Xrm.Page.getAttribute(‘ AttributeName ‘).getValue().substr(0,3);
         var v2=Xrm.Page.getAttribute(‘ AttributeName’).getValue().substr(3,2);
         var  v3=Xrm.Page.getAttribute(‘AttributeName’).getValue().substr(5,4);        
         Xrm.Page.getAttribute(‘AttributeName’).setValue(v1+”-“+v2+”-“+v3);
         Xrm.Page.getControl(‘AttributeName’).clearNotification();
                        
     }
}