faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

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

Entry

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=" 
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (var i = 0; i < string.length; i++) {
       if (good.indexOf(string.charAt(i)) == -1) {
          return false; }
       else { 
          return true;
       }    
} 
//--></script>
<form>
<input type="text" onChange="if (!goodchar(this.value)) alert('Not a  
valid character')">
</form>
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.
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com