admin管理员组文章数量:1277887
I have checked the below link for regular expression
Regex to match 2 digits, optional decimal, two digits
regular expression should accept whole number or decimal points. max length should be 10 numbers before decimal and 4 digits after decimal.
tried this below code from above link (2 digit decimal point)
\d{0,2}(\.\d{1,2})?
var patt =new RegExp("\d{0,2}(\.\d{1,2})?")
undefined
patt.test(1)
true
patt.test(1.1)
true
patt.test(11)
true
patt.test(111.111)
true
for 3 digits after decimal also it is giving true value, which is invalid.
I have checked the below link for regular expression
Regex to match 2 digits, optional decimal, two digits
regular expression should accept whole number or decimal points. max length should be 10 numbers before decimal and 4 digits after decimal.
tried this below code from above link (2 digit decimal point)
\d{0,2}(\.\d{1,2})?
var patt =new RegExp("\d{0,2}(\.\d{1,2})?")
undefined
patt.test(1)
true
patt.test(1.1)
true
patt.test(11)
true
patt.test(111.111)
true
for 3 digits after decimal also it is giving true value, which is invalid.
Share Improve this question edited May 23, 2017 at 12:15 CommunityBot 11 silver badge asked Aug 25, 2016 at 6:46 SivaRajiniSivaRajini 7,37522 gold badges84 silver badges129 bronze badges 4-
1
Your regex shorthand classes should be escaped with double backslashes and the current
/d{0,2}(.d{1,2})?/
expression matches an empty string, so the result is always true. – Wiktor Stribiżew Commented Aug 25, 2016 at 6:50 - @Wiktor It might be a duplicate but not of that question. See my answer. – jedifans Commented Aug 25, 2016 at 6:52
- @jedifans: I agree I was too quick, but your answer does not seem to answer the question correctly. – Wiktor Stribiżew Commented Aug 25, 2016 at 6:54
- I have updated to include both a fix for the given code and the requirement for 10 digits, etc. – jedifans Commented Aug 25, 2016 at 7:00
4 Answers
Reset to default 9You have to use delimiters, to determine where your match starts and where it ends. And you say max length should be 10 numbers before decimal and 4 digits after decimal
so your limits are incorrect too:
var patt = /^\d{1,10}(\.\d{1,4})?$/;
var patt = new RegExp(/^\d{1,10}(\.\d{1,4})?$/);
console.log( patt.test(1) ); // true
console.log( patt.test(1.1) ); // true
console.log( patt.test(11.11) ); // true
console.log( patt.test(111.111) ); // true
console.log( patt.test(1111.1111) ); // true
console.log( patt.test(1111111111.1111) ); // true
console.log( patt.test(111.11111) ); // false, because to long after decimal
console.log( patt.test(11111111111.1111) ); // false, because to long before decimal
Or as suggested here, not using RegExp
and instead use a literal. There could be support issues with RegExp
in some browsers. The output is the same, so better use this solution:
var patt = /^\d{1,10}(\.\d{1,4})?$/;
console.log( patt.test(1) ); // true
console.log( patt.test(1.1) ); // true
console.log( patt.test(11.11) ); // true
console.log( patt.test(111.111) ); // true
console.log( patt.test(1111.1111) ); // true
console.log( patt.test(1111111111.1111) ); // true
console.log( patt.test(111.11111) ); // false, because to long after decimal
console.log( patt.test(11111111111.1111) ); // false, because to long before decimal
The regex doesn't say the string should end with the last character, or start with the first one. Try:
var patt = /^\d{1,2}(\.\d{1,2})?$/;
For your 10 digits before and 4 digits after requirement:
var patt = /^\d{1,10}(\.\d{1,4})?$/;
Why don't you just check:
input < Math.pow(10,10) && input % Math.pow(10,-4) == 0
here is the code that is working for me
function IsValidNumericCode(number,messageDiv,msg){
var reNumeric = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
var result = reNumeric.test(number); if(!result){ messageDiv.html(msg);
messageDiv.fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
}else{ messageDiv.html('');} return result;
本文标签: regexregular expression for number and decimal points in javascriptStack Overflow
版权声明:本文标题:regex - regular expression for number and decimal points in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741239668a2363722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论