SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form field validation question

    Hello. I am making an order form in which some of the fields need to be 8 or above. I want it so that whenever the user clicks off of the field (after filling in the quantity of the item they want), it checks to see if the value is anywhere from 1 to 8 and if it is, to tell the user that it needs to be 8 or above, set the field's value to 8, and focus on it. The method I tried didn't work. The trick is I want the code to be re-useable so I can have like 16 to 100 items validated in the same way without making custom functions for each one. Dig?

    Anyway please take a look at what I've got and make any suggestions if you can. The alert works fine, but we get the error when we try focusing or resetting the value of the field in question.




    <script Language="JavaScript">
    <!--
    function Field_Validator(mrFieldName, mrFieldValue){

    if (mrFieldValue > 0 & mrFieldValue < 8){
    alert("Please note that this item has a minimum quantity of 8.");
    theForm.mrFieldName.value = 8;
    theForm.mrFieldName.setfocus();

    //return (false);
    }

    //return (true);
    }

    -->
    </script>


    (in the form


    Pasta A: <INPUT type="text" name="pastaA" onBlur='Field_Validator("pastaA", this.value)'>

    Pasta B: <INPUT type="text" name="pastaB" onBlur='Field_Validator("pastaB", this.value)'>


    (NOTE: I tried using onBlur='Field_Validator(this)' and then calling on the value in the validator function using this.value, this.focus(), etc, but it didn't seem to work for whatever reason.)

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing your input statement to this:
    <INPUT type="text" name="pastaA" onChange='Field_Validator(this)'>

    And your function to this:
    function Field_Validator(fldobj){

    if (fldobj.value > 0 && fldobj.value < 8){
    alert("Please note that this item has a minimum quantity of 8.");
    fldobj.value = "8";
    fldobj.focus();

    //return (false);
    }

    //return (true);
    }

    I have not tested this so I can't guarantee that it will work.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This has been tested in Nescape4+ and Explorer 4+ (and it works).

    Code:
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    
    function checkValue(e){
      var minValue = 8;
      var v = parseInt(e.value) || 0;
      if(v < minValue){
        alert("Please note that this item has a minimum quantity of "+minValue);
        e.value= minValue;
        e.focus();
        e.select();
      }
    }
    
    </script>
    </head>
    <body>
    
    <form>
      <p><input type="text" name="field1" value="" onblur="checkValue(this);" /></p>
      <p><input type="text" name="field2" value="" onblur="checkValue(this);" /></p>
      <p><input type="text" name="field3" value="" onblur="checkValue(this);" /></p>
      <p><input type="text" name="field4" value="" onblur="checkValue(this);" /></p>
      <p><input type="text" name="field5" value="" onblur="checkValue(this);" /></p>
    </form>
    
    </body>
    </html>
    Last edited by podlob; Jan 14, 2003 at 23:51.
    travis

  4. #4
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Totally thank you

    Thanks guys. You guys are the best.


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
  •