SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast overbyte's Avatar
    Join Date
    Jun 2003
    Location
    yes, he said exactly london, england
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question a little javascript form validation help

    Hi guys, hope you can help

    i am doing an article-based website in php/mysql and i've added a search form which sends a 'SELECT * FROM table WHERE articleText LIKE' query to retrieve articles from the database.

    Here's the problem - when the searchfield is empty the page is returned with all of the articles in the database (not surprisingly) so i've attempted to intercept that with a javascript form validation but it doesn't seem to be working.

    Here's what i have in the header of the page:
    Code:
    <script language="javascript">
      function IsEmpty(aTextField) {
        if ((aTextField.value.length==0) || (aTextField.value==null)) {
         return true;
        }
      else { return false; }
    }
    
    function ValidateForm(form)
      { 
        if(IsEmpty(form.searchtext)) 
          { 
            alert('You have not entered any text to search');
            form.searchtext.focus(); 
            return false; 
          } 
        return true;
    }
    </script>
    and this is the search form that i've put on the page:
    Code:
    <form name="searchForm" action="search.php" method="get" onsubmit="javascript:return ValidateForm(this)">
      <span class="navhead">Search panel</span> 
      <input name="searchtext" type="text" size="11" value="">
      <input type="submit" value="search">
    </form>
    But it seems that this gets completely dismissed and the search.php page comes up with all of the articles as if the script wasn't there at all.

    I've tried several variations on the theme in javascript and used a couple of different browsers (and made sure javascript was turned on) but nothing seems to change.

    Please help
    ;oB

  2. #2
    SitePoint Enthusiast overbyte's Avatar
    Join Date
    Jun 2003
    Location
    yes, he said exactly london, england
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PS i have also tried setting the onSubmit statement to false and even that doesn't stop the form being submitted.

    rilly confoozed
    ;oB

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    This is your problem

    aTextField.value==null

    null is not the same as a zero length (empty) string.

    I think you will find that the only condition you really need to test for is:

    if (aTextField.value == '')

  4. #4
    SitePoint Enthusiast overbyte's Avatar
    Join Date
    Jun 2003
    Location
    yes, he said exactly london, england
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks - i tried it but no cigar.

    i don't think that it's the script as i'm not getting an error - it seems more like the script isn't firing at all.
    ;oB

  5. #5
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to remove the javascript: from the onsubmit value. You only need javascript: if you're trying to run js code within a link's href.
    ck :: bringing chris to the masses.

  6. #6
    SitePoint Enthusiast overbyte's Avatar
    Join Date
    Jun 2003
    Location
    yes, he said exactly london, england
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that worked on another example i got from webmonkey (below) but not on the one i gave to you originally

    Code:
    <head>
    <script type="text/javascript" language="JavaScript">
    function validateString(field, msg, min, max) { 
            if (!min) { min = 1 } 
            if (!max) { max = 65535 } 
    
            if (!field.value || field.value.length < min || field.value.max > max) { 
                    alert(msg); 
                    field.focus(); 
                    field.select(); 
                    return false; 
            }
    }
    </script>
    </head>
    
    <body>
    <form name="searchForm" action="../search.php" method="get" onSubmit="return validateString(this.searchtext,
    'Please enter text to search for', 1, 15);"> 
            <span class="navhead">Search panel</span> 
            <input name="searchtext" type="text" size="11" value="">
            <input type="submit" value="search">
    </form>

    i think i might go and lie down somewhere dark ...
    ;oB


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
  •