Sunday, June 12, 2011

How to allow users to input allowed characters in Microsoft Dynamics CRM 4.0

I have already posted here how to restrict user to input special characters to text fields. Following script will allow user to input only predefined symbols to text field.

Add following script to OnLoad event handler:

FilterCharacters = function(fieldname)
{
document.getElementById(fieldname).attachEvent("onkeypress", function()
{
var allowed = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM 1234567890,.-";
var pressedkey = String.fromCharCode(event.keyCode);
if (allowed.indexOf(pressedkey) != -1)
return true;

return false;
});
}


if (crmForm.FormType == 1 || crmForm.FormType == 2)
{
//call this method for each field to switch on restriction
FilterCharacters('name');
}