JavaScript verification knowledge collation
- 2021-08-05 08:46:35
- OfStack
JS can only enter regular expressions for numbers, numbers, letters, and so on
1. The text box can only enter numeric codes (nor decimal points)
<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.
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
3. Numbers and decimal points method 2
<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}">
4. You can only enter letters and Chinese characters
<input onkeyup="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers">
5. You can only enter English letters and numbers, not Chinese
<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">
6. Enter only numbers and English < font color="Red" > chun < /font >
<input onKeyUp="value=value.replace(/[^\d|chun]/g,'')">
7. There can only be two digits after the decimal point (both numbers and Chinese can be entered), and letters and operation symbols cannot be entered:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
8. There can only be up to two digits after the decimal point (numbers, letters and Chinese can be entered), and operation symbols can be entered:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
Prohibit special characters:
onKeyPress="if(event.keyCode < 45 || event.keyCode > 57 ) event.returnValue = false;"
Only Chinese characters can be entered:
<input onkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/u4E00-/u9FA5]/g,''))">
style= "ime-mode: disabled" Chinese character input method is prohibited
Only numbers can be entered:
<input onkeyup="value=value.replace(/[^/d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))">
Enter only English and numbers:
<input onkeyup="value=value.replace(/[/W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))">
The control input box can only enter words or numbers, and can not allow special characters to be entered
The following characters are not allowed here: (like ^ & * Etc.) < br >
<textarea rows=2 cols=20 name=comments onKeypress="if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;">
Only spaces are prohibited
onkeyup="value=value.replace(//s/g,'')"
onkeydown="if(event.keyCode==32) return false"
Only Chinese and English can be entered:
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
0
Special characters and spaces are not allowed:
<input id="code" onkeypress="return ValidateSpecialCharacter();" onblur="validate(this)"/>
Cannot be empty
<input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')">
The judgment character is composed of letters and numbers, underscores and dots. And only underscores and letters can be used at the beginning
/^([a-zA-z_]{1})([\w]*)$/g.test(str)
You can only enter numbers
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
1
You can only enter Chinese
<input type="text" onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')">
Enter English only
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
2
You can only enter Chinese, English, numbers, @ symbol and. symbol
<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'')">
You can only enter English, and you can't paste or pop up the paste menu
<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,'')" onkeydown="fncKeyStop(event)" onpaste="return false" oncontextmenu = "return false"/>
Only numbers and dots can be entered. (Note: d in [^\ d\.] cannot be written in uppercase D, otherwise it becomes all characters except numbers.)
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
4
To sum up: First, < input > Enter onkeyup= "value=value. replace (/[^\ X]/g, '')" and then replace X in (/[\ X]/g,'') with the code you want to enter
English: u4E00-u9FA5
Numbers: d, 0-9
English: a-z, A-Z
Other symbols @, dots or other symbols. You can also have multiple symbols, just separate them by\.
For example:
Chinese, English and numbers with @ symbol and dot symbol:\ a-\ z\ A-\ Z0-9\ u4E00-\ u9FA5\ @\.
If you can't right-click the pop-up menu and paste the copied information in the text box, you should < input > Enter onKeyDown= "fncKeyStop (event)" onpaste= "return false" oncontextmenu= "return false; "
Its 1, only numbers and decimal points are allowed.
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
5
Its 2, the judgment is more detailed 1, even 22. 2, which is not a number, can be judged
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
6
Its 3, only integers are allowed. In fact, it is entirely possible to make some restrictions by citing 1 against 3 according to Article 3.
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert(' You can only enter numbers ');this.value='';}">
7
Conclusion, actually
style="ime-mode:Disabled
This sentence is more practical. Turn off the input method. Save some people open full-angle input numbers, and the result can't be input to find you crying and wiping tears, and blame your poor design.
Only numbers are allowed
<input onKeyUp="value=value.replace(/[^\d|chun]/g,'')">
0
Only English letters, numbers and underscores are allowed (the following two methods are realized)
<input name="username" type="text" style="ime-mode:disabled">
<input name="username" type="text" onkeyup="value=value.replace(/[^/w/.//]/ig,'')">
Only English letters, numbers and & =@
<input name="username" type="text" onkeyup="value=value.replace(/[^/w=@&]|_/ig,'')">
Only English letters, numbers and & =@
<input name="username" type="text" onkeyup="value=value.replace(/[^/w=@&]|_/ig,'')">
Only Chinese characters are allowed
<input name="username" type="text" onkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')">