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

1 comment:

  1. Thanks Andriy. I found your post very useful. I actually implemented this today for one of our fields that is commonly abused with special characters.

    While users were typing in special characters, our main issue with the field was that users would paste in data from another system. Users would often included special unicode characters that actually caused plugins to die-hard (weren't compatible with XML).

    The attachevent->onkeypress solution does not catch this. I had to implement a second-check on-change. It would loop through the characters in the field and filter out invalid characters.

    Thanks again for the post!

    ReplyDelete