Text Box input restrictions

Source: Internet
Author: User

1. Only numbers can be entered in the text box above.Code(Decimal point cannot be entered ):

<Input onkeyup = "this. value = This. value. replace (/\ D/g, '')" onafterpaste = "this. value = This. value. replace (/\ D/g, '')">

2. Only numbers can be entered and decimal points can be entered. (Note Chinese characters. There is a difference between using the input method and copying the above three values)

<Input onkeyup = "If (isnan (value) Execcommand ('undo ')" onafterpaste = "If (isnan (value) Execcommand ('undo')">

<Input name = txt1 onchange = "If (/\ D /. test (this. value) {alert ('only numbers allowed '); this. value = '';}"> <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} ">

3. Only letters and Chinese characters can be entered.

<Input onkeyup = "value = value. replace (/[\ D]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[\ D]/g, '')" maxlength = 10 name = "Numbers">

4. Only English letters and numbers are allowed, and Chinese characters are not allowed.

<Input onkeyup = "value = value. Replace (/[^ \ W \. \/]/ig,'') ">

5. Only numbers and English Chun numbers can be entered.

<Input onkeyup = "value = value. Replace (/[^ \ d | Chun]/g,'') ">

You can enter a maximum of two digits (numbers and Chinese characters) after a decimal point. You cannot enter letters or operator numbers:

<Input onkeypress = "If (event. keycode <48 | event. keycode> 57) & event. keycode! = 46 |/\. \ D $/. Test (value) event. returnvalue = false ">

You can enter up to two digits (numbers, letters, and Chinese characters) after the decimal point. You can enter an operator number.

<Input onkeyup = "this. value = This. value. replace (/^ (\-) * (\ D + )\. (\ D ). * $/, '$1 $2. $ 3') ">

Appendix:
Regular expressions are used for string processing, form verification, and other occasions. They are practical and efficient, but they are always not sure when used, so they often need to be checked online. I will add some frequently-used expressions to my favorites for memo. This post is updated at any time.

Regular Expression matching Chinese characters: [\ u4e00-\ u9fa5]

Match double-byte characters (including Chinese characters): [^ \ x00-\ xFF]

Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)

String. Prototype. Len = function () {return this. Replace ([^ \ x00-\ xFF]/g, "AA"). length ;}

Regular Expression for matching empty rows: \ n [\ s |] * \ r

Regular Expressions matching HTML tags:/<(. *)>. * <\/\ 1> | <(. *) \/>/

Regular Expression matching spaces at the beginning and end: (^ \ s *) | (\ s * $)

Application: JavaScript does not have trim functions like VBScript. We can use this expression to implement it, as shown below:

String. Prototype. Trim = function ()
{
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}

Use regular expressions to break down and convert IP addresses:

The following describes how to use a regular expression to match an IP address and convert the IP address to a corresponding value in Javascript.Program:

Function ip2v (IP)
{
Re =/(\ D +) \. (\ D +)/g // Regular Expression matching IP addresses
If (Re. Test (IP ))
{
Return Regexp. $1 * Math. Pow (255) + Regexp. $2 * Math. Pow () + Regexp. $3 * + Regexp. $4*1
}
Else
{
Throw new error ("not a valid IP address! ")
}
}

However, if the above program does not use regular expressions, it may be easier to directly use the split function to separate them. The program is as follows:

VaR IP = "10.100.0000168"
IP = IP. Split (".")
Alert ("the IP value is: "+ (IP [0] * 255*255*255 + IP [1] * 255*255 + IP [2] * 255 + IP [3] * 1 ))

The regular expression matching the email address: \ W + ([-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W + )*

The regular expression matching the URL: http: // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)?

The regular expression is used to remove repeated characters in the string.AlgorithmProgram: [Note: This program is incorrect. For the reason, see this post.]

VaR S = "abacabefgeeii"
VaR S1 = S. Replace (/(.). * \ 1/g, "$1 ")
VaR Re = new Regexp ("[" + S1 + "]", "G "? Joan? Too many p ????? Why is it slow? Using M ?)
VaR S2 = S. Replace (Re ,"")
Alert (S1 + S2) // The result is: abcefgi

I used to post on csdn to seek an expression to remove repeated characters, but I couldn't find it. This is the simplest implementation method I can think. The idea is to use the back-to-back reference to retrieve repeated characters, then create a second expression with repeated characters, get non-repeated characters, and connect the two. This method may not apply to strings with character order requirements.

Javascript programs that extract file names from URLs using regular expressions. the following result is page1.

S = "http://www.9499.net/page1.htm"
S = S. Replace (/(. * \/) {0,} ([^ \.] +). */ig, "$2 ")
Alert (s)

Use regular expressions to restrict text box input in a webpage form:

You can only enter Chinese characters using regular expressions: onkeyup = "value = value. replace (/[^ \ u4e00-\ u9fa5]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ u4e00-\ u9fa5]/g ,''))"

You can only enter the full-width characters: onkeyup = "value = value. replace (/[^ \ uff00-\ Uffff]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ uff00-\ Uffff]/g ,''))"

Use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. replace (/[^ \ D]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g ,''))"

You can only enter numbers and English letters using regular expressions: onkeyup = "value = value. replace (/[\ W]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[^ \ D]/g ,''))"

Supplement:
^ \ D + $ // match a non-negative integer (positive integer + 0)
^ [0-9] * [1-9] [0-9] * $ // match a positive integer
^ (-\ D +) | (0 +) $ // match a non-positive integer (negative integer + 0)
^-[0-9] * [1-9] [0-9] * $ // match a negative integer
^ -? \ D + $ // match the integer
^ \ D + (\. \ D + )? $ // Match non-negative floating point number (Positive floating point number + 0)
^ ([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ // matches the Positive floating point number
^ (-\ D + (\. \ D + )?) | (0 + (\. 0 + )?)) $ // Match a non-Positive floating point number (negative floating point number + 0)
^ (-([0-9] + \. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ // matches a negative floating point number.
^ (-? \ D +) (\. \ D + )? $ // Match floating point numbers
^ [A-Za-Z] + $ // match a string consisting of 26 English letters
^ [A-Z] + $ // match a string consisting of 26 uppercase letters
^ [A-Z] + $ // match a string consisting of 26 lowercase letters
^ [A-Za-z0-9] + $ // match a string consisting of digits and 26 letters
^ \ W + $ // match a string consisting of digits, 26 English letters, or underscores
^ [\ W-] + (\. [\ W? Joan? Too many p ????? Why is it slow? Between M? -] +) * @ [\ W-] + (\. [\ W-] +) + $ // match the email address
^ [A-Za-Z] +: // match (\ W + (-\ W + )*)(\. (\ W + (-\ W + )*))*(\? \ S *)? $ // Match the URL

Algorithm program that uses regular expressions to remove repeated characters in a string:

var S = "abacabefgeeii"
var S1 = S. replace (/(.). * \ 1/g, "$1")
var Re = new Regexp ("[" + S1 + "]", "G ")
var S2 = S. replace (Re, "")
alert (S1 + S2) // The result is: abcefgi
=====================================< br> If VaR S = "abacabefggeeii"
the result is incorrect, result: abeicfgg
the regular expression has limited capabilities

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.