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();
            }
        }
    }
  • 相关阅读:
    00045_多态-转型
    js下载文件夹的解决方案
    b/s下载文件夹的解决方案
    web下载文件夹的解决方案
    jsp下载文件夹的解决方案
    php下载文件夹的解决方案
    asp.net下载文件夹的解决方案
    KindEditor 从word中复制内容带多张图片
    CKEditor 从word中复制内容带多张图片
    Web编辑器 从word中复制内容带多张图片
  • 原文地址:https://www.cnblogs.com/zerogo/p/2209270.html
Copyright © 2011-2022 走看看