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

Numbers Only Problem

Status
Not open for further replies.

liamba

Technical User
Jan 6, 2007
21
IE
Hi,
I have some javascript code that im trying to use so as to prevent users from entering any characters except for numbers in to a ASP textbox. Im trying to call this function on the "onkeypress" of the HTML table however it doesnt seem to be working.Below is the code im trying

Any help would be great.Thanks

<head runat="server">
<title>Score Card</title>

<script language=javascript type="text/javascript">

function NumberOnly()
{
//only accept numbers
if (event.keyCode>=48 && event.keyCode<=57)
{
event.returnValue=true;
}
else
{
event.returnValue=false;
}
}

</script>
</head>


<table border="1" width="100%" onkeypress="NumberOnly">
<tr>
<td width="10%"><asp:Label ID="Label3" runat="server" Text="Hole 1"></asp:Label></td>
<td width="10%"><asp:Label ID="Label4" runat="server" Text="Par 4"></asp:Label></td>
<td width="10%"><asp:Label ID="Label18" runat="server" Text="Index 16"></asp:Label></td>
<td width="10%"><asp:TextBox ID="txtHole1" runat="server" AutoPostBack="True" Width="75px" OnTextChanged="txtHole1_TextChanged"></asp:TextBox></td>
</tr>
</table>
 
would you consider this (example)?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
function doit(o) {
    var re = /[^0-9]/g;
    o.value = o.value.replace(re, "");
}
//--></script>
</head>

<body>

<form>
    <input type="text" name="t" onkeyup="doit(this)" />
</form>

</body>
</html>



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Reason i was using the table was that i have 18 textboxes so thought it might be easier to call the function with that table instead of 18 separate times.i have tried what you suggested but that involves removing the ASP.NET text box which i have to use for other parts of the code.
Any suggestions on how i might get around this?
 
gah.

ask the ASP forum how to attach event handlers to inputs.

you could also use javascript to loop through all textboxes and automatically attach the function to them.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
just an fyi..

to attach using javascript (referencing my example):

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
function doit() {
    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    var re = /[^0-9]/g;
    targ.value = targ.value.replace(re, "");
}

onload = function() {
    var e = document.getElementById("blah");
    var c = e.getElementsByTagName("input");
    for ( var i = 0; i < c.length; i++ ) {
        c[i].onkeyup = doit;
    }
}
//--></script>
</head>

<body>

<form id="blah">
    <input type="text" name="t1" />
    <input type="text" name="t2" />
    <input type="text" name="t3" />
    <input type="text" name="t4" />
    <input type="text" name="t5" />
    <input type="text" name="t6" />
    <input type="text" name="t7" />
    <input type="text" name="t8" />
</form>

</body>
</html>

now, that will just blindly attach the function to all INPUT elements within an object with an id of blah. you'd need to hone it down a bit if you have stuff you don't want that function attached to.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I have some javascript code that im trying to use so as to [!]prevent[/!] users from entering any characters except for numbers in to a ASP textbox

I don't think anything will prevent characters from being entered into a textbox, with javascript and for example cLFlaVA's code above, the character will be typed in and after it's in the textbox, it will be replaced by the empty string. It will appear like the incorrect data is getting erased from right to left.

[small]"There's an old saying in Tennessee — I know it's in Texas, probably in Tennessee — that says, fool me once, shame on — shame on you. Fool me — you can't get fooled again." - George W. Bush[/small]
<.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top