Changes

Jump to: navigation, search

Removing Special Characters

353 bytes removed, 16:31, 24 April 2009
no edit summary
==Procedure:==
1. Include the following code in the HTML Tag box of the [[Custom Fields|custom field]] you wish to restrict:
:''onChange="javascript:this.value.replace(/['''''character set''''']/g,' '''''replacement character''''' ')
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''.
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 '''character set''' defines which characters you want to remove, and '''replace character''' defines what you want the special characters replaced with.
==Options==
For Character set you can use* \W* \'* \"* \d
==Examples==
<pre>
function valid(o) { var reonkeyup=/[&'"]/g ojavascript:this.value = othis.value.replace(re/[\W]/g,'_');}"
</pre>
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:
o.value = o.value.replace(re,'');
 
 
 
 
var re=/[\W]/g
 
NoteThe 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: this can be extended to check for double quotes or single quotes or only for integer numbers.
[[Category:Custom Fields]][[Category:JavaScript Examples]]
Smartstaff
137
edits

Navigation menu