take off和put off:文本框不允许输入特殊字符,只能是数字、字母、-和

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 06:41:08
文本框不允许输入特殊字符,只能是数字、字母、-和_,不允许输入空格键

在文本框的keypress事件调用下面函数。
如 

如果在文本框中按下特殊字符键,则显示警告信息,并且屏蔽输入。

function TextValidate()
{
    var code;
    var character;
    var err_msg = "Text can not contain SPACES or any of these special characters  other than underscore (_) and hyphen (-).";
    if (document.all) //判断是否是IE浏览器
    {
        code = window.event.keyCode;
    }
    else
    {
        code = arguments.callee.caller.arguments[0].which;
    }
    var character = String.fromCharCode(code);
    
    var txt=new RegExp("[ ,\\`,\\~,\\!,\\@,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\.,\\<,\\>,\\{,\\},\\(,\\),\\',\\;,\\=,\"]");
    //特殊字符正则表达式
    if (txt.test(character))
    {
        alert("User Name can not contain SPACES or any of these special characters:\n  , ` ~ ! @ # $ % ^ + & * \\ / ? | : . < > {} () [] \" ");
        if (document.all)
        {
            window.event.returnValue = false;
        }
        else
        {
            arguments.callee.caller.arguments[0].preventDefault();
        }
    }
}  注释:唯一遗憾的是Ext.form.TextField,Ext.form.Number中没有onkeypress事件。费尽周折,最终还是通过,正则,加上Ext.form元素的regex和regexText实现了效果。

ExtJs NumberField或TextField不能处理KeyPress问题

Form控件的
NumberField(或TextField)不能获取键盘事件,可以试试以下方法。

    var barcode = new Ext.form.NumberField({
        allowDecimals: false,
        decimalPrecision: 0,
        allowNegative: false,
        selectOnFocus: true,
        allowBlank: true,
        applyTo: 'txtCard',
        emptyText: '- Scan -'
    });

    Ext.get(barcode.el).on('keyup', function(e) {
    //do something
  }