SoFunction
Updated on 2025-03-10

JS can only enter regular expressions such as numbers or numbers and letters.

JS judges can only be numbers and decimal points
0. Cannot enter Chinese
1)<input onpaste="return false;" type="text" name="textfield" style="width:400px; ime-mode:disabled" value="">
2)
Copy the codeThe code is as follows:

<script>
function chkIt(frm){
if (frm.>0&&frm.(/[\x01-\xFF]*/)==false){
alert('n1 cannot enter Chinese!')
frm.();
return false;
}
}
</script>
<body>
<form onsubmit="return chkIt(this)">
<input name="n1">
<input name="n2">
<input name="smt" type="submit" value="submit">
</form>
</body>

1. Only numeric codes can be entered in the text box (the decimal point cannot be entered either)
<input onkeyup="=(/\D/g,'')" onafterpaste="=(/\D/g,'')">
2. You can only enter numbers and enter decimal points.
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test()){alert('Only enter numbers');='';}">
3. Number and decimal point method two
<input type=text t_value="" o_value="" onkeypress="if(!(/^[\+\-]?\d*?\.?\d*?$/))=this.t_value;else this.t_value=;if((/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=" onkeyup="if(!(/^[\+\-]?\d*?\.?\d*?$/))=this.t_value;else this.t_value=;if((/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=" onblur="if(!(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))=this.o_value;else{if((/^\.\d+$/))=0+;if((/^\.$/))=0;this.o_value=}">
4. Only enter letters and Chinese characters
<input onkeyup="value=(/[\d]/g,'') "onbeforepaste="('text',('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers">
5. Only enter English letters and numbers, not Chinese
<input onkeyup="value=(/[^\w\.\/]/ig,'')">
6. Only enter numbers and English <font color="Red">chun</font>
<input onKeyUp="value=(/[^\d|chun]/g,'')">
7. There can only be up to two digits after the decimal point (numbers and Chinese can be entered), and letters and operator symbols cannot be entered:
<input onKeyPress="if((<48 || >57) && !=46 || /\.\d\d$/.test(value))=false">
8. There can only be up to two digits after the decimal point (numbers, letters, and Chinese), and operator symbols can be entered:
<input onkeyup="=(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">

Special characters are prohibited:
onKeyPress="if( < 45 || > 57 ) = false;"
Only enter Chinese characters:
<input onkeyup="value=(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="('text',('text').replace(/[^/u4E00-/u9FA5]/g,''))">
style="ime-mode:disabled" prohibits Chinese character input method
Only enter numbers:
<input onkeyup="value=(/[^/d]/g,'') "onbeforepaste="('text',('text').replace(/[^/d]/g,''))">
Only enter English and numbers:
<input onkeyup="value=(/[/W]/g,'') "onbeforepaste="('text',('text').replace(/[^/d]/g,''))">
The control input box can only enter text or numbers, and special characters are not allowed to be entered.
The following characters are not allowed to be entered here: (like ^&*, etc.)<br>
<textarea rows=2 cols=20 name=comments onKeypress="if (( > 32 && < 48) || ( > 57 && < 65) || ( > 90 && < 97)) = false;">
Only space input is prohibited
onkeyup="value=(//s/g,'')"
onkeydown="if(==32) return false"
Only enter Chinese and English:
onkeyup="value=(/[^/a-zA-Z/u4E00-/u9FA5]/g,'')" onbeforepaste="('text',('text').replace(/[^/a-zA-Z/u4E00-/u9FA5]/g,''))"
Special characters and spaces are not allowed:
<input onkeypress="return ValidateSpecialCharacter();" onblur="validate(this)"/>

