...

View Full Version : Resolved Search for special characters



javanew
09-21-2011, 03:42 AM
First of all thanks for all the help you are providing.

I am looking to alert a user if they type in an character that I am going to not allow for their input box



function IsExc (str){
var AllChr = "~ # $ % ^ { } [ ] ' ,";
var AC= AllChr.split(" ");
var SC = 0;
for (SC=0; SC <= AC.length; SC++){
var ACS = AC[SC];
if (str.search(ACS) >0 ) alert ('Cannot use the characters' + AllChr )
}
}


For reasons I don't understand the code will not work

Old Pedant
09-21-2011, 04:11 AM
Easier way:


function IsExc (str)
{
var ng = /[\~\#\$\%\^\{\}\[\]\'\,]/;
if ( ng.test(str) ) alert("Illegal character");
}

Or maybe better:


function IsExc (str)
{
var ng = /[^\w\s]/;
if ( ng.test(str) ) alert("Illegal character--only letters, digits, underline, and space allowed");
}

Old Pedant
09-21-2011, 04:13 AM
If you insist on doing it one character at a time:


function IsExc (str)
{
var AllChr = "~#$%^{}[]',";
var SC = 0;
for (SC=0; SC <= AllChr.length; SC++)
{
if (str.indexOf( AllChr.charAt(SC) ) >= 0 ) alert ('Cannot use the character ' + AllChr.charAt(SC) );
}
}

javanew
09-21-2011, 11:20 AM
Thank you very much



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum