Check for minimum quantity?

N

Nicolae Fieraru

Hi All

I have a form, with a few input boxes (number of input boxes can vary). Each
input box has a name such as in1, in3, in7, etc (their names can be in any
order, although increasing).
I want to make a javascript function to check if the quantity a user fills
into each of the input boxes is over a minimum value (different for every
input box). Any ideas how can I do this?

Regards,
Nicoale
 
L

Lee

Nicolae Fieraru said:
Hi All

I have a form, with a few input boxes (number of input boxes can vary). Each
input box has a name such as in1, in3, in7, etc (their names can be in any
order, although increasing).

That's a direct contradiction. I think you mean that the order
is always increasing, but that they may not be sequential.
I want to make a javascript function to check if the quantity a user fills
into each of the input boxes is over a minimum value (different for every
input box). Any ideas how can I do this?

Since you don't know how many there are, I assume that the page
is being generate by software. Simply have that same software
generate a literal data object to control the audit (the variable
"minVal" in this example). If possible, you might also have the
code produce meaningful variable names.


<html>
<head>
<title>Demo</title>
<script type="text/javascript">
var minVal = [
{ name: "in1", min: 18, desc: "Your age" },
{ name: "in3", min: 6, desc: "Shoe size" },
{ name: "in7", min: 8, desc: "Hours requested" }
];

function checkMins(f) {
var msg="";
for (var i=0;i<minVal.length;i++) {
if(+f.elements[minVal.name].value < minVal.min) {
msg+="\n"+minVal.desc+" must be at least " +minVal.min;
}
}
if(msg) {
alert(msg);
return false;
}
return true;
}
</script>
</head>
<body>
<form name="foo" onsubmit="return checkMins(this)">
<input name="in1"><input name="in3"><input name="in7"><input type="submit">
</form>
</body>
</html>
 
N

Nicolae Fieraru

I have a form, with a few input boxes (number of input boxes can vary).
That's a direct contradiction. I think you mean that the order
is always increasing, but that they may not be sequential.

Thank you Lee, I know it was a contradiction, but I couldn't remember the
term "sequential" :)

The piece of software you kindly provided works perfectly and I will use
your idea in my asp page. Thank you very much for your help.

Best wishes,
Nicolae
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top