Javascript can only enter numbers and decimal text boxes

Source: Internet
Author: User

Only text boxes with numbers and decimal places can be entered
Html code

  1. <Html>
  2. <Head>
  3. <Meta http-equiv = "content-Type" content = "text/html; charset = gb2312">
  4. </Head>
  5. <Body>
  6. <! -- Add the following code to the <body> and </body> -->
  7. Only text boxes with numbers and decimal places can be entered:<InputOnkeyup = "value = value. replace (/[^ d.]/g ,'')">
  8. </Body>
  9. </Html>

<Html>
Decimal point judgment
1. The first input must be a number, not a decimal point. Example: 111
2. There cannot be multiple. Consecutive, but only one. Example 12... 1
3. There is not much one. in different places. For example, 12.1.1
Html code

  1. <Html>
  2. <Head>
  3. <Meta http-equiv = "content-Type" content = "text/html; charset = gb2312">
  4. <Title> javascript can only enter numbers and decimal places </title>
  5. <ScriptLanguage = "JavaScript" type = "text/javascript">
  6. Function clearNoNum (obj)
  7. {
  8. // Replace all non-numeric values first, except numbers and.
  9. Objobj. value = obj. value. replace (/[^ d.]/g ,"");
  10. // Ensure that the first digit is a number instead of a number.
  11. Objobj. value = obj. value. replace (/^./g ,"");
  12. // Ensure that there is only one and no more.
  13. Objobj. value = obj. value. replace (/. {2,}/g ,".");
  14. // Ensure that the message appears only once, but not more than twice.
  15. Objobj. value = obj. value. replace (". "," $ # $ "). replace (/. /g ,""). replace ("$ # $ ",". ");
  16. }
  17. </Script>
  18. </Head>
  19. <Body>
  20. <! -- Add the following code to the <body> and </body> -->
  21. Only text boxes with numbers and decimal places can be entered:<InputId = "input1" onkeyup = "clearNoNum (this )">
  22. </Body>
  23. </Html>

<Html>

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.