Forum Moderators: coopster

Message Too Old, No Replies

let user insert only numeric data

Can you make a textfield just like an user can only input 1-9

         

Twisted Mind

12:55 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



Hey i was thinking would it be possible to make a textfield only possible to insert numeric data
so when you would press an other button nothing will happen...
if you know let me know :P

grtz

Twisted_Mind

Nutter

1:46 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



It would probably have to be javascript. Somewhere I've got the JS code that'll allow input based on either a whitelist or deny based on a blacklist. If I can find it, I'll post it here later.

Nutter

1:51 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



Here's the JS routine - found it a little quicker than I thought I could :-). It goes in the onkyepress event for the text box.

strList is the whitelist / blacklist

bAllow = true is for whitelist, false is for blacklist

function limitinput(evt, strList, bAllow)
/*Limits the input to strList. If bAllow is true, then
only allow what is in strList. If bAllow is false,
then do not allow what is in strList.*/
{
var charCode = evt.keyCode;
if (charCode==0)
{
charCode = evt.which;
}
var strChar = String.fromCharCode(charCode);
/*controlArray holds the ASCII codes for valid
control commands (BS, CR, LF, etc)*/
var controlArray = Array(0, 8, 9, 10, 13, 27);
var intOut = 0;

if (bAllow==true)
{
if (charCode==8 ¦¦ charCode==9 ¦¦ charCode==37 ¦¦ charCode==39 ¦¦ charCode==46 ¦¦ charCode==116 ¦¦ (strList.indexOf(strChar)!=-1))
/*Valid*/
{
return true;
}
else
{
return false;
}
}
else
{
if (charCode==8 ¦¦ charCode==9 ¦¦ charCode==37 ¦¦ charCode==39 ¦¦ charCode==46 ¦¦ charCode==116 ¦¦ (strList.indexOf(strChar)==-1))
{
return true;
}
else
{
return false;
}
}

}

Twisted Mind

2:07 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



i dont really understand javascript and how the #*$! would i apply this ty bye :P

Twisted Mind

2:52 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



is there something missing in this code :?

Nutter

4:10 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



Add this in to your <input> tag...

onKeyPress="javascript:return limitinput(event, '0123456789', true);"

And only 0-9 will be allowed, plus backspace and other control codes.

LeChuck

6:48 pm on Dec 2, 2005 (gmt 0)

10+ Year Member



You may want to do this too, in case they have JS disabled:

if (ereg("^[0-9]+$", $input)) {
//do stuff
} else {
// return error page, something like "sorry, only numbers in this field"
}

Twisted Mind

7:42 am on Dec 5, 2005 (gmt 0)

10+ Year Member



am i allowed todo this

<script language="javascript">
bAllow = true

function limitinput(evt, strList, bAllow)
/*Limits the input to strList. If bAllow is true, then
only allow what is in strList. If bAllow is false,
then do not allow what is in strList.*/
{
var charCode = evt.keyCode;
if (charCode==0)
{
charCode = evt.which;
}
var strChar = String.fromCharCode(charCode);
/*controlArray holds the ASCII codes for valid
control commands (BS, CR, LF, etc)*/
var controlArray = Array(0, 8, 9, 10, 13, 27);
var intOut = 0;

if (bAllow==true)
{
if (charCode==8 ¦¦ charCode==9 ¦¦ charCode==37 ¦¦ charCode==39 ¦¦ charCode==46 ¦¦ charCode==116 ¦¦ (strList.indexOf(strChar)!=-1))
/*Valid*/
{
return true;
}
else
{
return false;
}
}
else
{
if (charCode==8 ¦¦ charCode==9 ¦¦ charCode==37 ¦¦ charCode==39 ¦¦ charCode==46 ¦¦ charCode==116 ¦¦ (strList.indexOf(strChar)==-1))
{
return true;
}
else
{
return false;
}
}

} </script><?php


