如何防止在输入时使用空格? [英] how to prevent using space while typing?

查看:141
本文介绍了如何防止在输入时使用空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字段可输入一些序列号代码.如果有人使用spase,我想将此代码设置为显示警报.这意味着不允许使用空格,而只允许使用减号来分隔此代码.您有解决此问题的想法吗?我可以使用jQuery验证吗?

i have one textfield for input some serial number code.i want set this code show alert if someone use spase. it means space is not allowed and just allowed use minus for separate this code. Are you have any idea for resolve this problem? can i use jquery validate?

the correct typing:
135x0001-135x0100

推荐答案

为防止输入元素中出现空格,您可以使用jQuery:

To prevent a space in your input element, you could do this using jQuery:

示例: http://jsfiddle.net/AQxhT/

​$('input').keypress(function( e ) {
    if(e.which === 32) 
        return false;
})​​​​​;​

.

$('input').keypress(function( e ) {    
    if(!/[0-9a-zA-Z-]/.test(String.fromCharCode(e.which)))
        return false;
});​

这篇关于如何防止在输入时使用空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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