...

View Full Version : javascript validation max number



esthera
06-18-2007, 07:32 AM
i'm trying to create a function where I can pass in a form the maxnumber of items

so my form like this

item 1 <input type=text name=item1>

item 2 <input type=text name=item2>

I want to set that the maximum number for item1 is 10 (so can be any number 1-10) and for item 2 the maximum is 5

show would I go about doing this?

Philip M
06-18-2007, 07:40 AM
function checkMax() {
temp1 = parseInt(document.formname.item1.value);
temp2 = parseInt(document.formname.item2.value);

if (temp1 <1 || temp1>10) {
alert ("Error - only values from 1 - 10 are allowed!");
document.formname.item1.value = "";
document.formname.item1.focus();
return false;
}

if (temp2 <1 || temp2>5) {
alert ("Error - only values from 1 - 5 are allowed!");
document.formname.item2.value = "";
document.formname.item2.focus();
return false;
}
}

esthera
06-18-2007, 07:46 AM
thanks - there is a whole list of items - -and dynamically created in asp
is there a way I can set the max items in the form field somehow?

esthera
06-18-2007, 08:56 AM
i figured it out

function checknum(valfield,num){
if (valfield.value >num) {
alert ("Error - There are only " + num + "items in stock");
valfield.value = num;
valfield.focus();
return false;
}

}

Philip M
06-18-2007, 10:18 AM
So long as you are sure that valfield and num really are numbers, not strings.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum