SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alerting User for Blank Search Queries

    Hi,

    I have a small problem in trying to produce an alertbox should a user not enter anything into a search dialog box. Does a blank query convert to a variable in JavaScript as a Null? Here is the script (the illegal character search works fine, the blank query does not):

    <script type='text/javascript'>

    // Javascript function to test for illegal characters or a blank query
    function charTest(str)
    {
    var v;

    if(( v=str.match( /(\<|\>|\||\/g )))
    alert('You have entered the following illegal character/s (excluding commas): '+v);

    if(v = '')
    alert('You forgot to enter a search query!');

    return !v;
    }

    </script>

    And this is the form to call this script:

    <form name="search" method="post" action="search_result.php" onsubmit="return charTest(this.find.value)">
    <p class="header">Enter a query: <input type="text" name="find" />
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </form>

    Thanks for any help!

    Christian

    Edit: disregard that illegal characters code with the smiley, it works fine anyway!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    blank search

    If you declare the varaible as empty it wont be NULL
    HTML Code:
    var v = '';
    As for smilies, you can put code inside BB tags (please do) or disable smilies when you post.

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added that in for the 'var v' part, but the code still doesn't work

    Is there a way to str.match for an empty query, like I did for the illegal characters?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,255
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    1 2 or 3 =s

    Sorry, I didn't notice it before (please use the BB tags) but you missed an equal sign
    PHP Code:
    if(''
    should be
    PHP Code:
    if(== ''
    or
    PHP Code:
    if(=== ''

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    v is an array returned by String.match, it does not represent the user's input. You need to test str:
    Code:
    function charTest(str)
    {
     var v, ok=false;
    
     if(( v=str.match( /(\<|\>|\||\;)/g )))
      alert('You have entered the following illegal character/s (excluding commas): '+v);
     else
      if(str == '')
       alert('You forgot to enter a search query!');
      else
       ok=true;
    
      return ok;
    }

  6. #6
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much guys! Works perfectly.

    Christian

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks aswell, I was about to ask a similar question, but this solved it instantly.


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
  •