Changes

Jump to: navigation, search

Removing Special Characters

1,655 bytes added, 14:58, 14 July 2014
See Also
{{JavaScript Syntax}}The following technique can be used to prevent a user from entering special characters in designated form fields[[Custom Fields]].<br>
Special characters can cause problems, particularly when the data is exported to other applications.<br>
==Procedure:==
1. Include the following code in the '''HTML Tag box ''' of the [[Custom Fields|custom field]] you wish to restrict::''onChange onchange="javascript:this.value=this.value.replace(/['''''character setSearch Expression''''']/g'''flag''',' '''New String'''');" where *'''Search Expression''' specifies a string value to find.*'''New String''' specifies the replacement characterstring.*'''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''.
2. Create a [[Custom Field Type: Special – Browser Script|Browser Script custom field]] with the following function:<br>
:''function valid(o)''
:''{''
:''var re=/['' '''character set''' '']/g''
:''o.value = o.value.replace(re,' '''''replace character''''' ');''
:''}''
where ==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' defines in "ache". :* Any individual character can be specified within the square brackets except double quote " which characters has special meaning.:* If you wish to include the backslash \ or closing square bracket ] you want must precede them with a backslash (the first backslash tells the system to remove, and 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 characterthe characters as soon as the user clicks away from current field, or hits Save.<br>Using ''onKeyup'' defines what you want will replace the special characters replaced withas soon as they are typed.
==Options==
==Examples==
1. To remove all characters that aren't part of the basic Latin alphabet and 0-9 (such as . , & ; : ~ ! @ # $ % ^ & * ( ) + " ' <u>including the space key<pre/u>function valid) and immediately replace them with an underscore (o_) :{ var re onkeyup=/[&'"]/g othis.value = othis.value.replace(re/[\W]/g,'_');"}</pre>2. The example above following code will remove all & " look for # [ $ ] and ' characters \ and replace them with an underscore (_). If you would rather simple remove once the characters you should use the following as the last line of user has clicked away from the functionfield: o.value = o.value.replace(re,'');
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>''
var re=/[\W]/g3. To restrict user to only enter Integers write this code in the HTML tag box:
:''<nowiki>onkeyup="this.value=this.value.replace(/[\D]/g,'');"</nowiki>''
Note: this can be extended to check for double quotes or single quotes or only for integer numbers.==See Also==* [[Ssescape|ssEscape]]
[[Category:Custom Fields]][[Category:JavaScript Examples]]
8,849
edits

Navigation menu