How does javascript limit the type of text box input values?

Source: Internet
Author: User

How does javascript limit the type of text box input values?

This article describes how to restrict the types of input values in a text box in javascript. It involves javascript mouse events, keyboard buttons, and string related operation skills. It is very useful. For more information, see.

This example describes how to restrict the input value type of a text box in javascript. Share it with you for your reference. The specific analysis is as follows:

Requirements: only numbers and decimal places can be entered in all text boxes. Other symbols cannot be entered;

The key point is that I want to limit the number when the user inputs, instead of determining when the user submits -- that is to say, if the user enters a number or decimal point in the text box, the user can enter the number normally. If the user inputs non-numeric characters such as letters, the text box does not respond, and the entered characters are not displayed.
======== Below is the sample code section ====




Var s = "<input type = \" text \ "size = \" 20 \ "" +

"Style = \" text-align: center \ "" +

"Onkeydown = \" if (event. keyCode> 57 & event. keyCode! = 190) return false \ "" +

// Only numbers can be entered

"Onblur = \" value = value. replace (/[^ 0-9 \.]/g, '') \" "+

// Restrict the mouse to enter non-Numbers

"Onbeforepaste = \" clipboardData. setData (\ "text \", "+

// Only numbers can be pasted.

"ClipboardData. getData (\" text \ "). replace (/[^ 0-9 \.]/g,'') \ "> ";

Document. write (s );

Document. write ('HTTP: // /');




================== End of the sample code ======

I hope this article will help you design javascript programs.


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: 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.