Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,458
    Thanks
    15
    Thanked 0 Times in 0 Posts

    javascript validation max number

    i'm trying to create a function where I can pass in a form the maxnumber of items

    so my form like this

    item 1 <input type=text name=item1>

    item 2 <input type=text name=item2>

    I want to set that the maximum number for item1 is 10 (so can be any number 1-10) and for item 2 the maximum is 5

    show would I go about doing this?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    function checkMax() {
    temp1 = parseInt(document.formname.item1.value);
    temp2 = parseInt(document.formname.item2.value);

    if (temp1 <1 || temp1>10) {
    alert ("Error - only values from 1 - 10 are allowed!");
    document.formname.item1.value = "";
    document.formname.item1.focus();
    return false;
    }

    if (temp2 <1 || temp2>5) {
    alert ("Error - only values from 1 - 5 are allowed!");
    document.formname.item2.value = "";
    document.formname.item2.focus();
    return false;
    }
    }

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,458
    Thanks
    15
    Thanked 0 Times in 0 Posts
    thanks - there is a whole list of items - -and dynamically created in asp
    is there a way I can set the max items in the form field somehow?

  • #4
    Senior Coder
    Join Date
    May 2004
    Posts
    1,458
    Thanks
    15
    Thanked 0 Times in 0 Posts
    i figured it out

    function checknum(valfield,num){
    if (valfield.value >num) {
    alert ("Error - There are only " + num + "items in stock");
    valfield.value = num;
    valfield.focus();
    return false;
    }

    }

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    So long as you are sure that valfield and num really are numbers, not strings.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •