Restrict input types: Numbers, decimal points, and negative numbers, and input negative numbers

Source: Internet
Author: User

Restrict input types: Numbers, decimal points, and negative numbers, and input negative numbers

The input type is limited. Requirements: numbers can contain decimal points and negative numbers, and the negative number can only appear at the leftmost. the decimal point and negative number can only appear once, and cannot exceed 2 digits after the decimal point.


This setting is most suitable for the input of the currency amount. It has the n-digit limit (n> 1) after the decimal point and the n-digit limit (n> 1); it has positive and negative values (borrow); the negative number can only appear at the leftmost; the decimal point and negative number can only appear once.


In the <Head> ...... </Head>

<SCRIPT language = "javascript"> function checkNum (obj) {// check whether it is a non-numeric value if (isNaN (obj. value) {obj. value = "";} if (obj! = Null) {// check whether two if (obj. value. toString (). split (". "). length> 1 & obj. value. toString (). split (". ") [1]. length> 2) {alert ("no more than two digits after the decimal point! "); Obj. value =" ";}}</SCRIPT>

In the INPUT box

<Input type = "text" name = "cash" id = "cash" onkeyup = "checkNum (this)"/>



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.