Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

15 of 23 people (65%) answered Yes
Recently 6 of 10 people (60%) answered Yes


Check a field where spaces, digits, alphabets are ok but no special characters

Apr 6th, 2008 19:25
ha mo, Randy Harmelink, Colin Fraser, d m,

Another option is to use a regular expression.  Then you wouldn't need 
the goodchar() function.
Instead of validating with:
    if (!goodchar(this.value)) 
The regular expression would be:
    if (this.value.match(/[^ a-zA-Z0-9]/))
This is basic field validation, so try : 
<script language="JavaScript"><!--
function goodchar(string) {
var good=" 
    for (var i = 0; i < string.length; i++) {
       if (good.indexOf(string.charAt(i)) == -1) {
          return false; }
       else { 
          return true;
<input type="text" onChange="if (!goodchar(this.value)) alert('Not a  
valid character')">
The space occurs at the start of the good string. You can change this
around to suit yourself, like you may want to set the good string up as
an array.