SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist jkh1978's Avatar
    Join Date
    May 2001
    Location
    Northern Virginia
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    numbers in js on a form

    I want to do client side checking on a form.

    There are lots of boxes where people can input numbers. These numbers must be equal or less than 100 before a user hits submit. How is this programmed?

    Also, can I have the total displayed before the submit?

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    form tag onsubmit should point to a js function.

    the function should loop through the form.length and check
    each object's value < 100 and add it to a running total.

    if it encounters one more than 100 or not there - it should then return false ( the onsubmit should return the function )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  3. #3
    SitePoint Evangelist jkh1978's Avatar
    Join Date
    May 2001
    Location
    Northern Virginia
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help. do you have any sample code?

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just do a:

    Code:
    f=document.getElementById('formId');
    
    for (var i=0;i<f.length;i++){
            if (f[i].type == 'text' && f[i].className == 'wanted'){
                     if (f[i].value > 100 || f[i].value.match(/[^\(\d|\.)]/){
                              alert(f[i].name+' is not valid: '+f[i].value);
                              return false;
                              }
                     }
            }
    Stick that in a function i guess.
    Tell the form to onsubmit='return funcName();'

    I'm not sure that the function needs to be told to return
    true - it should just assume it - hence no reference to it
    in the code.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    SitePoint Evangelist jkh1978's Avatar
    Join Date
    May 2001
    Location
    Northern Virginia
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad... I reread my post and you answered what I asked, but that wasnt what I wanted lol. I want the sum of all the numbers to be <= 100.

    Thanks for your help.

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol then add them up as you go ...

    Code:
    f=document.getElementById('formId');
    total=0'
    for (var i=0;i<f.length;i++){
            if (f[i].type == 'text' && f[i].className == 'wanted'){
                     total += f[i].value;            
                     }
            }
    
    if (total > 100) return false;
    else return true;
    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •