Difference between revisions of "Removing Special Characters"

From SmartWiki
Jump to: navigation, search
Line 1: Line 1:
To prevent a user from entering special characters in designated form fields.
+
The following technique can be used to prevent a user from entering special characters in designated form fields.<br>
  
Write the following code in Javascript box of a Custom field of type Special-Browser Script:
+
Special characters can cause problems, particularly when the data is exported to other applications.<br>
 +
:''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 Fields|custom field]] you wish to restrict:
 +
:''onChange="javascript:this.value.replace(/['''''character set''''']/g,' '''''replacement character''''' ')
 +
 
 +
 
 +
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==
 +
 
 +
 
 +
 
 +
==Examples==
 
<pre>
 
<pre>
var r={'special':/[\W]/g}
+
function valid(o)
function valid(o,w){
+
{
   o.value = o.value.replace(r[w],'');
+
  var re=/[&'"]/g
 +
   o.value = o.value.replace(re,'_');
 
}
 
}
 
</pre>
 
</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
  
Write the following code in the HTML Tag box of the custom field:
 
<pre>
 
onkeyup="valid(this,'special')" onblur="valid(this,'special')"
 
</pre>
 
  
 
Note: this can be extended to check for double quotes or single quotes or only for integer numbers.
 
Note: this can be extended to check for double quotes or single quotes or only for integer numbers.
  
 
[[Category:Custom Fields]][[Category:JavaScript Examples]]
 
[[Category:Custom Fields]][[Category:JavaScript Examples]]

Revision as of 12:07, 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:

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

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


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

2. Create a Browser Script custom field with the following function:

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

Examples

function valid(o) 
{
  var re=/[&'"]/g
  o.value = o.value.replace(re,'_');
}

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


Note: this can be extended to check for double quotes or single quotes or only for integer numbers.