Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-21-2011, 02:42 AM   PM User | #1
javanew
New to the CF scene

 
Join Date: Apr 2005
Posts: 8
Thanks: 5
Thanked 0 Times in 0 Posts
javanew is an unknown quantity at this point
Search for special characters

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

Code:
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

Last edited by javanew; 09-21-2011 at 10:19 AM..
javanew is offline   Reply With Quote
Old 09-21-2011, 03:11 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Easier way:
Code:
function IsExc (str)
{
   var ng = /[\~\#\$\%\^\{\}\[\]\'\,]/;
   if ( ng.test(str) ) alert("Illegal character");
}
Or maybe better:
Code:
function IsExc (str)
{
   var ng = /[^\w\s]/;
   if ( ng.test(str) ) alert("Illegal character--only letters, digits, underline, and space allowed");
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
javanew (09-21-2011)
Old 09-21-2011, 03:13 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If you insist on doing it one character at a time:
Code:
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) );
    }
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-21-2011, 10:20 AM   PM User | #4
javanew
New to the CF scene

 
Join Date: Apr 2005
Posts: 8
Thanks: 5
Thanked 0 Times in 0 Posts
javanew is an unknown quantity at this point
Thank you very much
javanew is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:08 AM.


Advertisement
Log in to turn off these ads.