如何防止打字时使用空格?

发布于 2024-09-16 23:26:28 字数 170 浏览 3 评论 0原文

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

the correct typing:
135x0001-135x0100

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼睛会笑 2024-09-23 23:26:28

为了防止输入元素中出现空格,您可以使用 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;
});​

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

Example: 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;
});​
红ご颜醉 2024-09-23 23:26:28

简短而甜蜜,不依赖 jQuery

function nospaces(t){
  if(t.value.match(/\s/g)){
    t.value=t.value.replace(/\s/g,'');
  }
}

HTML

<input type="text" name ="textbox" id="textbox" onkeyup="nospaces(this)">

Short and sweet NOT jQuery dependent

function nospaces(t){
  if(t.value.match(/\s/g)){
    t.value=t.value.replace(/\s/g,'');
  }
}

The HTML

<input type="text" name ="textbox" id="textbox" onkeyup="nospaces(this)">
任性一次 2024-09-23 23:26:28
$('.noSpace').keyup(function() {
 this.value = this.value.replace(/\s/g,'');
});

<input type="text" name ="textbox" class="noSpace"  />
$('.noSpace').keyup(function() {
 this.value = this.value.replace(/\s/g,'');
});

<input type="text" name ="textbox" class="noSpace"  />
私野 2024-09-23 23:26:28

您可以在 onkeypress 事件中使用此 js 函数。这是js函数。

function AvoidSpace() {
            if (event.keyCode == 32) {
                event.returnValue = false;
                return false;
            }
        } 

这是输入元素(文本框)

   <asp:TextBox ID="textbox" onkeypress="return AvoidSpace()" runat="server"></asp:TextBox>

you can use this js function in an onkeypress event. Here is the js function.

function AvoidSpace() {
            if (event.keyCode == 32) {
                event.returnValue = false;
                return false;
            }
        } 

here is the input element (textbox)

   <asp:TextBox ID="textbox" onkeypress="return AvoidSpace()" runat="server"></asp:TextBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文