Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

What's the cleanest, most effective way to validate decimal numbers in JavaScript?

Bonus points for:

  1. Clarity. Solution should be clean and simple.

  2. Cross-platform.

Test cases:

01. IsNumeric('-1') => true 

02. IsNumeric('-1.5') => true 

03. IsNumeric('0') => true 

04. IsNumeric('0.42') => true 

05. IsNumeric('.42') => true 

06. IsNumeric('99,999') => false 

07. IsNumeric('0x89f') => false 

08. IsNumeric('#abcdef') => false 

09. IsNumeric('1.2.3') => false 

10. IsNumeric('') => false 

11. IsNumeric('blah') => false

1 Answer

0 votes
by (106k points)
edited by

The cleanest and most effective way to validate decimal numbers in JavaScript is to following the below-mentioned way:-

isNumber: function(o) { 

return typeof o === 'number' && isFinite(o); 

}

Enroll in our web development training to become an Expert in Web Development! 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 4, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...