jQuery电话号码验证允许空格,至少8位数字 [英] jQuery phone number validation allow spaces, minimum of 8 digits

查看:136
本文介绍了jQuery电话号码验证允许空格,至少8位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数字验证中允许空格(至少8位数字)? 在允许使用空格的情况下,输入电话号码更加容易.例如0400 123 456、9699 1234.

How can I allow spaces within an a numbers only validation, with a minimum of 8 digits? Phone numbers are much easier to type in when spaces are allowed. e.g. 0400 123 456, 9699 1234.

到目前为止,这是我的代码,我仅能使用最少8位数字进行验证:

Here's my code so far, I've only got the minimum 8 digits validation working:

jQuery.validator.addMethod(
  "phone",
  function(phone_number, element) {
    return this.optional(element) || /^\d{8}$/.test(phone_number);
  },
    "Please enter numbers only"
);

推荐答案

在验证之前删除空间:

return this.optional(element) || /^\d{8,}$/.test(phone_number.replace(/\s/g, ''));

这样您就可以保留空间

这篇关于jQuery电话号码验证允许空格,至少8位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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