限制文本框输入

本文介绍了如何使用JavaScript实现对输入的限制与验证,包括字符类型、长度、正则表达式等,涵盖了小写英文、大写英文、任意数字、限2位小数、日期、任意中文、部分英文和部分中文等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#TextBox1").keypress(function (e) {
                alert(event.keyCode);
                if (((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 46) ||  //只能输入字母和“.”
                     (/\.\d$/.test($("#TextBox1").val())) ||                               //只能输入一位小数
                     (event.keyCode == 46 && ($("#TextBox1").val().length == 0 || $("#TextBox1").val().indexOf(".") != -1)) //当输入“.”是不能是第一位而且在输入框中不能含有“.”
                     ) {
                    return false;
                }


            })
        })
    </script>

<script>
None.gif    
function regInput(obj, reg, inputStr)
None.gif    {
None.gif        
var docSel    = document.selection.createRange()
None.gif        
if (docSel.parentElement().tagName != "INPUT")    return false
None.gif        oSel 
= docSel.duplicate()
None.gif        oSel.text 
= ""
None.gif        
var srcRange    = obj.createTextRange()
None.gif        oSel.setEndPoint(
"StartToStart", srcRange)
None.gif        
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
None.gif        
return reg.test(str)
None.gif    }
None.gif
</script>
None.gif
None.gif小写英文:
<xmp style= "display:inline">    </xmp>
None.gif
<input    onkeypress    = "return regInput(this,    /^[a-z]*$/,        String.fromCharCode(event.keyCode))"
None.gif        onpaste        
= "return regInput(this,    /^[a-z]*$/,        window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^[a-z]*$/,        event.dataTransfer.getData('Text'))"
None.gif        style
="ime-mode:Disabled"
None.gif
><br>
None.gif
None.gif大写英文:
<xmp style= "display:inline">    </xmp>
None.gif
<input    onkeypress    = "return regInput(this,    /^[A-Z]*$/,        String.fromCharCode(event.keyCode))"
None.gif        onpaste        
= "return regInput(this,    /^[A-Z]*$/,        window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^[A-Z]*$/,        event.dataTransfer.getData('Text'))"
None.gif        style
="ime-mode:Disabled">
None.gif
<br>
None.gif
None.gif任意数字:
<xmp style="display:inline">    </xmp>
None.gif
<input    onkeypress    = "return regInput(this,    /^[0-9]*$/,        String.fromCharCode(event.keyCode))"
None.gif        onpaste        
= "return regInput(this,    /^[0-9]*$/,        window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^[0-9]*$/,        event.dataTransfer.getData('Text'))"
None.gif        style
="ime-mode:Disabled"
None.gif
><br>
None.gif
None.gif限2位小数:
<xmp style="display:inline">    </xmp>
None.gif
<input    onkeypress    = "return regInput(this,    /^\d*\.?\d{0,2}$/,        String.fromCharCode(event.keyCode))"
None.gif        onpaste        
= "return regInput(this,    /^\d*\.?\d{0,2}$/,        window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^\d*\.?\d{0,2}$/,        event.dataTransfer.getData('Text'))"
None.gif        style
="ime-mode:Disabled"
None.gif
> 如: 123.12<br>
None.gif
None.gif
None.gif日   期:
<xmp style="display:inline">    </xmp>
None.gif
<input    onkeypress    = "return regInput(this,    /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/,        String.fromCharCode(event.keyCode))"
None.gif        onpaste        
= "return regInput(this,    /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/,        window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^\d{1,4}([-\/](\d{1,2}([-\/](\d{1,2})?)?)?)?$/,        event.dataTransfer.getData('Text'))"
None.gif        style
="ime-mode:Disabled"
None.gif
> 如: 2002-9-29<br>
None.gif
None.gif任意中文:
<xmp style="display:inline">    </xmp>
None.gif
<input    onkeypress    = "return regInput(this,    /^$/,                    String.fromCharCode(event.keyCode))"
None.gif        onpaste        
= "return regInput(this,    /^[\u4E00-\u9FA5]*$/,    window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^[\u4E00-\u9FA5]*$/,    event.dataTransfer.getData('Text'))"
None.gif
><br>
None.gif
None.gif部分英文:
<xmp style="display:inline">    </xmp>
None.gif
<input    onkeypress    = "return regInput(this,    /^[a-e]*$/,        String.fromCharCode(event.keyCode))"
None.gif        onpaste        
= "return regInput(this,    /^[a-e]*$/,        window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^[a-e]*$/,        event.dataTransfer.getData('Text'))"
None.gif        style
="ime-mode:Disabled"
None.gif
> 范围: a,b,c,d,e<br>
None.gif
None.gif部分中文:
<xmp style="display:inline">    </xmp>
None.gif
None.gif
<script language=javascript>
None.gif
function checkChinese(oldLength, obj)
None.gif{
None.gif    
var oTR = window.document.selection.createRange()
None.gif    
var reg = /[^一二三四五六七八九十]/g
None.gif    oTR.moveStart(
"character"-1*(obj.value.length-oldLength))
None.gif    oTR.text 
= oTR.text.replace(reg, "")
None.gif}
None.gif
</script>
None.gif
<input    onkeypress="return false" onkeydown="setTimeout('checkChinese('+this.value.length+','+this.uniqueID+')', 1)"
None.gif        onpaste        
= "return regInput(this,    /^[一二三四五六七八九十]*$/,        window.clipboardData.getData('Text'))"
None.gif        ondrop        
= "return regInput(this,    /^[一二三四五六七八九十]*$/,        event.dataTransfer.getData('Text'))"
None.gif
> 范围: 一二三四五六七八九十<br>

转载于:https://www.cnblogs.com/gf345725726/archive/2011/10/14/2212242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值