text box limit input type <input> input box

Source: Internet
Author: User

Recently after the development of a project, and testers to test the bug, and then I based on the bug they tested a change, and then encountered a problem, the text box is used to search, but, for example, this text box is used to search for the age of the interval, when entering the condition, if you enter a non-numeric character can not be entered. Problems like this can actually be done through regular expressions, and we've developed a project that has a corresponding regular expression validation, but it's not that easy to do in this framework, and this method is probably not the best, but it solves the current problem.

1. Only import and paste Chinese characters
<input onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ')) "><br/>

2. Only enter and paste numbers
<input onkeyup= "This.value=this.value.replace (/\d/g, ')" onafterpaste= "This.value=this.value.replace (/\D/g, ' ) "/><br/>

3. Digital scripting
<input onkeyup= "if (/\d/.test (this.value)) {alert (' Input number only '); this.value= ';}" ><br/>

4. Only enter numbers and English
<input onkeyup= "Value=value.replace (/[\w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')) "><br/>

5. Simple forbidden to enter Chinese characters
<input style= "ime-mode:disabled" > IME does not convert, but can be pasted on <br/>

6. Enter numbers and decimal points
<input onkeyup= "Value=value.replace (/[^\d{1,}\.\d{1,}|\d{1,}]/g, ')"/><br/>

7. Only numbers and "-", for example, can be used when entering a time
<input onkeyup= "Value=value.replace (/[^\w&=]|_/ig, ')" onblur= "Value=value.replace (/[^\w&-]|_/ig, ')" />

JS control input character limit

Enter key to move the cursor to the next input box

<input onkeydown= "if (event.keycode==13) event.keycode=9" > can only be Chinese
<input onkeyup= "Value=value.replace (/[-~]/g, ')" onkeydown= "if (event.keycode==13) event.keycode=9" > Shielded Input Method <input style= "ime-mode:disabled" onkeydown= "if (event.keycode==13) event.keycode=9" > can only input English and digital
<input onkeyup= "Value=value.replace (/[/w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ') "onkeydown=" if (event.keycode==13) event.keycode=9 "> can only be digital
<input onkeyup= "Value=value.replace (/[^/d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ')) ">


Can only be displayed, cannot be modified

<input readonly value= "can only display, can not be modified" > can only lose numbers, determine the value of the key
<script language=javascript>
function Onlynum ()
{
if (! ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105) | | (event.keycode==8)))
Event.returnvalue=false;
}
</script>
<input onkeydown= "Onlynum (); >


1. Text box can only enter a numeric code (the decimal point can not be entered)
<input onkeyup= "This.value=this.value.replace (//d/g, ')" onafterpaste= "This.value=this.value.replace (//D/g, ' ) ">

2. Only the number can be entered and the decimal point will be lost.
<input onkeyup= "if (IsNaN (value)) ExecCommand (' Undo ')" Onafterpaste= "if (IsNaN (value)) ExecCommand (' Undo ')" >
<input name=txt1 onchange= "if (//d/.test (this.value)) {alert (' Input number only '); this.value= ';}" >

3. Number and decimal method two

<input type=text t_value= "" o_value= "" onkeypress= "if (!this.value.match.? /d*?$/)) This.value=this.t_value;else this.t_value=this.value;if (This.value.match (?: [/^ (?:/. /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+)?)? $/)) This.o_value=this.value "onblur=" if (!this.value.match (?: [/^ (?:/. /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 kanji can be entered
<input onkeyup= "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 can be entered, not Chinese
<input onkeyup= "Value=value.replace (/[^/w/.//]/ig, ')" >

6. Only enter numbers and English
<font color= "Red" >chun</font>
<input onkeyup= "Value=value.replace (/[^/d|chun]/g, ')" >
7. Only a maximum of two digits after the decimal point (numeric, Chinese can be entered), can not enter letters and operation symbols: <input onkeypress= "if ((event.keycode<48 | | event.keycode>57) & & Event.keycode!=46 | | ./d/d$/.test (value) event.returnvalue=false ">8. Only up to two digits after the decimal point (numbers, letters, Chinese can be entered), you can enter the operation symbol:
<input onkeyup= "This.value=this.value.replace (/^ (/-) * (/d+)/. ( /D/D). *$/, ' $1$2.$3 ') ">
can only be numbers and decimal points and plus minus multiply
9. Only numbers, decimal points, negative numbers can be entered

<input name= "Input" type= "text" onkeyup= "Jhshnumbertext (This)" id= "title" >

<script language= "javascript" type= "Text/javascript" >function Jhshnumbertext (a)
{
var fa= "";
if (a.value.substring (0,1) = = "-")
Fa= "-";
var str= (A.value.replace (/[^0-9.) /g, "). Replace (/[.) [0-9]*[.] /, ‘.‘);
if (str.substring (0,1) = = ".")
Str= "0" +STR;
A.VALUE=FA+STR;
}
</script>


1. Cancel the dashed box when the button is pressed, add the attribute value in input hidefocus or hidefocus=true
<input type= "Submit" value= "Commit" hidefocus= "true"/>

2. Read-only text box contents, add attribute value in input readonly
<input type= "text" readonly/>

3. Prevent the text document from being emptied (you can use the style content as a class reference)
<input type= "text" style= "Behavior:url (#default #savehistory);"/>

4.ENTER key to move the cursor to the next input box
<input type= "text" onkeydown= "if (event.keycode==13) event.keycode=9"/>

5. Only Chinese (with flashing)
<input type= "text" onkeyup= "Value=value.replace (/[-~]/g, ')" onkeydown= "if (event.keycode==13) event.keyCode=9"/ >

6. Only digital (with flashing)
<input type= "text" onkeyup= "Value=value.replace (/[^/d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ') "/>

7. Only digital (no flashing)
<input type= "text" style= "ime-mode:disabled" onkeydown= "if (event.keycode==13) event.keycode=9" onkeypress= "if (( event.keycode<48 | | event.keycode>57)) Event.returnvalue=false "/>

8. Only input English and numerals (with flashing)
<input type= "text" onkeyup= "Value=value.replace (/[/w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ') "/>

9. Shielding Input Method
<input type= "text" name= "url" style= "ime-mode:disabled" onkeydown= "if (event.keycode==13) event.keycode=9"/>

10. Enter only digits, decimal point, minus (-) character (no flashing)
<input onkeypress= "if (event.keycode!=46 && event.keycode!=45 && (event.keycode<48 | | event.keycode>57)) Event.returnvalue=false "/>

11. Only two decimal places can be entered, three decimal places (with flashing)
<input type= "Text" maxlength= "9" onkeyup= "if (Value.match (/^/d{3}$/)) Value=value.replace (Value,parseint (value/ Value=value.replace (//./d*/./g, '. ') "onkeypress=" if ((event.keycode<48 | | event.keycode>57) && event.keycode!=46 && event.keycode!=45 | | Value.match (/^/d{3}$/) | | ./d{3}$/.test (value)) {Event.returnvalue=false} "/>

text box limit input type <input> input box

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.