Knowledge found and lost while working with Microsoft Dynamics CRM
RSS icon Home icon
  • Creating a legal filename in JavaScript

    Posted on April 29th, 2007 Mitch Milam Print Print No comments

    I am working on a project this weekend ( yeah, I know ) where I will be creating a folder in the file system based on the Account Name for an Account within CRM.  Since it is possible to have characters within an Account Name that would be invalid in a file or folder name, they must be replaced.

    The following JavaScript snippet will replace any invalid character within the Account Name with an underscore. 

    // Create a dummy account name for testing purposes.
    crmForm.all.name.DataValue = "this?is/\ a* test";
    
    if (crmForm.all.name != null)
    {
    	// The replaceChar should be either a space
    	// or an underscore.
    	var replaceChar = "_";
    	var regEx = new RegExp('[,/\:*?""<>|]', 'g');
    	var Filename = crmForm.all.name.DataValue.replace(regEx, replaceChar);
    
    	// Show me the new file name.
    	alert(Filename);
    }
    
    Customization, Development, Dynamics CRM
    1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
    2,367 views

    Leave a reply