判断方法:1、使用“$.isNumeric(x)”,可判断指定参数“x”是否是数字值;2、使用“isNaN(x)”,可判断参数“x”是否是为数字值;3、使用正则表达式“/^(-)?\d+(\.\d+)?$/”配合test来判断数字。
本教程操作环境:windows7系统、jquery3.2.1版,该方法适用于所有品牌电脑。
相关推荐:《jQuery视频》
jquery判断变量是否为数字的方法
方法1、使用$.isNumeric()
jquery里内置的一个用来判断是否为数字的函数$.isNumeric()。$.isNumeric() 函数用于判断指定参数是否是一个数字值。
用$.isNumeric()来判断是否为数字的话,一些特殊的字符会被当成8进制或12进制的数字,判定为true。
注意:在jQuery 3.0中,$.isNumeric()方法只有接收number类型的参数,或者是可以被强制为有限数值的 string类型的参数时,才会返回true,否则返回false。
语法
$.isNumeric( value )
参数:
value:任意类型,需要进行判断的任意值。
示例:
$.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() 函数
var val = $("#test").val(); var ival = parseInt(val);//如果变量val是字符类型的数则转换为int类型 如果不是则ival为NaN alert(typeof(ival)); if(!isNaN(ival)){ alert(val +"是数字"); } else{ alert(val +"不是数字"); }
说明: isNaN()函数,如果传入的参数是数字返回false,否则返回true
方法3:使用正则表达式判断
常用正则:
" /^(0|[1-9]\d*)$/" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\\d+)|(0+))$" //非正整数(负整数 + 0) "^-[0-9]*[1-9][0-9]*$" //负整数 "^-?\\d+$" //整数 "^\\d+(" //非负浮点数(正浮点数 + 0) "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数 "^((-\\d+(" //非正浮点数(负浮点数 + 0) "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数 "^(-?\\d+)(" //浮点数
实例:
var r = /^\+?[1-9][0-9]*$/; //判断是否为正整数 r.test(str);
或者:
function isNumber(value) { //验证是否为数字 var patrn = /^(-)?\d+(\.\d+)?$/; if (patrn.exec(value) == null || value == "") { return false } else { return true } }
知识拓展:JavaScript的Number()函数 —-这里不是判断方法
<script type="text/javascript"> var test1= new Boolean(true); var test2= new Boolean(false); var test3= new Date(); var test4= new String("999"); var test5= new String("999 888"); document.write(Number(test1)+ "<br />"); document.write(Number(test2)+ "<br />"); document.write(Number(test3)+ "<br />"); document.write(Number(test4)+ "<br />"); document.write(Number(test5)+ "<br />"); </script>
输出的结果是:
1 0 1492855437972 999 NaN
可以看出在JavaScript中0代表false,1代表true。但是我测试了一下,除0以外的数放在if的条件中,都可以执行if语句内容。所以。可以得出除0以外的所有数都可以代表true。
更多编程相关知识,请访问:编程课程!!
The above is the detailed content of How does jquery determine whether it is a number?. For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),