Click here to Skip to main content
15,920,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox in a asp form and I want only to input numbers and "."
And I want to create a session to save in it the text in the text box to use it in another page
Thank you for your help
Posted

You should do it in Javascript as the event needed for this is too frequent, too costly to do it through the server.

Look at this sample:
JavaScript
<html>
   <head>
      <script type="text/javascript"><!--
         function filterDigits(eventInstance) { 
            eventInstance = eventInstance || window.event;
                key = eventInstance.keyCode || eventInstance.which;
            if ((47 < key) && (key < 58) || key = 45 || key == 8) {
               return true;
            } else {
                    if (eventInstance.preventDefault) eventInstance.preventDefault();
                    eventInstance.returnValue = false;
                    return false;
                } //if
         } //filterDigits
      --></script>
   </head>
<body">

<input type="text" onkeypress="filterDigits(event)"/>

</body>
</html>


It does exactly this: allows digits and '.' but also a backspace. By some historical reasons, backspace is processed as a character, not as a raw keyboard event, so it should be allowed separately.

—SA
 
Share this answer
 
v3
Comments
Dalek Dave 18-May-11 19:42pm    
Nice, I just plumped for the IsNumeric option.
Sergey Alexandrovich Kryukov 18-May-11 21:27pm    
Thank you, Dalek.
--SA
The Archive[^] may help.

IsNumeric is probably the best way to go though.
See Here[^]


Hope they help.


Mark up if they do!
 
Share this answer
 
lots of ways to do that
1) from javacript
2) via jquery
3) using ajax control kit validators

or simply use compare validatior in asp (use double as a type in your case)

C#
<br />
<asp:comparevalidator operator="DataTypeCheck" type="Double" controltovalidate="MyTextBox" display="None" errormessage="Valid Number is required." runat="server" /><br />


redirect to another page using text box value as a query parameter like:
C#
<br />
Response.Redirect("~/yourPage.aspx?value=" + MyTextBox.Text);<br />

OR
store it in a session on server side like below:
C#
<br />
Session["TBValue"] = MyTexyBox.Text;<br />
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900