Js restricts input to only numbers, letters, and Chinese characters.

Source: Internet
Author: User

Js restricts input to only numbers, letters, and Chinese characters.

Js restricts that input can only be a number or a decimal point, or only a number or a decimal point, etc. This article has roughly sorted out some items and interested friends can add them to their favorites.

The Code is as follows:

<Input type = "text" onKeyUp = "this. value = this. value. replace (/[^. d]/g, ''); if (this. value. split ('. '). length> 2) {this. value = this. value. split ('. ') [0] + '. '+ this. value. split ('. ') [1]} ">

 

1. Only numeric code can be entered in the text box (decimal point cannot be entered)

The Code is as follows:

<Input parameter nkeyup = "this. value = this. value. replace (/D/g, ''') "using nafterpaste =" this. value = this. value. replace (/D/g, ''') ">

 

2. Only numbers can be entered, and decimal points can be entered.

The Code is as follows:

<Input parameter nkeyup = "if (isNaN (value) execCommand (''undo'') "using nafterpaste =" if (isNaN (value )) execCommand (''undo '')">

<Input name = txt1 then nchange = "if (/D /. test (this. value) {alert (''can only enter numbers''); this. value = ''';} ">

 

3. Number and decimal point method 2

The Code is as follows:

<Input type = text t_value = "" o_value = "" Export nkeypress = "if (! This. value. match (/^ [+-]? D *?.? D *? $/) This. value = this. t_value; elsethis. t_value = this. value; if (this. value. match (/^ (? : [+-]? D + (? :. D + )?)? $/) This. o_value = this. value "encode nkeyup =" if (! This. value. match (/^ [+-]? D *?.? D *? $/) This. value = this. t_value; elsethis. t_value = this. value; if (this. value. match (/^ (? : [+-]? D + (? :. D + )?)? $/) This. o_value = this. value "returns nblur =" if (! This. value. match (/^ (? : [+-]? D + (? :. D + )? |. D *?)? $/) This. value = this. o_value; else {if (this. value. match (/^. d + $/) this. value = 0 + this. value; if (this. value. match (/^. $ /))

This. value = 0; this. o_value = this. value} ">

 

4. Only letters and Chinese characters can be entered

The Code is as follows:

<Input parameter nkeyup = "value = value. replace (/[d]/g, ''') "onbeforepaste =" clipboardData. setData (''text'', clipboardData. getData (''text '')

. Replace (/[d]/g, ''') "maxlength = 10 name =" Numbers ">

 

5. Only English letters and numbers are allowed. Chinese characters are not allowed.

Copy the Code as follows:

<Input parameter nkeyup = "value = value. replace (/[^ w./]/ig, ''')">

 

6. Only numbers and English letters can be entered.

The Code is as follows:

<Input parameter nKeyUp = "value = value. replace (/[^ d | chun]/g, ''')">

 

7. After the decimal point, you can enter a maximum of two digits (numbers and Chinese characters). You cannot enter letters or operator numbers:

The Code is as follows:

<Input parameter nKeyPress = "if (event. keyCode <48 | event. keyCode> 57) & event. keyCode! = 46 |/. dd $/. test (value) event. returnValue = false ">

 

8. A maximum of two digits (numbers, letters, and Chinese characters) can be entered after the decimal point. You can enter the operator number:

The Code is as follows:

<Input parameter nkeyup = "this. value = this. value. replace (/^ (-) * (d + ). (dd ). * $/, ''$1 $2. $3 '')">

 

Related Article

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.