[英]comma and space separated number and hyphen separated string regular expression
[英]Regular expression to allow comma and space delimited number list
我想使用javascript或jquery編寫正則表達式以允許逗號分隔的數字列表或空格分隔數字或逗號后跟空格分隔數字或任何上述任何不是數字,空格或逗號的任何組合必須被拒絕
應該通過111,222,333 111 222 333 111,222,333 111,222,333 444 555 666,111,222,333,
不應通過:111,222,3a 3a 111 222 3a等
我嘗試下面的代碼他們似乎工作,但當我輸入3a作為數字,它通過! 怎么樣? 我無法理解我的代碼如何允許該字母通過。
我想拒絕任何不是空格,逗號或數字的東西
或者沒有正則表達式有更好的方法嗎? 我看了谷歌,沒有找到任何答案。
預先感謝您的任何幫助。
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]+$
不會這樣做嗎?
試試這個Regex ... 點擊查看你的演示
^[0-9 _ ,]*$
猜猜^(\\d+[, ]+)*$
應該這樣做。
說明:包含一個或多個數字,后跟至少一個空格或逗號的組。 小組可以重復任何次數。
它不會處理所有事情,比如在沒有數字的逗號(如果這是你想要的)之間失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.