1
<script>
2
function regInput(obj, reg, inputStr)
3
{
4
var docSel = document.selection.createRange()
5
if (docSel.parentElement().tagName != "INPUT") return false
6
oSel = docSel.duplicate()
7
oSel.text = ""
8
var srcRange = obj.createTextRange()
9
oSel.setEndPoint("StartToStart", srcRange)
10
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
11
return reg.test(str)
12
}
13
</script>
14
15
小写英文:<xmp style= "display:inline"> </xmp>
16
<input onkeypress = "return regInput(this, /^[a-z]*$/, String.fromCharCode(event.keyCode))"
17
onpaste = "return regInput(this, /^[a-z]*$/, window.clipboardData.getData(\'Text\'))"
18
ondrop = "return regInput(this, /^[a-z]*$/, event.dataTransfer.getData(\'Text\'))"
19
style="ime-mode:Disabled"
20
><br>
21
22
大写英文:<xmp style= "display:inline"> </xmp>
23
<input onkeypress = "return regInput(this, /^[A-Z]*$/, String.fromCharCode(event.keyCode))"
24
onpaste = "return regInput(this, /^[A-Z]*$/, window.clipboardData.getData(\'Text\'))"
25
ondrop = "return regInput(this, /^[A-Z]*$/, event.dataTransfer.getData(\'Text\'))"
26
style="ime-mode:Disabled">
27
<br>
28
29
任意数字:<xmp style="display:inline"> </xmp>
30
<input onkeypress = "return regInput(this, /^[0-9]*$/, String.fromCharCode(event.keyCode))"
31
onpaste = "return regInput(this, /^[0-9]*$/, window.clipboardData.getData(\'Text\'))"
32
ondrop = "return regInput(this, /^[0-9]*$/, event.dataTransfer.getData(\'Text\'))"
33
style="ime-mode:Disabled"
34
><br>
35
36
限2位小数:<xmp style="display:inline"> </xmp>
37
<input onkeypress = "return regInput(this, /^\d*\.?\d{0,2}$/, String.fromCharCode(event.keyCode))"
38
onpaste = "return regInput(this, /^\d*\.?\d{0,2}$/, window.clipboardData.getData(\'Text\'))"
39
ondrop = "return regInput(this, /^\d*\.?\d{0,2}$/, event.dataTransfer.getData(\'Text\'))"
40
style="ime-mode:Disabled"
41
> 如: 123.12<br>
42
43
44
日 期:<xmp style="display:inline"> </xmp>
45
<input onkeypress = "return regInput(this, /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/, String.fromCharCode(event.keyCode))"
46
onpaste = "return regInput(this, /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/, window.clipboardData.getData(\'Text\'))"
47
ondrop = "return regInput(this, /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/, event.dataTransfer.getData(\'Text\'))"
48
style="ime-mode:Disabled"
49
> 如: 2002-9-29<br>
50
51
任意中文:<xmp style="display:inline"> </xmp>
52
<input onkeypress = "return regInput(this, /^$/, String.fromCharCode(event.keyCode))"
53
onpaste = "return regInput(this, /^[\u4E00-\u9FA5]*$/, window.clipboardData.getData(\'Text\'))"
54
ondrop = "return regInput(this, /^[\u4E00-\u9FA5]*$/, event.dataTransfer.getData(\'Text\'))"
55
><br>
56
57
部分英文:<xmp style="display:inline"> </xmp>
58
<input onkeypress = "return regInput(this, /^[a-e]*$/, String.fromCharCode(event.keyCode))"
59
onpaste = "return regInput(this, /^[a-e]*$/, window.clipboardData.getData(\'Text\'))"
60
ondrop = "return regInput(this, /^[a-e]*$/, event.dataTransfer.getData(\'Text\'))"
61
style="ime-mode:Disabled"
62
> 范围: a,b,c,d,e<br>
63
64
部分中文:<xmp style="display:inline"> </xmp>
65
66
<script language=javascript>
67
function checkChinese(oldLength, obj)
68
{
69
var oTR = window.document.selection.createRange()
70
var reg = /[^一二三四五六七八九十]/g
71
oTR.moveStart("character", -1*(obj.value.length-oldLength))
72
oTR.text = oTR.text.replace(reg, "")
73
}
74
</script>
75
<input onkeypress="return false" onkeydown="setTimeout(\'checkChinese(\'+this.value.length+\',\'+this.uniqueID+\')\', 1)"
76
onpaste = "return regInput(this, /^[一二三四五六七八九十]*$/, window.clipboardData.getData(\'Text\'))"
77
ondrop = "return regInput(this, /^[一二三四五六七八九十]*$/, event.dataTransfer.getData(\'Text\'))"
78
> 范围: 一二三四五六七八九十<br>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78