Home  >  Article  >  Web Front-end  >  How to determine if a variable is a number in jquery

How to determine if a variable is a number in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-19 13:50:001795browse

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

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

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

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

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+))$/;

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!

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