zoukankan      html  css  js  c++  java
  • 文本框不允许输入特殊字符,只能是数字、字母、-和_,不允许输入空格键

    在文本框的keypress事件调用下面函数。
    如 <input disabled="disabled" type="text" id='userNameToEdit' onkeypress="TextValidate()" />

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

    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();
            }
        }
    }
  • 相关阅读:
    使用Astah画UML类图经验总结
    Qt的四个常见的图像叠加模式
    获取Linux时间函数
    DBus学习网站
    线程属性pthread_attr_t简介
    Secure CRT 自动记录日志log配置
    MySQL的group_concat()函数合并多行数据
    MySQL的Limit详解
    异步查询json传日期格式到前台,变成了时间戳的格式
    启动studio报错Gradle error
  • 原文地址:https://www.cnblogs.com/zerogo/p/2209270.html
Copyright © 2011-2022 走看看