Home > Web Front-end > JS Tutorial > body text

A simple example of Javascript regular expression checking numbers

高洛峰
Release: 2016-12-08 10:51:36
Original
1598 people have browsed it

实例如下:

$("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);
      }
Copy after login

   


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template