www.webdeveloper.com
+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2005
    Posts
    268

    Best way to check for multiple characters in a string

    I have pages that print Code 39 bar codes; however, I do not have development in place to dissalow the entry of the invalid characters that Code 39 requires. For this discussion, you dont' really need to knwo what Code 39 is, just the list of invalid characters.

    So, upon data entry, I would like to check the string to ensure the following special characters are NOT present in the string, if they are, then I'll alert them or do something:

    !
    @
    #
    ^
    &
    *
    (
    )
    _
    =
    {
    }
    [
    ]
    \
    |
    :
    ;


    <
    >
    ,
    ?

    Because there are so many characters to choose from, I am not certain the best code for this. What do you all suggest would be the best route to take?

  2. #2
    Join Date
    Feb 2003
    Location
    Chicago Area, IL
    Posts
    5,736
    Regular expressions are your friend in this case:
    Code:
    var invalidChars = /[^!@#^\&\*\(\)_=\{\}\[\]\\|:;“‘<>,\?]/;
    
    if (str.match(invalidChars)) {
      alert("Bad characters!");
    }
    else {
      alert("Good characters");
    }

  3. #3
    Join Date
    Aug 2005
    Posts
    268
    Close, but not quite there, I'll describe a bit more.

    They will be entereing in a piece of data, 9/10 they will be pasting it in. So, they may enter in ABC123^4, or ABC1234

    Currently, both are tagged as "Bad Characters" prompt from the above code, when only the first should be.

  4. #4
    Join Date
    Feb 2003
    Location
    Chicago Area, IL
    Posts
    5,736
    I had the logic a little wrong. The regular express tests to make sure those characters are not there. Revised code is below:
    Code:
    var invalidChars = /[!@#^\&\*\(\)_=\{\}\[\]\\|:;“‘<>,\?]/;
    
    if (str.match(invalidChars)) {
      alert("Bad characters!");
    }
    else {
      alert("Good characters");
    }
    The "^" after the opening "[" causes the character class in the regular expression to be inverted, meaning "match any character that is not a, b, c or d". I changed "[^" to simply "[". That should be all you need.

  5. #5
    Join Date
    Aug 2005
    Posts
    268
    So far so good, thanks a bunch!

  6. #6
    Join Date
    Jan 2013
    Posts
    1
    Code 39 (also known as "USS Code 39", "Code 3/9", "Code 3 of 9", "USD-3", "Barcode 3 of 9", "Type 39") is a linear barcode symbology that can encode upper-case alphanumeric A-Z and 0-9 and some special characters, with lower-case a-z added for Code 39 Extension.
    Sorry, i have no ideas , but you can try to generate barcode in asp.net, including code 39 and other linear barcodes or 2D barcodes.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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



Recent Articles