Javascript regexp使用val()。match()方法 [英] Javascript regexp using val().match() method

查看:88
本文介绍了Javascript regexp使用val()。match()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下规则验证名为phone_number的字段:

I'm trying to validate a field named phone_number with this rules:

第一个数字应该是3然后是另外9个数字所以总共10个数字示例:3216549874

the first digit should be 3 then another 9 digits so in total 10 number example: 3216549874

或者可以是7个数字1234567

or can be 7 numbers 1234567

这里我有我的代码:

        if (!($("#" + val["htmlId"]).val().match(/^3\d{9}|\d{7}/)))
            missing = true;

为什么不工作:(当我把它放入在线正则表达式检查器显示良好。

Why doesnt work :( when i put that into an online regexp checker shows good.

推荐答案

你应该使用测试而不是匹配,这里是正确的代码:

You should be using test instead of match and here's the proper code:

.test(/^(3\d{9}|\d{7})$/)

匹配将查找所有事件,而测试只会检查是否有至少一个可用(从而验证您的号码)。

Match will find all the occurrences, while test will only check to see if at least one is available (thus validating your number).

这篇关于Javascript regexp使用val()。match()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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