Difference between revisions of "Removing Special Characters"

From SmartWiki
Jump to: navigation, search
m
(See Also)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The following technique can be used to prevent a user from entering special characters in designated form fields.<br>
+
{{JavaScript Syntax}}
 +
The following technique can be used to prevent a user from entering special characters in [[Custom Fields]].<br>
  
 
Special characters can cause problems, particularly when the data is exported to other applications.<br>
 
Special characters can cause problems, particularly when the data is exported to other applications.<br>
Line 6: Line 7:
  
 
==Procedure:==
 
==Procedure:==
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(/['''''Search Expression''''']/'''flag''',' '''New String''' ');"
+
  onchange="this.value=this.value.replace(/['''''Search Expression''''']/'''flag''',' '''New String''' ');"
  
 
where
 
where
Line 22: Line 23:
 
==Options==
 
==Options==
 
For Search Expression you can use
 
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 character that is '''not''' a character from the basic Latin alphabet, an integer, or the underscore.
 
* \w = Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_]
 
* \w = Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_]
* '\'' = single quotes
+
* \D = Matches any character that is '''not''' an integer.
* '\"' = double quotes
+
* \d = Matches all integers. Equivalent to [0-9]
* \d = Matches a digit character in the basic Latin alphabet. Equivalent to [0-9]
 
 
* \0 = NULL character
 
* \0 = NULL character
 
* \s = Matches a single white space 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.
+
* [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 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
+
:* Any individual character can be specified within the square brackets except double quote " which has special meaning.
*  g =   global match  
+
:* If you wish to include the backslash \ or closing square bracket ] you must precede them with a backslash (the first backslash tells the system to read the next character but not interpret it. See Example 2 below).
 +
 
 +
 
 +
For Flag the following options are available:
 +
*  g = global match  
 
*  i =  ignore case  
 
*  i =  ignore case  
  
  
Using ''onChange'' will replace the characters as soon as the user clicks in a field other than the current field, or hits save.<br>
+
Using ''onChange'' will replace the characters as soon as the user clicks away from current field, or hits Save.<br>
 
Using ''onKeyup'' will replace the characters as soon as they are typed.
 
Using ''onKeyup'' will replace the characters as soon as they are typed.
  
Line 43: Line 47:
  
 
==Examples==
 
==Examples==
'''1. To remove all characters that aren't part of the basic Latin alphabet and 0-9 ('''such as . , & ; : ~ ! @ # $ % ^ & * ( ) + " ' and the space key''') and replace them with an underscore (_) actively while typing the text.  
+
1. To remove all characters that aren't part of the basic Latin alphabet and 0-9 (such as . , & ; : ~ ! @ # $ % ^ & * ( ) + " ' <u>including the space key</u>) and immediately replace them with an underscore (_):
<pre>
+
 
onkeyup="javascript:this.value=this.value.replace(/[\W]/g,'_');"
+
onkeyup="this.value=this.value.replace(/[\W]/g,'_');"
</pre>
+
 
 +
 
 +
2. The following code will look for # [ $ ] and \ and replace them with an underscore (_) once the user has clicked away from the field:
 +
 
 +
onchange="this.value=this.value.replace(/[#[$\]\\@]/g,'_');"
 +
 
 +
'''Note''': if you also want to replace double quotes you must omit the double quotes around the javascript statement:
 +
:''onchange=javascript:this.value=this.value.replace(/['@#&"]/g,'_');''
 +
If you want to replace double quotes with a single quote use this:
 +
:''<nowiki>onkeyup=javascript:this.value=this.value.replace(/["]/g,'\'');</nowiki>''
 +
 
  
 +
3. To restrict user to only enter Integers write this code in the HTML tag box:
  
'''2. After user has entered text and clicked away from the field, the following code will look for quotes (" and ') and the ampersand (&) and replace them with an underscore (_).
+
:''<nowiki>onkeyup="this.value=this.value.replace(/[\D]/g,'');"</nowiki>''
<pre>
 
onchange="javascript:this.value=this.value.replace(/['"&]/g,'_');"
 
</pre>
 
  
'''3. To restrict user to only enter Integer numbers write this code in the HTML tag box.
+
==See Also==
<pre>
+
* [[Ssescape|ssEscape]]
onkeyup="javascript:this.value=this.value.replace(/[^\d]/g,'');"
 
</pre>
 
  
[[Category:Custom Fields]][[Category:JavaScript Examples]]
+
[[Category:Custom Fields]][[Category:JavaScript]]

Latest revision as of 09:58, 14 July 2014

This feature uses JavaScript syntax
Text code javascript.gif

The following technique can be used to prevent a user from entering special characters in Custom 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="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 character from the basic Latin alphabet, an integer, or the underscore.
  • \w = Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_]
  • \D = Matches any character that is not an integer.
  • \d = Matches all integers. 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". 
  • Any individual character can be specified within the square brackets except double quote " which has special meaning.
  • If you wish to include the backslash \ or closing square bracket ] you must precede them with a backslash (the first backslash tells the system to read the next character but not interpret it. See Example 2 below).


For Flag the following options are available:

  • g = global match
  • i = ignore case


Using onChange will replace the characters as soon as the user clicks away from current field, or hits Save.
Using onKeyup will replace the characters as soon as they are typed.



Examples

1. To remove all characters that aren't part of the basic Latin alphabet and 0-9 (such as . , & ; : ~ ! @ # $ % ^ & * ( ) + " ' including the space key) and immediately replace them with an underscore (_):

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


2. The following code will look for # [ $ ] and \ and replace them with an underscore (_) once the user has clicked away from the field:

onchange="this.value=this.value.replace(/[#[$\]\\@]/g,'_');"

Note: if you also want to replace double quotes you must omit the double quotes around the javascript statement:

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

If you want to replace double quotes with a single quote use this:

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


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

onkeyup="this.value=this.value.replace(/[\D]/g,'');"

See Also