如何使输入警报消息仅在输入字母时才起作用,而不是用于数字?不让任何文字发出 [英] How do I get an input alert message to work only when imputing alphabets, not for numbers? Without letting any text tipped in

查看:239
本文介绍了如何使输入警报消息仅在输入字母时才起作用,而不是用于数字?不让任何文字发出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在输入字母时输入提示信息,而不是输入数字?不要让任何文字倾斜。
当输入数字时,应该没有警报,但是当输入字母时,您会收到警报。我也接受JavaScript标记的解决方案,但是如果有内联解决方案,那就更好了。



这个函数的问题在于它如果你是快速翻车手,就让我们在文本框中输入文字。它应该是,不要让任何文本发送



JavaScript:

 < script language =JavaScript> 
函数checkInp()
{
var x = document.forms [isnform04] [areinp] .value;
var regex = / ^ [0-9] + $ /;
if(!x.match(regex))
{
alert(这里没有文本,仅输入数字);
返回false;
}
}
< / script>

HTML:
onKeyup =return checkInp();

 < input type =textname =areinpsize =30value = > 

没有查询解决方案plz。 $ b $

< input type = textname =areinpsize =30value =onChange =areCon()onkeyup =if(this.value = this.value.replace(/ [^ \d] /,'' ))alert('这里没有文字,只有输入数字');> 


解决方案

您需要使用 preventDefault ()如果你想取消事件,那就是禁止文本框接受非数字输入。这样你就不必费心去删除它了。但是, preventDefault()不适用于 onkeyup $ b

JS $ b

 函数checkKey(e)
{
if(e.keyCode!= 8&& // //允许退格
e。 keyCode!= 46&& //允许删除
e.keyCode!= 37&& // //允许左箭头
e.keyCode!= 39&&& // //允许右箭头
(e.keyCode< 48 || e.keyCode> 57))//只允许数字
{
e.preventDefault();


HTML p>

 < input type =textonkeydown =checkKey(event)/> 

注意 - 在每个按键上按下/向下/向上提醒用户是可怕的。简单的烦人。避免!如上所示,静静地屏蔽不需要的密钥。


How do I get an input alert message to work only when imputing alphabets, not for numbers? Without letting any text tipped in. When imputing numbers there should be no alerts, but when imputing alphabets you get an alert. I accept a JavaScript tagged solution too, but if there is an inline solution then that is preferred.

The problem with this function is that it lets the text you type in the box if you are fast tipper as I am. It should be without letting any text tipped in.

JavaScript:

<script language="JavaScript">
function checkInp()
{
    var x = document.forms["isnform04"]["areinp"].value;
    var regex = /^[0-9]+$/;
    if (!x.match(regex))
    {
        alert("no text here, input numbers only");
        return false;
    }
}
</script>

HTML:

<input type="text" name="areinp" size="30" value="" onChange="areCon()" onkeyup="return checkInp();">

No query solution plz.

UPDATE: I also used this one:

<input type="text" name="areinp" size="30" value="" onChange="areCon()" onkeyup="if (this.value=this.value.replace(/[^\d]/,'')) alert('no text here, input numbers only');">

解决方案

You need to use preventDefault() if you want to to cancel the event, that is to disallow text box from accepting non-numeric input. So that you don't have to bother about deleting it. However, preventDefault() does not work with onkeyup. Use onkeydown.

JS:

function checkKey(e) 
{
    if (e.keyCode != 8 && // allow backspace
        e.keyCode != 46 && // allow delete
        e.keyCode != 37 && // allow left arrow
        e.keyCode != 39 && // allow right arrow
        (e.keyCode < 48 || e.keyCode > 57)) // allow numerics only
    {
        e.preventDefault();
    }
}

HTML:

<input type="text" onkeydown="checkKey(event)" />

Note - Alerting user on each key press / down / up is terrible. Simply annoying. Avoid! Silently block the unwanted keys as I showed above.

这篇关于如何使输入警报消息仅在输入字母时才起作用,而不是用于数字?不让任何文字发出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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