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

How to determine if a variable is a number in jquery

coldplay.xixi
Release: 2020-11-19 13:50:00
Original
1844 people have browsed it

Jquery method to determine whether a variable is a number: 1. Use the built-in function in [$.isNumeric] to determine whether a variable or a given value is a number; 2. Use the [isNaN()] function; 3. Use the function [Number()] to convert the string into a number.

How to determine if a variable is a number in jquery

jquery method to determine whether a variable is a number:

In jquery, we can use the following methods to Determine whether a variable is a number

1, $.isNumeric();

A function built into jquery to determine whether it is a number. The webmaster used to write it specifically Let’s introduce it in an article: $.isNumeric—the built-in function in jquery used to determine whether a variable or a given value is a number. If you use $.isNumeric() to determine whether it is a number, some special characters will be treated as An octal or hexadecimal number is judged to be true, such as:

$.isNumeric(0xFF);    //true
$.isNumeric("0xFF");    //true
Copy after login

2, isNaN(); one of

js is used to determine whether it is A function of numbers, which means "not a number", that is, "determine whether it is not a number. If it is not a number, it will be true, if it is a number, it will be false." Its disadvantage is that some variables have empty values, such as null and spaces. etc., they will be converted into "0" and treated as numbers:

isNaN("abc");//true
isNaN(null);//false
Copy after login

3, Number();

Number() function is actually used in js It converts a string into a number, but it can also be used to determine whether it is a number. If it is not a number, the value "NaN" will be returned. But like isNaN() above, if the value is null or empty, it will Return a 0:

Number("aijquery.cn");//NaN
Number(null);//0
Copy after login

4. Regular:

The safest and most complete method! ! You can create specific judgment rules according to your own needs:

var r=/^[1-9][0-9]+$/gi;
document.writeln(r.test("011"));//false
document.writeln(r.test("11"));//true
Copy after login

The above is just an example given by the webmaster. If you want to know more examples about regularity, you can pay attention to the regularity topic of this site and the positive site. I would like to share with you some commonly used rules related to judging numbers:

数字:var r=/^[0-9]*$/; 
验证n位的数字:var r=/^\d{n}$/; 
验证至少n位数字:var r=/^\d{n,}$/; 
验证m-n位的数字:var r=/^\d{m,n}$/; 
验证零和非零开头的数字:var r=/^(0|[1-9][0-9]*)$/; 
验证有两位小数的正实数:var r=/^[0-9]+(.[0-9]{2})?$/; 
验证有1-3位小数的正实数:var r=/^[0-9]+(.[0-9]{1,3})?$/; 
验证非零的正整数:var r=/^\+?[1-9][0-9]*$/; 
验证非零的负整数:var r=/^\-[1-9][0-9]*$/; 
验证非负整数 var r=/^\d+$/; 
验证非正整数 var r=/^((-\d+)|(0+))$/;
Copy after login

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to determine if a variable is a number in jquery. For more information, please follow other related articles on the PHP Chinese website!

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