/*** Add-to-Cart Button ***/
if (USE_AS_CATALOGUE!= '1' && $product_price!= "" &&!stristr( $product_price, $PHPSHOP_LANG->_PHPSHOP_PRODUCT_CALL )) {
$form_addtocart = "<form action=\"". $mm_action_url ."index.php\" method=\"post\" name=\"addtocart\" id=\"addtocart".$i."\">\n
<label for=\"quantity_".$i."\">".$PHPSHOP_LANG->_PHPSHOP_CART_QUANTITY.":</label>\n
<input id=\"quantity_".$i."\" class=\"inputbox\" type=\"text\" size=\"3\" name=\"quantity\" value=\"1\" />
<input onKeyPress=\"javascript:return limitinput(event, '0123456789', true);\" class=\"button\" type=\"submit\" name=\"submit\" value=\"".$PHPSHOP_LANG->_PHPSHOP_CART_ADD_TO."\" />\n
<input type=\"hidden\" name=\"category_id\" value=\"". @$_REQUEST['category_id'] ."\" />\n
<input type=\"hidden\" name=\"product_id\" value=\"". $db_browse->f("product_id") ."\" />\n
<input type=\"hidden\" name=\"page\" value=\"shop.cart\" />\n
<input type=\"hidden\" name=\"func\" value=\"cartadd\" />\n
<input type=\"hidden\" name=\"Itemid\" value=\"$Itemid\" />\n
<input type=\"hidden\" name=\"option\" value=\"com_phpshop\" />\n
</form>\n";

Twisted Mind

7:43 am on Dec 5, 2005 (gmt 0)

10+ Year Member



any help becouse i needs it :P

Twisted Mind

8:53 am on Dec 5, 2005 (gmt 0)

10+ Year Member



Help me dan godverdinges

Twisted Mind

9:05 am on Dec 5, 2005 (gmt 0)

10+ Year Member



OMG STUPID APPLYING IT ON A BUTTON! LOL

Twisted Mind

9:08 am on Dec 5, 2005 (gmt 0)

10+ Year Member



The following does not work any idea's what i did wrong?

<script language="javascript">
bAllow = true

function limitinput(evt, strList, bAllow)
/*Limits the input to strList. If bAllow is true, then
only allow what is in strList. If bAllow is false,
then do not allow what is in strList.*/
{
var charCode = evt.keyCode;
if (charCode==0)
{
charCode = evt.which;
}
var strChar = String.fromCharCode(charCode);
/*controlArray holds the ASCII codes for valid
control commands (BS, CR, LF, etc)*/
var controlArray = Array(0, 8, 9, 10, 13, 27);
var intOut = 0;

if (bAllow==true)
{
if (charCode==8 ¦¦ charCode==9 ¦¦ charCode==37 ¦¦ charCode==39 ¦¦ charCode==46 ¦¦ charCode==116 ¦¦ (strList.indexOf(strChar)!=-1))
/*Valid*/
{
return true;
}
else
{
return false;
}
}
else
{
if (charCode==8 ¦¦ charCode==9 ¦¦ charCode==37 ¦¦ charCode==39 ¦¦ charCode==46 ¦¦ charCode==116 ¦¦ (strList.indexOf(strChar)==-1))
{
return true;
}
else
{
return false;
}
}

} </script><?php


/*** Add-to-Cart Button ***/
if (USE_AS_CATALOGUE!= '1' && $product_price!= "" &&!stristr( $product_price, $PHPSHOP_LANG->_PHPSHOP_PRODUCT_CALL )) {
$form_addtocart = "<form action=\"". $mm_action_url ."index.php\" method=\"post\" name=\"addtocart\" id=\"addtocart".$i."\">\n
<label for=\"quantity_".$i."\">".$PHPSHOP_LANG->_PHPSHOP_CART_QUANTITY.":</label>\n
<input onKeyPress=\"javascript:return limitinput(event, '0123456789', true);\" id=\"quantity_".$i."\" class=\"inputbox\" type=\"text\" size=\"3\" name=\"quantity\" value=\"1\" />
<input class=\"button\" type=\"submit\" name=\"submit\" value=\"".$PHPSHOP_LANG->_PHPSHOP_CART_ADD_TO."\" />\n
<input type=\"hidden\" name=\"category_id\" value=\"". @$_REQUEST['category_id'] ."\" />\n
<input type=\"hidden\" name=\"product_id\" value=\"". $db_browse->f("product_id") ."\" />\n
<input type=\"hidden\" name=\"page\" value=\"shop.cart\" />\n
<input type=\"hidden\" name=\"func\" value=\"cartadd\" />\n
<input type=\"hidden\" name=\"Itemid\" value=\"$Itemid\" />\n
<input type=\"hidden\" name=\"option\" value=\"com_phpshop\" />\n
</form>\n";
}

Nutter

2:23 pm on Dec 5, 2005 (gmt 0)

10+ Year Member



Part of the problem may be that WW changes all pipe characters to broken pipes. Everywhere there is a pair of broken pipes, there should be two closed pipes - the character when you push shift-backspace.