Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何防止用户在文本框中插入字符,而不是只有数字才能插入jQuery Mobile_Jquery_Html_Jquery Mobile_Cordova - Fatal编程技术网

如何防止用户在文本框中插入字符,而不是只有数字才能插入jQuery Mobile

如何防止用户在文本框中插入字符,而不是只有数字才能插入jQuery Mobile,jquery,html,jquery-mobile,cordova,Jquery,Html,Jquery Mobile,Cordova,我在jQuery mobile中实现了iphone和android应用程序。*我使用了**纯jQuery mobile 在文本框中输入电话号码。我使用TYPE=“TEL”'这是用于numaric键盘的 <input type="tel" style=" width:81%;" id="contactNumber" value="" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset" /> 但是用户也可以在

我在jQuery mobile中实现了iphone和android应用程序。*我使用了**纯jQuery mobile

在文本框中输入电话号码。我使用TYPE=“TEL”'这是用于numaric键盘的

<input type="tel" style=" width:81%;" id="contactNumber" value="" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset" /> 

但是用户也可以在文本框中输入文本,这是不必要的。

如何防止用户插入数字以外的字符?

对于移动设备用户来说,如果唯一可能的输入是数字,那会舒服得多,因为他们必须按下每个按钮才能得到一个数字而不是一个字母


我试着在输入字段中添加
TYPE=“TEL”TYPE=“mumber”
,但它无法阻止。

以前看到过答案。退房

您只需更改类id,并在页面上显示以下代码:

$(document).ready(function(){
    $(".numeric").numeric();
});
如果不想使用jQuery,可以编写自己的函数并将其调用onkeyup事件

见:

另一个选项是使用步骤属性:


这将在输入时用空字符*替换任何非数字,从而消除一次搜索多个非数字的需要。

type=“tel”是HTML 5,而不是jqueryMobile。 浏览器支持HTML5输入类型的较新设备可以很好地呈现这些框。但是对于较旧的版本,需要有额外的脚本。上述答案中已经张贴了纸条。

$('contactNumber').bind('keypress',函数(事件){
$('#contactNumber').bind('keypress', function (event) {
        event = event || window.event; canAdd = new Boolean(false);
        canAdd = ((event.charCode > 47) && (event.charCode < 58) || (event.charCode == 32) || (event.charCode == 40) || (event.charCode == 41) || (event.charCode == 43) || (event.charCode == 45) || (event.charCode == 124));
        if (!canAdd && event.charCode)
            return false;
    }); 
event=event | | window.event;canAdd=new Boolean(false); canAdd=((event.charCode>47)和&(event.charCode<58)| |(event.charCode==32)| |(event.charCode==40)| |(event.charCode==41)| |(event.charCode==43)| |(event.charCode==45)| |(event.charCode==124)); if(!canAdd&&event.charCode) 返回false; });
但是最好使用HTML5 tel attrubute

以上这些对我来说都不管用,但我找到了一个完全适合我需要的解决方案(不允许在输入字段中使用特殊字符)。防止和提醒用户

    <script>
     $("#userlogin").keypress(function(e) {
      if (String.fromCharCode(e.which).match(/[^A-Za-z0-9_ ]/)) {
        e.preventDefault();
        alert("Special characters are not allowed. Use 'A-Z', 'a-z' and '0-9'.");
       }
     });
    </script>

$(“#userlogin”).keypress(函数(e){
if(String.fromCharCode(e.which).match(/[^A-Za-z0-9\]/){
e、 预防默认值();
警报(“不允许使用特殊字符。请使用“A-Z”、“A-Z”和“0-9”);
}
});

HTML

 input type="tel" (keypress)="isNumberKey($event)" 
isNumberKey(evt: any) { < br >
        // to accept only numbers in contact field<br>
        if ( < br >
            (evt.key >= '0' && evt.key <= '9') || < br >
            evt.key == "Backspace" || < br >
            evt.key == "Delete" || < br >
            evt.key == "ArrowLeft" || < br >
            evt.key == "ArrowRight" < br >
        ) { < br > //"Backspace" etc for 'firefox' browser<br>
                return true; < br >
        } < br >
        return false; < br >
}
脚本

 input type="tel" (keypress)="isNumberKey($event)" 
isNumberKey(evt: any) { < br >
        // to accept only numbers in contact field<br>
        if ( < br >
            (evt.key >= '0' && evt.key <= '9') || < br >
            evt.key == "Backspace" || < br >
            evt.key == "Delete" || < br >
            evt.key == "ArrowLeft" || < br >
            evt.key == "ArrowRight" < br >
        ) { < br > //"Backspace" etc for 'firefox' browser<br>
                return true; < br >
        } < br >
        return false; < br >
}
isNumberKey(evt:any){
//仅接受联系人字段中的号码
如果(
(evt.key>=“0”和&evt.key evt.key==“退格”| |
evt.key==“删除”| |
evt.key==“箭头左”| |
evt.key==“箭头右键”
){
/“firefox”浏览器的“Backspace”等
返回true;
}
返回false;
}
可能重复的$(document).ready(function(){$('#contactNumber').keyup(function(){var numbers=$(this.val();$(this.val.));val(numbers.replace(/\D/,''););});在我写了这篇文章之后,我意识到这不是在更新值。只是$(this.val()中的rap numbers.replace()这应该很好。我修改了答案。我意识到最初这不是更新#contactNumber的值。现在是了。我以前没有使用过JQuery mobile,所以我不知道它是否会以通常的方式响应.keyup。你可以把它作为一个快速测试:$('#contactNumber').keyup(function(){alert(“works”)});你能在上面加上一个负整数检查吗。@DaleI只得到了它并做了那件事。$(this).val(numbers.replace(/\D+/,'');有效。