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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting Numeric Value in Input

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
In a form, I want to limit the numeric value that can be entered. For example, on a given entry, 1 or 2 can be entered but not 3. I already added a function to make it numeric-only but I have nothing at all for limiting the value, which will from be a PHP variable. Any ideas?

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Sorry - I should have said that I assumed you already knew enough about the event data and cancelling to have made the input numeric-only to start with. If that's not the case, could you post your existing JS & form code?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks. Sounds easy but I'm not a JavaScript programmer at all so I have no clue how to do it! Actually the validation is in an external file and I need to call the function by passing the maximum number to it something like this:

Code:
$output[] = '<input type="text" name="qty'.$ItemID.'" value="'.$qty.'" size="1" maxlength="2" onkeypress="return numbersonly(event);maxstock('.$StockQuantity.')" />';

where the PHP variable $StockQuantity contains the number. The value in the form can be this number or anything less including 0.

As for the JavaScript, all I have was gleaned from some other function and does not work as it is:

Code:
function maxstock(val){
   var max=val;
   if (form.value > max) {
      return false;
   }
}

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Hi, I once made this script I called trapkey.
I used it meerly for submitting a page, from an input which was not inside a form.

You can add more keycodes in this function.


Here is a list of keycodes:
I have not tested that list to check if it's 100% correct, I simply googled it now.

Anyhow, it seems you will want to add the keycodes 2, 3, 11 and maybe you want to have the enter keycode trapped there too? Anyhow, I believe you can use the function I made for the commerce starter kit.

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks, I'll check it out more closely tomorrow. Looking at it briefly, it's not clear what I would do with keycodes since there is no specific code that I need. I used the values of 1, 2 and 3 just as an example but really it could be any number at all as the maximum for a given entry. There are multiple forms in the list and each would probably have a different value than the neighboring forms. It's already validated for numbers only using keycodes.

In the meantime, in my code above, am I using the correct method of passing a value to a JavaScript function?

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Don,

Can you confirm your exact requirements - as I'm unsure at to what it is you want.

Your initial post led me to believe you didn't want the user to be able to enter into the box any number greater than (for example) 3. However, now I believe you might only want to stop form submission, rather than stop the number being put into the box at all.

Can you confirm which of these two is the case? The former is harder / more complex, and the latter is a doddle.

Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If you only wish to limit input to one type of input, let's say integer, you can simply try an parseint?

If you wish several type of checks, I guess you could make that function accept an second parametre which you could then switch inside the function, to select a pattern.

Why not also accept regexp as a parametre?
You have many ways to go.

One other way to go, would be to have one input and then have maybe another input which gets transferred all the values which are "ok". This second input could be hidden, it could be a readonly input or whatever.

For simplicitys sake, I would rather check type of input.
However, I too, like Dan thought you wanted to limit the input to certain values.

If you wish to have multiple functions within a function, just switch a parametre, like I said earlier in this post :)

Olav Alexander Mjelde
Admin & Webmaster
 
and the latter is a doddle

Not to mention, the more secure of the 2 options. Onkeypress is not going to be your salvation to prevent letters and numbers above 3 from being put into the textbox. For example, a user can copy "abc123" to their clipboard, right click on the text field, and then paste the value into the text field - completely bypassing the onkeypress event.

I find that doing all checks at time of submission using the onsubmit handler is usually the most cohesive check (besides checking server-side which you should be doing anyway)

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
For example, a user can copy "abc123" to their clipboard, right click on the text field, and then paste the value into the text field - completely bypassing the onkeypress event.

Yes, one can never use JS for security-check of form inputs.
Users can disable JS and some dont even have support for it.

in php could check the variable

Olav Alexander Mjelde
Admin & Webmaster
 
There is no security issue here as this form has no personal information and is not secure anyway.

I sell rare and out of print items where I have only a few of each. I need to prevent someone from trying to order more items than I have by doing a database lookup (in PHP) of the quantity on-hand, then making the quantity field on the shopping cart not allow a higher number than the stock quantity.

The function I am working on now needs only to limit how high a number can be entered based on a value passed into the function from PHP.

The field already has a function to stop any non-numeric characters from being entered. You can see the call to it in my form sample form code that I posted above. There are two onkeypress calls: one to the numbersonly() function which is working perfectly, and one to the maxstock() function which is not working.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Code:
<?php
if ($row['numberOfItems'] > 0) {
?>
<select name="numOfItems">
<?php
  for ($i = 0; $i < $row['numberOfItems']; $i++) {
      echo "<option>{$i}</option>";
    }
?>
</select>
<?php
  }
?>
ps. psuedocode.. I havent tested this!

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks and I thought of that before posting originally but no, I prefer to do it in JavaScript and it seems it would be a simple thing. If a visitor has JavaScript disabled, I might make it use PHP as a standby but I don't want it to be the default.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
You can still do the integer-check in php though..
Pass the value through a php function and return 0 if the value (regex) is not numerical.

eg. if the user enters anything but the integers you specify to the regexp function, return 0 if the value is not an integer and if you have let's say 10 items and he orders 20, you could set it to 10 and notify the user that you only have 10 in stock, so his order-abount has been adjusted to compensate for what you have in stock.

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks again but again, it needs to be done in JavaScript. I need it to be instantanteous rather than having to submit the form to discover the error. What I want is for the user to be unable to enter any number higher than the quantity on-hand. Most of the people who use this site are not computer-saavy and many do not even speak English so I do not want to have to deal with the countless languages and locale settings necessary for any error messages to be understandable.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Don said:
I prefer to do it in JavaScript and it seems it would be a simple thing. If a visitor has JavaScript disabled, I might make it use PHP as a standby but I don't want it to be the default.

That's entirely the wrong way of thinking. You should use PHP as a default, and then if the user has JS enabled, you can do it client-side instead (providing instant feedback instead of having to submit the form).

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ok, then you do like this:
function trapValue(value, maxValue){
if (parseInt(string)) {
if (value <= maxValue) {
return value;
}
else {
return maxValue;
// maybe also notify the user that the amount has been adjusted?
}
}
else {
return '0';
}
}
</script>

You would have to call the function from the input.
How you know the maxvalue, I dont know.

You say you get it from the db, so maybe you can make some hidden fields for the amount.

I think this is very non-elegant and I would rather use AJAX + PHP for this purpose.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top