Enter a text box with only numbers

Source: Internet
Author: User

Hello, everyone. I am very happy to write some attention here.

Tonight, I want to tell you how to write a small verification for the text box ------- text box verification that can only enter numbers.

I know that this is not something worth showing off, but it is indeed a learning point for colleagues who have no access to it.

Okay, go to today's topic.

It is actually two sectionsCode(^-^ If you have no foundation at all, please contact me because I am also a beginner and understand your desire to start the first instance ):

One is the script:

<SCRIPT type = "text/JavaScript"> <br/> function keyValue () <br/>{< br/> If (event. keycode> = 48 & event. keycode <= 57) | (event. keycode> = 96 & event. keycode <= 105) | (event. keycode = 8) | (event. keycode = 46) // determines whether it is a number, an 8-digit backspace key, or a 46delete key <br/> event. returnvalue = true; <br/> else <br/> event. returnvalue = false; <br/>}< br/> </SCRIPT>

The second is to add the event onkeydown to the text box.

<Asp: textbox id = "textbox1" runat = "server" onkeydown = "keyValue ()"> </ASP: textbox>

It is also strange to say that when you drag a text box to the interface, you cannot find the onkeydown event in any way, but you directly enterOnkeydown = "keyValue ()",It actually plays a role. In fact, this onkeydown event is in the text box in HTML, but when we use the text box control on the server side, it does not have this event, but the problem is that the control on the server end will eventually be converted to HTML labels (at least I think so now, because when I run the aspx file and view the source file, the text box on the server end is changed<Input name = "textbox1" type = "text" id = "textbox1" onkeydown = "keyValue ()"/>)

To be honest, we may not think of any difficulty in the result of the single argument, but who knows that the code of the first script needs to be solved with painstaking efforts, because the numbers at the beginning do not include the keypad numbers, and I am looking at the http://hi.baidu.com/anylzer/blog/item/ad7b3b0ebe1ecdcc7acbe129.html here ascll code is not able to find the keypad ascll code, but with the help of some forums, finally, I found a good way to view it, that is, to display the ascll code of my ammonium key. This code can be used:

<SCRIPT type = "text/JavaScript <br/> function test () {<br/> alert (event. keycode) <br/>}< br/> </SCRIPT>

The text box code is as follows:

<Input type = "text" onkeydown = "test ()"/>

Then runProgramAnd enter a number in the text box, from 0 to 9. The output value is 96 to 105.

Okay. Let's write it here tonight. If you have any questions, please share it with me. Thank you!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.