正则表达式,以逗号和空格分隔的数字列表 [英] Regular expression to allow comma and space delimited number list

查看:486
本文介绍了正则表达式,以逗号和空格分隔的数字列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用javascript或jquery编写正则表达式以允许 以逗号分隔的数字列表或 以空格分隔的数字或 逗号后跟一个以空格分隔的数字或 以上任何一项的组合 任何非数字,空格或逗号的内容都必须被拒绝

I want to write a regular expression using javascript or jquery to allow comma delimited list of numbers OR space delimited numbers OR comma followed by a space delimited numbers OR a combination of any of the above ex anything that is not a digit, space or comma must be rejected

通行证 111,222,333 111222333 111、222、333 111,222,333 444 555 666、111、222、333,

SHOULD PASS 111,222,333 111 222 333 111, 222, 333 111,222,333 444 555 666, 111, 222, 333,

不应通过: 111,222,3a 3a 111222 3a等等等

should NOT pass: 111,222,3a 3a 111 222 3a etc etc

我尝试了下面的代码,但是当我键入代码时,它们似乎可以正常工作 3a作为一个数字,它通过了!!!如何?我不明白我的代码如何允许那封信通过.

I tried the code below they seemed to work however when I typed 3a as a number, it PASSED!!! How? I cannot understand how my code allowed that letter to pass.

我要拒绝任何非空格,逗号或数字的内容

I want to reject anything that is not a space, comma or digit

或者有没有正则表达式的更好方法吗? 我在google中查看,没有找到任何答案.

or is there a better way to do this without regular expressions? I looked in google and did not find any answer.

在此先感谢您的帮助.

var isNumeric = /[\d]+([\s]?[,]?[\d])*/.test(userInput);
var isNumeric = /^[\d\s,]*/.test(userInput);    
var isNumeric = /^[\d]*[\s,]*/.test(userInput); 
var isNumeric = /^[\d\s,]*/.test(userInput);    
var isNumeric = /\d+\s*,*/.test(userInput); 

if (isNumeric == false) {
    alert(isNumeric);
  return false;
}
else
      alert('is Numeric!!!');

推荐答案

正则表达式是否会 ^[\d,\s]+$ 不把戏吗?

Would the regular expression ^[\d,\s]+$ not do the trick?

这篇关于正则表达式,以逗号和空格分隔的数字列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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