Home  >  Article  >  Web Front-end  >  A simple example of Javascript regular expression checking numbers

A simple example of Javascript regular expression checking numbers

高洛峰
高洛峰Original
2016-12-08 10:51:361553browse

实例如下:

$("input[datatype=number]").blur(function () {
           var str = $(this).val();
           if (!isDecimal(str)) {
             alert("请输入数字");
           }
         });
 
function isDecimal(str) {
        if (isInteger(str)) return true;
        var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/;
        if (re.test(str)) {
          if (RegExp.$1 == 0 && RegExp.$2 == 0) return false;
          return true;
        } else {
          return false;
        }
      }
      function isInteger(str) {
        var regu = /^[-]{0,1}[0-9]{1,}$/;
        return regu.test(str);
      }

   


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn