On Key Down 限制用户输入一些特殊字符 [英] On Key Down Restrict the user to enter Some Special Characters

查看:32
本文介绍了On Key Down 限制用户输入一些特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制用户在工具栏搜索中,不允许他/她使用一些特殊字符,例如 ('/','>','<','|').请帮帮我.

I want to restrict the user in toolbar search by not allowing him/her using Some Special Characters like ('/','>','<','|').Please help me out.

$("#tblFundComp").bind("keydown",function(e) 
{
  if(e.keyCode >=48 && e.keyCode <=57 ) 
  { 
    return false; 
  }
  else 
  { 
    return true; 
  }
}); 

我在搜索功能之前放置了这段代码.但这不起作用

I have placed this piece of code after before search function. But this does not work

推荐答案

如果你希望只允许在搜索工具栏的输入字段中输入一些特殊字符,你可以使用 dataEventssearchoptions 使用 type:'keypress' 定义code> 或 type:'keydown'.随后会为相应的输入字段调用 jQuery.bindjQuery.unbind.只允许数字的代码片段如下

If you want allow only some special characters are entered in the input field of the search toolbar you can use dataEvents of the searchoptions defined using type:'keypress' or type:'keydown'. It will follows to call jQuery.bind and jQuery.unbind for the corresponding input field. The code fragment which allows only digits is following

searchoptions: {
    dataEvents: [
        {
            type: 'keypress', // keydown
            fn: function(e) {
                // console.log('keypress');
                if(e.keyCode >=48 && e.keyCode <=57) {
                    // allow digits
                    return true;
                } else {
                    // disallow the key
                    return false;
                }
            }
        }
    ]
}

在现场 demo 你将不能在姓名"的搜索字段中输入数字.

这篇关于On Key Down 限制用户输入一些特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