RegEx用于JavaScript验证逗号分隔的数字 [英] RegEx for JavaScript validation of comma separated numbers
本文介绍了RegEx用于JavaScript验证逗号分隔的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框,它可以有1或1,2或1,225,345,21之类的值,即多个值。但现在我想验证这个输入。
I have one text box, it can have values like 1 or 1,2 or 1,225,345,21 ie multiple values. But now I want to validate this input.
toString().match(/^(([0-9](,)?)*)+$/)
这是我正在使用的代码。它只是验证是正确的,但是当用户输入如下值时会出现一个问题:
This is code I'm using. It is validating correct only, but one problem when user enter values like this:
inputval:1,22,34,25,645(true)
inputval:1,22,34,25,645,(falues)
当用户输入逗号(,)作为最后一次它应该抛出错误。
When the user enters comma (,) as last it should throw an error.
任何人都可以帮助我吗?
Can any one help me please?
推荐答案
只需手动包含至少一个:
Just manually include at least one:
/^[0-9]+(,[0-9]+)*$/
let regex = /[0-9]+(,[0-9]+)*/g
console.log('1231232,12323123,122323',regex.test('1231232,12323123,122323'));
console.log('1,22,34,25,645,',regex.test('1,22,34,25,645,'));
console.log('1',regex.test('1'));
这篇关于RegEx用于JavaScript验证逗号分隔的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文