hello i am using javascript to validate field
i am checking special characters.. the code i am implementing validates all the special characters except _ underscore..
<script type="text/javascript" language="javascript">
function validateForm()
{
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?_";
for (var i = 0; i < document.reg_form.txtusername.value.length; i++)
{
if (iChars.indexOf(document.reg_form.txtusername.value.charAt(i)) != -1)
{
alert ("Special Characters are not allowed.");
return false;
}
}
return true;
}
</script>
for this filed
<input name="txtusername" type="text" id="txtusername" maxlength="10" style="background-color:#CCC" />
but its not validating the underscore
characters in strings can be addressed as if the string were an array (which, if you think about it, it is). Since you’re looping through the string char-per-char, again, as you would with an array, why not be consistent in your logic?
That should work… Also: there’s no real need to escape the single quote, since the iChar string is delimited by double quotes… if all else fails, you might want to try omitting that backslash, too. Though I don’t think that’s what’s causing the problem
It is working: here