————————————————————————————————————————
Can't be empty
<input onblur="if((/^ +| +$/g,'')=='')alert('can't be empty!')">
Judgment characters consist of letters and numbers, underscores, and dots. The only thing that starts is underscores and letters.
/^([a-zA-z_]{1})([\w]*)$/(str)
Only enter numbers
<input name="text" type="text" onKeyUp="value=(/\D/g,'')" onafterpaste="value=(/\D/g,'')" >
Only enter Chinese
<input type="text" onkeyup="value=(/[^\u4E00-\u9FA5]/g,'')">
Only enter English
<input type="text" onkeyup="value=(/[^\a-\z\A-\Z]/g,'')">
<input type="text" onkeyup="value=(/[^a-zA-Z]/g,'')">
--------------------------------------------------------------------------------------------------------------------

Only enter Chinese, English, numbers, @ symbols and.
<input type="text" onkeyup="value=(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'')">
Only English is allowed, and the paste menu cannot be popped up.
<input type="text" onkeyup="value=(/[^\a-\z\A-\Z]/g,'')" onkeydown="fncKeyStop(event)" onpaste="return false" oncontextmenu = "return false"/>
Only enter numbers and dot numbers (note: d in [^\d\.] cannot be written in capital D, otherwise it will become all characters except numbers)
<input name="price" type="text" size="8" maxlength="8" onkeyup="value=(/[^\d\.]/g,'')" >
In short: first enter onkeyup="value=(/[^\X]/g,'')" in <input> and then replace the X in (/[\X]/g,'') with the code you want to enter.
Chinese: u4E00-u9FA5
Numbers: d, 0-9
English: a-z, A-Z
Other symbols @, dots or other symbols. You can also use multiple symbols, just separate them with \.
For example:
Chinese, English and numbers with @ symbols and dotted symbols: \a-\z\A-\Z0-9\u4E00-\u9FA5\@\.
If you want to not right-click the menu and cannot paste the copied information in the text box, you must enter onKeyDown="fncKeyStop(event)" onpaste="return false" oncontextmenu="return false;"
---------------------------------------------------------------------------------------------------------------------------------------

First, only numbers and decimal points are allowed to be entered.
Copy the codeThe code is as follows:

<input onKeypress="return (/[/d.]/.test(()))" style="ime-mode:Disabled">

Second, the judgment is more detailed, and even 22..2 can be judged without counting the numbers.
Copy the codeThe code is as follows:

<script>
function check(){
if (isNaN())
{alert("illegal character!");
="";}
}
</script>
<input type="text" name="tt" onkeyup="check();">

Third, only integers are allowed. In fact, we can also learn from one example and apply some restrictions according to the third article.
Copy the codeThe code is as follows:

<script language=javascript>
function onlyNum()
{
if(!(==46)&&!(==8)&&!(==37)&&!(==39))
if(!((>=48&&<=57)||(>=96&&<=105)))
=false;
}
</script><input onkeydown="onlyNum();" style="ime-mode:Disabled>

Conclusion, actually
style="ime-mode:Disabled
This sentence is more practical. Means to turn off the input method. It saves some people from entering numbers in full-width, but they can't enter them in the end, and they blame you for not designing them well.
Only numbers are allowed
<input name="username" type="text" onkeyup="value=(//D+/g,'')">
Only English letters, numbers and underscores are allowed (the following two methods are implemented)
<input name="username" type="text" style="ime-mode:disabled">
<input name="username" type="text" onkeyup="value=(/[^/w/.//]/ig,'')">
Only English letters, numbers and &=@ are allowed
<input name="username" type="text" onkeyup="value=(/[^/w=@&]|_/ig,'')">
Only Chinese characters are allowed
<input name="username" type="text" onkeyup="value=(/[^/u4E00-/u9FA5]/g,'')">
me" type="text" style="ime-mode:disabled">
<input name="username" type="text" onkeyup="value=(/[^/w/.//]/ig,'')">
Only English letters, numbers and &=@ are allowed
<input name="username" type="text" onkeyup="value=(/[^/w=@&]|_/ig,'')">
Only Chinese characters are allowed
<input name="username" type="text" onkeyup="value=(/[^/u4E00-/u9FA5]/g,'')">