View Full Version : I want to do some validation for speacial character...

03-17-2005, 05:47 PM
I want to validate data input when user try to input special characters like "@#$%%^&*() ... and those characters like , , ...

can i use this method?

str = str.toLowerCase();
var strOk = ' -abcdefghijklmnopqrstuvwxyz0123456789';
for ( var i = 0; i < str.length; i++ )
if ( strOk.indexOf(str.charAt(i)) < 0 )
return false;
return true;

Any idea for this?

03-17-2005, 06:04 PM
using RegExp might be probably the easiest way. Probably it would be more hopeful if you would say which characters may pass.

Well, for the begining, considering that you want to pass only numbers and letters, try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function valid(field) {
var re = /^[0-9-A-z]*$/;
if (!re.test(field.value)) {
alert('Value must be only numbers or letters!');
field.value = field.value.replace(/[^0-9-A-z]/g,"");
<textarea name="text" cols="" rows="" onkeyup="valid(this)"></textarea>

You may add later which other characters u wanna pass, if needed