检查输入中是否只输入了数值.(jQuery)

Check If only numeric values were entered in input. (jQuery)

本文关键字:输入 jQuery 是否 检查      更新时间:2023-09-26

我想在jQuery的帮助下检查用户是否在中输入了正确的电话号码,到目前为止我已经进入了这个阶段:

var phone = $("input#phone").val();
if (phone !== "") {
    //Check if phone is numeric
    $("label#phone_error").show(); //Show error
    $("input#phone").focus(); //Focus on field
    return false;
}

基本上,它会检查是否输入了电话号码,如果是,我想检查它是否是一个数字值,是否没有显示错误消息。有人能帮我检查一下它是否是数字吗?

试试这个。。。它将确保字符串"phone"只包含数字,并且至少包含一个数字

if(phone.match(/^'d+$/)) {
    // your code here
}

jQuery中有一个内置函数可以检查这一点(isNumeric),所以请尝试以下操作:

var phone = $("input#phone").val();
    if (phone !== "" && !$.isNumeric(phone)) {
  //Check if phone is numeric
  $("label#phone_error").show(); //Show error
  $("input#phone").focus(); //Focus on field
  return false;
}

您可以使用jQuery方法来检查一个值是数值还是其他类型。

$.isNumeric()

示例

$.isNumeric("46")

真实

$.isNumeric(46)

真实

$.isNumeric("dfd")

错误

我用它来检查所有文本框是否都有数值:

if(!$.isNumeric($('input:text').val())) {
        alert("All the text boxes must have numeric values!");
        return false;
    }

或者其中之一:

$.isNumeric($("txtBox").val());

可与jQuery 1.7一起使用。

http://docs.jquery.com/Plugins/Validation/CustomMethods/phoneUS

看看这个。它应该正是你想要的。jQuery的美国手机验证插件。

如果你想自己做,你需要做大量的工作。检查isNaN()功能。它告诉你它是否不是一个数字。您还需要复习正则表达式以进行验证。如果您使用的是RegEx,您可以不使用isNaN(),因为您无论如何都要为此进行测试。

我使用了这个:

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/'s+/g, ""); 
return this.optional(element) || phone_number.length > 9 &&
    phone_number.match(/^(1-?)?('([2-9]'d{2}')|[2-9]'d{2})-?[2-9]'d{2}-?'d{4}$/);
}, "Please specify a valid phone number");
 if (!(/^[-+]?'d*'.?'d*$/.test(document.getElementById('txtRemittanceNumber').value))){
            alert('Please enter only numbers into amount textbox.')
            }
            else
            {
            alert('Right Number');
            }

我希望这个代码可以帮助你。

在这个代码中,如果有任何法定小数位数的小数位数,if条件将返回true。警报将显示消息"正确的数字",另一方面,它将显示一个弹出的警报,消息"请只在金额文本框中输入数字"。

谢谢…:)

对于未来的访问者,您可以添加这个允许用户只输入数字的函数:您只需要将jquery和类名添加到输入中即可http://jsfiddle.net/celia/dvnL9has/2/

$('.phone_number').keypress(function(event){
var numero= String.fromCharCode(event.keyCode);
 var myArray = ['0','1','2','3','4','5','6','7','8','9',0,1,2,3,4,5,6,7,8,9];
index = myArray.indexOf(numero);// 1
var longeur= $('.phone_number').val().length;
if(window.getSelection){
 text = window.getSelection().toString();
 } if(index>=0&text.length>0){
  }else if(index>=0&longeur<10){
    }else {return false;} });

我使用了这种验证。。。。检查粘贴的文本,如果其中包含字母,则向用户显示错误,然后在延迟后清除该框,以便用户检查文本并进行适当的更改。

$('#txtbox').on('paste', function (e) {
            var $this = $(this);
            setTimeout(function (e) {
                if (($this.val()).match(/[^0-9]/g))
                {
                    $("#errormsg").html("Only Numerical Characters allowed").show().delay(2500).fadeOut("slow");                       
                    setTimeout(function (e) {
                        $this.val(null);
                    },2500);
                }                   
            }, 5);
        });

这不是这个问题的确切答案,但电话验证的另一个选项是确保以您期望的格式输入号码。

这是我处理过的一个函数,当设置为onInput事件时,它将去掉任何非数字输入,并在"右"点自动插入短划线,假设xxx xxx xxxx是所需的输出。

<input oninput="formatPhone()">

function formatPhone(e) {
    var x = e.target.value.replace(/'D/g, '').match(/('d{0,3})('d{0,3})('d{0,4})/);
    e.target.value = !x[2] ? x[1] : x[1] + '-' + x[2] + (x[3] ? '-' + x[3] : '');
}