Computer >> Computer tutorials >  >> Programming >> Javascript

How to validate decimal numbers in JavaScript?


To validate decimal numbers in JavaScript, use the match() method. It retrieves the matches when matching a string against a regular expression.

Example

You can try to run the following code to validate decimal numbers −

<html>
   <body>
      <script>
         var str = "1.5";
         var re = /^[-+]?[0-9]+\.[0-9]+$/;
         var found = str.match( re );
         document.write("decimal" );
      </script>
   </body>
</html>