www.webdeveloper.com
Results 1 to 4 of 4

Thread: search() problem

  1. #1
    Join Date
    Mar 2013
    Posts
    5

    search() problem

    Hi,
    I want to use search() to check if a string contains any illegal characters. Problem is that I can't find out how to do it when the illegal variables come from a string. It should be something like below. Only this doesn't work.

    Code:
    function check_string(mystr,taboos)
    { var patt = "/["+taboos+"]/"; // this doesn't work but I don't know what will
      if(mystr.search(patt) == -1)
        return true;
      else
      { alert("The following characters are not allowed: "+taboos+" - "+myelt.value);
        return false;
      }
    
    if(!check_string("Hello world","<>;=#{}")) // string should not contain any of these characters
          return;
    }

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Try:
    Code:
    var patt = new RegExp( "[" + taboos + "]" );

  3. #3
    Join Date
    Mar 2013
    Posts
    5

    Thanks

    Thank you for your help

  4. #4
    Join Date
    May 2013
    Location
    New York
    Posts
    1
    You can get answer of this question using search function also. Please change into the given two lines and your code will work fine. The search() method searches a string for a specified value, or regular expression, and returns the position of the match.

    Use:
    var patt = taboos;
    if(mystr.search(patt) > 0)

    There may be error in return keyword so you can alert something instead of it to know actual result

Thread Information

Users Browsing this Thread

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

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