Removing Special Characters

Revision as of 11:31, 24 April 2009 by Madhulika (talk | contribs)

Revision as of 11:31, 24 April 2009 by Madhulika (talk | contribs)

The following technique can be used to prevent a user from entering special characters in designated form fields.

Special characters can cause problems, particularly when the data is exported to other applications.

For example, if you wish to export the data to Microsoft Excel, any text fields with ampersands (&) or quotation marks (" or ') often cause formatting issues for Excel.


Procedure:

1. Include the following code in the HTML Tag box of the custom field you wish to restrict:

onchange="javascript:this.value=this.value.replace(/[character set]/g,' replacement character ');"

where character set defines which characters you want to remove, and replacement character defines what you want the special characters replaced with.

If you want the replacement to happen actively while the user is typing into the field use onKeyup instead of onChange.


Options

For Character set you can use

  • \W
  • \'
  • \"
  • \d


Examples

onkeyup="javascript:this.value=this.value.replace(/[\W]/g,'_');"

The example above will remove all & ~ ! @ # $ % ^ & * ( ) + " and ' characters and replace them with an underscore (_). If you would rather simple remove the characters you should use the following as the last line of the function: