SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Indiana
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with a function to check the range of values in multiple text inputs.

    Hi. Could someone please look at a form page Iam working on. I am trying to call a function named "rangeCheck()" to make shure that all the values entered in all of the 8 textfields are between 1-5. If this is true I need it to call the function calculate(). Could someone point me in the right direction? Here is the link to my page.http://www.inahq.org/memberladder.html

    Thanks!!!

  2. #2
    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)
    I'd do it this way
    Code:
    var numCats = 8;
    function checkData(o) {
    	if (/^[1-5]$/.test(o.value)) {
    		return true;
    		doTotal(o.form);
    		}
    	else {
    		alert("Must be a number between 1 and 5!");
    		o.select();
    		o.focus();
    		return false;
    		}	
    	}
    	
    function doTotal(f) {
    	var total = 0;
    	for (var i=1, i<=numCats ; i++) {
    		var o = f.elements['cat'+i];
    		total += (/^\s*$/.test(o.value)) ? 0 : parseInt(o.value);
    		}
    	f.totalpoints.value = total;
    	}
    
    <input type="text" name="cat1" id="cat1" onBlur="return checkData(this)" />
    <!-- Etc etc -->
    <input type="text" name="cat1" id="cat8" onBlur="return checkData(this)" />
    <input type="text" name="totalpoints" id="totalpoints" />
    Last edited by beetle; Sep 10, 2002 at 20:20.
    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




  3. #3
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Indiana
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help, but I am fairly new to Javascript and I don't want to just use someone eleses script without being able to understand how they did it. I was sort of hoping you could show me ways to make my script work and if it won't tell me why. Thanks again.

  4. #4
    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)
    well, I think you would need to use parseInt() and the 'elements' collection in a couple places

    Line 12: range = parseInt(document.memberladder.elements[ 'cat' + n ].value);

    Line 24: total += parseInt(document.memberladder.elements[ 'cat' + i ].value - 0);
    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




  5. #5
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Indiana
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok after further reading I can see some of my errors. I have changed the lines 12 & 24 to includes the 'elements' collection and got rid of the 'cat' so the lines look like this...

    line 12: range = parsInt(document.memberladder.elements[ n ].value);

    Line 24: total += parsInt(document.memberladder.elements[ i ].value - 0);

    Shouldn't this work? (It doesn't) It gives me a script error stating "Line 16 object expected". Take a look at the updated code

    http://www.inahq.org/memberladder.html
    Last edited by 4ever; Sep 11, 2002 at 10:05.


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
  •