How does jquery tell if there are numbers?
This article mainly introduces jquery how to judge whether there is a number of related knowledge, content detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that everyone will read this jquery how to judge whether there are digital articles will be harvested, let's take a look at it.
In jquery, we can use the following methods to determine whether a variable is a number:
1、$.isNumeric();
jQuery is a built-in function used to determine whether it is a number. If $.isNumeric() is used to determine whether it is a number, some special characters will be treated as octal or decimal numbers, and it will be determined to be true, such as:(Related courses recommended: jQuery tutorial)$.isNumeric(0xFF); //true
$.isNumeric("0xFF"); //true$.isNumeric(3.13); //true
$.isNumeric("3.13"); //true
$.isNumeric(-3.13); //true
$.isNumeric("-3.13"); //true
$.isNumeric("03.13"); //true
$.isNumeric(01); //true
$.isNumeric(001); //true
$.isNumeric(+3.13); //true
$.isNumeric(0xFF); //true
$.isNumeric("0xFF"); //true
$.isNumeric(true); //false
$.isNumeric(NaN); //false
2、isNaN();
js is a function used to determine whether it is a number, it means "not a number", that is,"determine whether it is not a number, if it is not a number, it is true, if it is a number, it is false", its disadvantage is that some variables with empty values, such as null, spaces, etc., will turn them into "0" as a number to deal with: var val=$("#test").val();
var ival=parseInt(val);//if val is a number of character type convert to int if not ival is NaN
alert(typeof(ival));
if(! isNaN(ival)){
alert(val +"is a number");
} else{
alert(val +"not a number");
}
Description: isNaN() function returns false if the passed parameter is a number, otherwise returns true
3. Use regular expressions to judge
Common regular: "^\\d+$" //Non-negative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$" //positive integer
"^((-\\d+)|(0+))$" //Non-positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$" //negative integer
"^-?\\ d+$" //integer
"^\\d+(" //non-negative floating point number (positive floating point number + 0)
"^(([0-9]+\\. [0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\. [0-9]+)|([0-9]*[1-9][0-9]*))$" //positive floating point number
"^((-\\d+(" //Non-positive floating point number (negative floating point number + 0)
"^(-(([0-9]+\\. [0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\. [0-9]+)|([0-9]*[1-9][0-9]*)))$" //negative floating point number
"^(-?\\ d+)(" //floating point number
Example: var r=/^\+? [1-9][0-9]*$/; //Determine whether it is a positive integer
r.test(str);
Or: function isNumber(value) { //Verify whether it is a number
var patrn=/^(-)?\ d+(\.\ d+)?$/;
if (patrn.exec(value)==null || value=="") {
return false
} else {
return true
}
}
About "jquery how to judge whether there are numbers" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "how jquery judges whether there are numbers." If you still want to learn more knowledge, please pay attention to the industry information channel.