Current location: Hot Scripts Forums » General Web Coding » JavaScript » Add top 5 form fields and write them


Add top 5 form fields and write them

Reply
  #1 (permalink)  
Old 09-01-06, 01:50 AM
e-XPLoDeR's Avatar
e-XPLoDeR e-XPLoDeR is offline
Newbie Coder
 
Join Date: Sep 2006
Location: Ankara, Turkey
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Question Add top 5 form fields and write them

Hi.

I want to add (opposite of subtract) some values in the form. The form is located at www.e-xploder.net/harici/form

In this form, there is no problem when adding (when you enter some values in form fields, script adds them and writes the total in "I Alt" area). But, I want to add top 5 values. So, for example, when I enter 5,2,6,8,10,20,30 in fields, in "I alt" area it must be 74 (30+20+10+8+4).

Is there a script like this? Please help. I don't have any experience on JS and can't understand codes.

Yours sincerely.
(Sorry for my English)
Reply With Quote
  #2 (permalink)  
Old 09-01-06, 07:17 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
function calc(){
        var theValues=new Array(parseInt(document.autoSumForm.s1c.value), // Add all values to an array
                parseInt(document.autoSumForm.s1p.value),
                parseInt(document.autoSumForm.s2c.value),
                parseInt(document.autoSumForm.s2p.value),
                parseInt(document.autoSumForm.s3c.value),
                parseInt(document.autoSumForm.s3p.value),
                parseInt(document.autoSumForm.s4c.value),
                parseInt(document.autoSumForm.s4p.value),
                parseInt(document.autoSumForm.s5p.value),
                parseInt(document.autoSumForm.s5c.value),
                parseInt(document.autoSumForm.s6p.value),
                parseInt(document.autoSumForm.s6c.value),
                parseInt()document.autoSumForm.finale.value)
        theValues.sort(mySort)
        document.autoSumForm.ialt.value = (theValues[0]+theValues[1]+theValues[2]+theValues[3]+theValues[4]);
}

function mySort(a,b){
        return a-b
}
I think that should work, but I'm not sure if the quick way to do numerical sorting was to do a-b or b-a in mySort. But I guess you'll notice since if it's the wrong way, you'll get the five smallest values.
Also note that non-numerical values will mess up the script.
If you want to only allow numbers in a field, try this:
Code:
function Cleanup(field){
	input=field.value.substring(0,20) // Set absolute max length to 20
	while(/\D/.test(input)){
		input=input.replace(/\D/,'')
	}
	while(input.substring(0,1)=="0"){
		input=input.substring(1)
	}
	if(input==''){
		input=0
	}
	field.value=input
}

...

<input type="text" onchange="cleanup(this)">
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #3 (permalink)  
Old 09-02-06, 02:51 AM
e-XPLoDeR's Avatar
e-XPLoDeR e-XPLoDeR is offline
Newbie Coder
 
Join Date: Sep 2006
Location: Ankara, Turkey
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks TwoD.

Your codes are great. I've tried this and it works.


Code:
function calc(){
	var inputnames = "s1c s1p s2c s2p s3c s3p s4c s4p s5p s5c s6p s6c s7c s7p s8c s8p s9c s9p s10c s10p s11c s11p s12c s12p finale".split(" ")
	var values = []
	for (var i = 0; i < inputnames.length; ++i)
		values[i] = +document.autoSumForm[inputnames[i]].value
	values.sort(function(a,b) { return b - a })
	values.length = 5
	document.autoSumForm.ialt.value = eval(values.join("+"))
}
Reply With Quote
  #4 (permalink)  
Old 09-03-06, 09:25 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Umm why first put the names in a string, and then split them?
You might as well do
Code:
var inputnames = ["s1c","s1p","s2c","s2p","s3c","s3p","s4c","s4p","s5p","s5c","s6p","s6c","s7c","s7p","s8c","s8p","s9c","s9p","s10c","s10p","s11c","s11p","s12c","s12p","finale"]
I do not recommend using eval(). (search Google for "eval is evil" and you'll find out why)
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #5 (permalink)  
Old 09-03-06, 09:27 AM
e-XPLoDeR's Avatar
e-XPLoDeR e-XPLoDeR is offline
Newbie Coder
 
Join Date: Sep 2006
Location: Ankara, Turkey
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
So, I will do yours. Thanks again
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Programmer - Form Metic Job Offers & Assistance 13 06-29-06 07:03 AM
verify form fields. pkcidstudio PHP 27 05-19-06 11:21 AM
Form Validation HELP dwoody JavaScript 2 08-11-04 12:12 PM
LWP posting variable numbers of fields? afenn Perl 1 05-14-04 11:02 PM
Write form data to file dragge PHP 1 12-27-03 07:26 PM


All times are GMT -5. The time now is 11:15 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.