检查是否仅在输入中输入了数值. (jQuery的) [英] Check If only numeric values were entered in input. (jQuery)

查看:94
本文介绍了检查是否仅在输入中输入了数值. (jQuery的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我想检查用户是否在jQuery的帮助下输入了正确的电话号码,

I would like to check if users enter correct phone number in, with help of jQuery, so far I got to this stage:

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;
}

基本上,它检查是否输入了电话号码,是否输入了电话号码,我想检查它是否为数字值,以及是否未显示错误消息. 有人可以帮助检查它是否为数字吗?

Basically it checks if phone number was entered and if it was, I would like to check if it is a numeric value and if it is not display the error messages. Could anyone help with checking if it is numeric?

推荐答案

尝试一下……它将确保字符串"phone"仅包含数字,并且至少包含一个数字

Try this ... it will make sure that the string "phone" only contains digits and will at least contain one digit

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

这篇关于检查是否仅在输入中输入了数值. (jQuery的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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