Regular Expression for JavaScript cursor exit to determine number

Source: Internet
Author: User

 

 

Javascript judgment number

 

Bind events to the text box. When the focus enters the text box, cancel document. onkeydown and assign a value to document. onkeydown again when the focus leaves. For example, the text box can be written as follows: <input type = "text" onfocus = "document. onkeydown = jumppage" onblur = "document. onkeydown = NULL;"/>

 

// Integer format <br/> patternsdict. long =/^ (0 | [1-9]/D *) $/; <br/> // decimal format <br/> patternsdict. double =/^ (0 | [1-9]/D *)/. (/d +) $/; <br/> // value format <br/> patternsdict. number =/^ (0 | [1-9]/D *) $ | ^ (0 | [1-9]/D *)/. (/d +) $/; <br/> // natural number <br/> patternsdict. positive =/^ ([1-9]/D *) $/; <br/> // negative integer <br/> patternsdict. negative =/^ (-([1-9]/D *) $/; <br/> 1. only numeric code can be entered in the text box (decimal point cannot be entered) <br/> <input onkeyup = "this. value = This. value. replace (// D/g, '')" onafterpaste = "This. value = This. value. replace (// D/g, '')"> <br/> 2. only numbers are allowed, and decimal points are allowed. <br/> <input onkeyup = "If (isnan (value) Execcommand ('undo ')" onafterpaste = "If (isnan (value) Execcommand ('undo ') "> <br/> <input name = txt1 onchange =" If (// D /. test (this. value) {alert ('only numbers allowed '); this. value = '';}"> <br/> 3. method 2: Number and decimal point <br/> <input type = text t_value = "" o_value = "" onkeypress = "If (! This. value. Match (/^ [/+/-]? /D *? /.? /D *? $/) This. value = This. t_value; else this. t_value = This. value; If (this. value. Match (/^ (?: [/+/-]? /D + (?: //./D + )?)? $/) This. o_value = This. Value "onkeyup =" If (! This. value. Match (/^ [/+/-]? /D *? /.? /D *? $/) This. value = This. t_value; else this. t_value = This. value; If (this. value. Match (/^ (?: [/+/-]? /D + (?: //./D + )?)? $/) This. o_value = This. Value "onblur =" 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} "> <br/> 4. only letters and Chinese characters can be entered. <br/> <input onkeyup = "value = value. replace (/[/d]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[/d]/g, '')" maxlength = 10 name = "Numbers"> <br/> 5. only English letters and numbers are allowed. Chinese characters are not allowed. <br/> <input onkeyup = "Va Lue = value. replace (/[^/W /. //]/ig, '')"> <br/> 6. only numbers and English letters can be entered <font color = "red"> Chun </font> <br/> <input onkeyup = "value = value. replace (/[^/d | Chun]/g, '')"> <br/> 7. only two digits can be entered after the decimal point. Letters and operator numbers are not allowed: <br/> <input onkeypress = "If (event. keycode <48 | event. keycode> 57) & event. keycode! = 46 | //. /D/d $ /. test (value) event. returnvalue = false "> <br/> 8. only two digits (numbers, letters, and Chinese characters) can be entered after the decimal point. You can enter the operator number: <br/> <input onkeyup = "this. value = This. value. replace (/^ (/-) * (/d + )/. (/D/d ). * $/, '$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.