Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,594
    Thanks
    78
    Thanked 4,386 Times in 4,351 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    javanew (09-21-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,594
    Thanks
    78
    Thanked 4,386 Times in 4,351 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thank you very much


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •