validating quantity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terje

    validating quantity

    Hello I have a function like this

    function checkquantitiy( quantitiy){

    if(quantitiy.va lue != parseInt(quanti tiy.value)) {
    alert(quantitiy .value+" sorry not integer quantitiy");
    }

    } //End of function

    This check if quantitiy is integer. In the form I have following

    <input name="buyquanti tiy" type="hidden" id="buyquantiti y" value="<?php echo
    $buyquantitiy; ?>" size="1" maxlength="3">
    <input name="quantitiy " type="text" id="quantitiy" value="1" size="1"
    maxlength="3" onchange="javas cript:return checkquantitiy( this);">

    The problem is when i will calculate if quantitiy/buyquantitiy is an
    integer. The reason is that some products can only be seld as 4 items, and
    if you press 3 wou should get a message that says
    "Sorry a quantity of 3 are not aviable because there are 4 items in
    package". Can transfer 2 varibales into the function? Or have anyone any
    suggestion how to solve this.

    Terje


  • McKirahan

    #2
    Re: validating quantity

    "Terje" <terje@cyberfac tory.no> wrote in message
    news:nmwJd.8520 7$Vf.3768343@ne ws000.worldonli ne.dk...[color=blue]
    > Hello I have a function like this
    >
    > function checkquantitiy( quantitiy){
    >
    > if(quantitiy.va lue != parseInt(quanti tiy.value)) {
    > alert(quantitiy .value+" sorry not integer quantitiy");
    > }
    >
    > } //End of function
    >
    > This check if quantitiy is integer. In the form I have following
    >
    > <input name="buyquanti tiy" type="hidden" id="buyquantiti y" value="<?php[/color]
    echo[color=blue]
    > $buyquantitiy; ?>" size="1" maxlength="3">
    > <input name="quantitiy " type="text" id="quantitiy" value="1" size="1"
    > maxlength="3" onchange="javas cript:return checkquantitiy( this);">
    >
    > The problem is when i will calculate if quantitiy/buyquantitiy is an
    > integer. The reason is that some products can only be seld as 4 items, and
    > if you press 3 wou should get a message that says
    > "Sorry a quantity of 3 are not aviable because there are 4 items in
    > package". Can transfer 2 varibales into the function? Or have anyone any
    > suggestion how to solve this.
    >
    > Terje[/color]

    Is "quantitiy" in Norwegian for the English "quantity"?

    Will this help?

    A second parameter is passed which, if non-zero, will be used to validate
    the minimum "quantitiy" .

    <html>
    <head>
    <title>quantity .htm</title>
    <script type="text/javascript">
    function checkQty(qty,mi n){
    if (qty.value != parseInt(qty.va lue,10)) {
    alert("Quantiti y not numeric");
    } else if (min > 0 && qty.value < min) {
    alert("Quantiti y below minimum of " + min);
    }
    }
    </script>
    </head>
    <body>
    <form action="" method="post" name="form1">
    <input type="text" name="quantitiy " id="quantitiy"
    value="1" size="1" maxlength="3"
    onchange="check Qty(this,4);">
    </form>
    </body>
    </html>


    If "1" is a desired value then it will not be validated as the "onchange() "
    event does not fire.


    Comment

    • Fred Oz

      #3
      Re: validating quantity

      McKirahan wrote:
      [...][color=blue]
      > if (qty.value != parseInt(qty.va lue,10)) {
      > alert("Quantiti y not numeric");[/color]

      0.5 will fail the test, but it is certainly a numeric value. The
      original error message was better:

      alert(quantitiy .value+" sorry not integer quantitiy");

      But why not:

      alert("Quantity must be a whole number of "
      + min + " or more.");

      Then the same error message can be used for both cases.

      --
      Fred

      Comment

      • Dr John Stockton

        #4
        Re: validating quantity

        JRS: In article <nmwJd.85207$Vf .3768343@news00 0.worldonline.d k>, dated
        Tue, 25 Jan 2005 19:38:23, seen in news:comp.lang. javascript, Terje
        <terje@cyberfac tory.no> posted :[color=blue]
        >Hello I have a function like this
        >
        >function checkquantitiy( quantitiy){
        >
        > if(quantitiy.va lue != parseInt(quanti tiy.value)) {
        > alert(quantitiy .value+" sorry not integer quantitiy");
        > }
        >
        >} //End of function
        >
        >This check if quantitiy is integer. In the form I have following
        >
        ><input name="buyquanti tiy" type="hidden" id="buyquantiti y" value="<?php echo
        >$buyquantiti y; ?>" size="1" maxlength="3">
        > <input name="quantitiy " type="text" id="quantitiy" value="1" size="1"
        >maxlength="3 " onchange="javas cript:return checkquantitiy( this);">
        >
        >The problem is when i will calculate if quantitiy/buyquantitiy is an
        >integer. The reason is that some products can only be seld as 4 items, and
        >if you press 3 wou should get a message that says
        >"Sorry a quantity of 3 are not aviable because there are 4 items in
        >package". Can transfer 2 varibales into the function? Or have anyone any
        >suggestion how to solve this.[/color]


        See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

        The best way to validate such starts with a RegExp;

        input = quantity.value
        OK = /^\d+$/.test(input) // all digits
        OK = /^[1-9]\d*$/.test(input) // all digits, not starting 0

        If OK, input = +input will safely convert to string.

        With a RegExp /^\d{1,3}$/ you can allow at most 3 digits, if that's
        useful.

        OK = /^[1-9]\d*$/.test(input) && input%4==0 // & divisible by 4

        But, to transfer two parameters into a function
        function Add(X, Y) { return X+Y } // for numbers
        function Add(X, Y) { return +X + +Y } // for numbers/strings

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...