Use javascript to restrict some text input in the text box

Source: Internet
Author: User

<Scr platinum pt>
Function regInput (obj, reg, inputStr)
{
Var docSel = document. selection. createRange ()
If (docSel. parentElement (). tagName! = "INPUT") return false
OSel = docSel. duplicate ()
OSel. text = ""
Var srcRange = obj. createTextRange ()
OSel. setEndPoint ("StartToStart", srcRange)
Var str = oSel. text + inputStr + srcRange. text. substr (oSel. text. length)
Return reg. test (str)
}
</Scr platinum pt>
Lowercase English: <xmp style = "display: inline"> </xmp>
<Input onkeypress = "return regInput (this,/^ [a-z] * $/, String. fromCharCode (event. keyCode ))"
Onpaste = "return regInput (this,/^ [a-z] * $/, window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this,/^ [a-z] * $/, event. dataTransfer. getData ('text '))"
Style = "ime-mode: Disabled"
> <Br>
Uppercase: <xmp style = "display: inline"> </xmp>
<Input onkeypress = "return regInput (this,/^ [A-Z] * $/, String. fromCharCode (event. keyCode ))"
Onpaste = "return regInput (this,/^ [A-Z] * $/, window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this,/^ [A-Z] * $/, event. dataTransfer. getData ('text '))"
Style = "ime-mode: Disabled">
<Br>
Any number: <xmp style = "display: inline"> </xmp>
<Input onkeypress = "return regInput (this,/^ [0-9] * $/, String. fromCharCode (event. keyCode ))"
Onpaste = "return regInput (this,/^ [0-9] * $/, window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this,/^ [0-9] * $/, event. dataTransfer. getData ('text '))"
Style = "ime-mode: Disabled"
> <Br>
Limit 2 decimal places: <xmp style = "display: inline"> </xmp>
<Input onkeypress = "return regInput (this,/^ \ d *\.? \ D {0, 2 }$/, String. fromCharCode (event. keyCode ))"
Onpaste = "return regInput (this,/^ \ d *\.? \ D {0, 2} $/, window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this,/^ \ d *\.? \ D {0, 2} $/, event. dataTransfer. getData ('text '))"
Style = "ime-mode: Disabled"
> For example, 123.12 <br>

Date: <xmp style = "display: inline"> </xmp>
<Input onkeypress = "return regInput (this, /^ \ d {} ([-\/] (\ d {} ([-\/] (\ d })?)?)?)? $/, String. fromCharCode (event. keyCode ))"
Onpaste = "return regInput (this, /^ \ d {} ([-\/] (\ d {} ([-\/] (\ d })?)?)?)? $/, Window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this, /^ \ d {} ([-\/] (\ d {} ([-\/] (\ d })?)?)?)? $/, Event. dataTransfer. getData ('text '))"
Style = "ime-mode: Disabled"
> Example: 2002-9-29 <br>
Any Chinese: <xmp style = "display: inline"> </xmp>
<Input onkeypress = "return regInput (this,/^ $/, String. fromCharCode (event. keyCode ))"
Onpaste = "return regInput (this,/^ [\ u4E00-\ u9FA5] * $/, window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this,/^ [\ u4E00-\ u9FA5] * $/, event. dataTransfer. getData ('text '))"
> <Br>
<Xmp style = "display: inline"> </xmp>
<Input onkeypress = "return regInput (this,/^ [a-e] * $/, String. fromCharCode (event. keyCode ))"
Onpaste = "return regInput (this,/^ [a-e] * $/, window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this,/^ [a-e] * $/, event. dataTransfer. getData ('text '))"
Style = "ime-mode: Disabled"
> Range: a, B, c, d, e <br>
Partially Chinese: <xmp style = "display: inline"> </xmp>
<Scr limit pt language = Limit Cr limit pt>
Function checkChinese (oldLength, obj)
{
Var oTR = too many Doc ument. selection. createRange ()
Var reg =/[^ 80 or 90,]/g
OTR. moveStart ("character",-1 * (obj. value. length-oldLength ))
OTR. text = oTR. text. replace (reg ,"")
}
</Scr platinum pt>
<Input
Onpaste = "return regInput (this,/^ [80 or 90,] * $/, window. clipboardData. getData ('text '))"
Ondrop = "return regInput (this,/^ [80 or 90,] * $/, event. dataTransfer. getData ('text '))"
> Scope: 1234, 80 or 90 <br>

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.