Difference between revisions of "Removing Special Characters"

From SmartWiki
Jump to: navigation, search
Line 8: Line 8:
 
Include the following code in the HTML Tag box of the [[Custom Fields|custom field]] you wish to restrict:
 
Include the following code in the HTML Tag box of the [[Custom Fields|custom field]] you wish to restrict:
  
  onchange="javascript:this.value=this.value.replace(/['''''character set''''']/g,' '''replacement character''' ');"
+
  onchange="javascript:this.value=this.value.replace(/['''''Search Expression''''']/'''flag''',' '''New String''' ');"
 +
 
 +
where
 +
 +
*'''Search Expression''' specifies a string value to find.
 +
*'''New String''' specifies the replacement string.
 +
*'''flag ''' is a command line switch used to control outcome of the replace operation.
  
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''.
 
If you want the replacement to happen actively while the user is typing into the field use ''onKeyup'' instead of ''onChange''.
Line 16: Line 21:
  
 
==Options==
 
==Options==
For Character set you can use
+
For Search Expression you can use
* \W
+
* \W = Matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Z a-z 0-9 _]
* \'
+
* \w = Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_]
* \"
+
* \' = single quotes
* \d
+
* \" = double quotes
 +
* \d = Matches a digit character in the basic Latin alphabet. Equivalent to [0-9]
 +
* \0 = NULL character
 +
* \s = Matches a single white space character
 +
* [xyz]= A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen.
 +
        For example, [abcd] is the same as [a-d]. They match the 'b' in "brisket" and the 'c' in "ache".
 +
For Flags following options are available
 +
*  g =  global match
 +
*  i =  ignore case
  
  

Revision as of 13:12, 24 April 2009

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:

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

onchange="javascript:this.value=this.value.replace(/[Search Expression]/flag,' New String ');"

where

  • Search Expression specifies a string value to find.
  • New String specifies the replacement string.
  • flag is a command line switch used to control outcome of the replace operation.


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


Options

For Search Expression you can use

  • \W = Matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Z a-z 0-9 _]
  • \w = Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_]
  • \' = single quotes
  • \" = double quotes
  • \d = Matches a digit character in the basic Latin alphabet. Equivalent to [0-9]
  • \0 = NULL character
  • \s = Matches a single white space character
  • [xyz]= A character set. Matches any one of the enclosed characters. You can specify a range of characters by using a hyphen.
       For example, [abcd] is the same as [a-d]. They match the 'b' in "brisket" and the 'c' in "ache".

For Flags following options are available

  • g = global match
  • i = ignore case


Examples

1. To remove all & ~ ! @ # $ % ^ & * ( ) + " and ' characters and replace them with an underscore (_) actively while typing the text.

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


2. After user has entered text and clicked away from the field, the following code will look for " and ' quotes and replace them with an underscore (_).

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

3. To restrict user to only enter Integer numbers write this code in the HTML tag box.

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