Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate value in one field is greater than the previous field 2

Status
Not open for further replies.

Katie6

Programmer
Jun 12, 2007
58
GB
Hi there,

I'm making a web form to record the number of people who answer a particular question correctly. E.g.

Code:
Question: How many people answered question 1 correctly?
Answer: [  ] of [  ]

The [ ] represents a field to be filled in by the user. The left field is the number who answered correctly and the right field is the total number of people questioned. Therefore the number in the right box must be greater than or equal to the number in the left.

This form exists already and we get people putting a higher number in the right box, making the results of the survey meaningless, because it is impossible for 5 out of 4 people to answer a question correcty for example.

I don't know much javascript, so I was wondering if anyone could help write something that alerts the user if they enter a higher number in the right box than the left?

If it will work with the following example which has three questions, that would be great! (This would help me see how to deal with multiple questions).

Code:
Question: How many people answered question 1 correctly?
Answer: [  ] of [  ]

Question: How many people answered question 2 correctly?
Answer: [  ] of [  ]

Question: How many people answered question 2 correctly?
Answer: [  ] of [  ]

Many thanks,

Katie
 
Hi

This solution permits any number of answers as long as the correct [tt]input[/tt]'s names are 'c*' and the total [tt]input[/tt]'s names are 't*' ( * meaning the order number starting from 1 ).
JavaScript:
function chk(what)
{
  var err=''
  var nr=1
  while (what.elements['c'+nr] && what.elements['t'+nr]) {
    if (parseInt(what.elements['c'+nr].value)>parseInt(what.elements['t'+nr].value))
      err+='\n - '+nr
    nr++
  }
  if (err) alert('Wrong values at answer :'+err)
  return err==''
}
HTML:
<form action="#" onsubmit="return chk(this)">
<p>
Question: How many people answered question 1 correctly?<br>
Answer: <input type="text" name="c1"> of <input type="text" name="t1">
</p>
<p>
Question: How many people answered question 2 correctly?<br>
Answer: <input type="text" name="c2"> of <input type="text" name="t2">
</p>
<p>
Question: How many people answered question 3 correctly?<br>
Answer: <input type="text" name="c3"> of <input type="text" name="t3">
</p>
<p>
<input type="submit">
</p>

Feherke.
 
Hi Katie, I'm basing this example on the assumption you are using text boxes:

Code:
function CheckValue(obj){
var value1 = document.getElementById('Input'+obj+'a');
var value2 = document.getElementById('Input'+obj+'b'));

	if(value1.value !='' && value2.value !=''){
		if(value1.value > value2.value){
			alert('value 1 must be less than or equal to value 2');
		}
	}
}

Thats your javascript and then all you need to do is ensure you give your text boxes an id and call that function when it's updated.

Code:
Answer: <input type="text" id="Input1a" onchange="CheckValue(1);"> of <input type="text" id="Input1b" onchange="CheckValue(1);">

Hope this helps

Cheers

Nick
 
Thanks Feherke, thanks Nick!
I'm liking this code!
I haven't decided which to use yet, but I'll give you both a star anyway!

Thanks!

Katie
 
Hi Nick,

I've been testing the two options above. I've managed to get Feherke's code to work, but not yours. I know it must be my fault, but I was wondering if you could check this code? I have included all the code in my html test page:

Code:
<head>
<script type="text/javascript" language="JavaScript">

function CheckValue(obj)
{
var value1 = document.getElementById('q'+obj+'a');
var value2 = document.getElementById('q'+obj+'b'));

    if(value1.value !='' && value2.value !=''){
        if(value1.value > value2.value){
            alert('value 1 must be less than or equal to value 2');
        }
    }
}
</script>
</head>
<body>
Answer: 
<input type="text" id="q1a" name="q1a" onchange="CheckValue(1);"> of <input type="text" name="q2a" id="q1b" onchange="CheckValue(1);"><br />
<input type="text" id="q2a" name="q1a" onchange="CheckValue(2);"> of <input type="text" name="q1a" id="q2b" onchange="CheckValue(2);">
</body>

I like the idea of the warning appearing onchange so that the user knows straight away that they've made an error, but I get the following warning:

Code:
Line: 20
Char: 1
Error: Object expected
code: 0

Any help would be much appreciated.

Thanks,

Katie
 
No reply required! I have figured out the answer. There was an extra bracket. The variables should have been:

Code:
var value1 = document.getElementById('Input'+obj+'a');
var value2 = document.getElementById('Input'+obj+'b');

instead of
Code:
var value1 = document.getElementById('Input'+obj+'a');
var value2 = document.getElementById('Input'+obj+'b')[b])[/b];

Nick's code now works perfectly :)
 
oops, sorry about that, my fault! I agree with feherke though, you should match the names with the ID's.

Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top