SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru moonman's Avatar
    Join Date
    Dec 2000
    Location
    The Sea of Tranquility
    Posts
    696
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple validation

    Please excuse my ignorance, but I just can't get Javascript at all.

    I'm trying to write some validation for a field called Propid, in a form called qSearch. Basically if there's nothing in Propid, or it is says "ref no.", I want to alert the user, otherwise, I want the form to submit.

    Here's what I have at the moment:
    Code:
     
    
    <script>
    function checksearch()
    { 
    
    if (qSearch.Propid.value!="ref no." )
    { 
    alert ("Select reference number" );
    qsearch.Propid.focus();
    return;
    }
    if (qSearch.Propid.length != 0)
    {
    alert ("Select reference number" );
    qSearch.Propid.focus();
    return false;
    }
    }
    }
    Last edited by moonman; Jun 13, 2003 at 07:49.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    qSearch.Propid isn't the best way to get a reference to a form element
    I prefer document.getElementById(elem_id)

    And; shouldn't ... !="ref no."... be ... =="ref no."... ?
    But what happens if the text is "ref no" or "ref n" etc?
    Conclusion; you need another method to check that the textbox contains a valid Propid, e.g. check if the text ends with a numeric value

  3. #3
    SitePoint Guru moonman's Avatar
    Join Date
    Dec 2000
    Location
    The Sea of Tranquility
    Posts
    696
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "ref no." gets removed on focus, so I'm not bothered about "ref n" or anything like that. I just need it to alert when "ref no." or nothing is entered.

  4. #4
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    People can still enter a text like "blah" and then click Submit

  5. #5
    SitePoint Guru moonman's Avatar
    Join Date
    Dec 2000
    Location
    The Sea of Tranquility
    Posts
    696
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it'll just bring up no results on the results page.

    I don't care about that

  6. #6
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't like alerts, you can do like this:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html>
      <head>
        <title>Validation example</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
        <style type="text/css">
          .err
          {
            color: #ff0000;
            visibility: hidden;
          }
        </style>
        <script type="text/javascript">
          function checkValue(elem)
          {
            var value = new Number(elem.value);
            var span = document.getElementById(elem.name + "err");
            if(value <= 0 || isNaN(value))
            {
              span.style.visibility = "visible";
            }
            else
            {
              span.style.visibility = "hidden";
            }
          }
        </script>
      </head>
      <body>
        <form action="">
          <p>
            <input type="text" name="txt1" onblur="checkValue(this);"> 
            <span id="txt1err" class="err">Value must be numeric, and larger than 0!</span>
          </p>
          <p>
            <input type="submit">
          </p>
        </form>
      </body>
    </html>

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My newest version of fValidate supports 13 pre-defined error modes

    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •