How to determine whether a specified value is a positive number in javascript

青灯夜游
Release: 2022-02-07 14:25:30
Original
4995 people have browsed it

Judgment method: 1. Use the "var reg = /^\d (?=\.{0,1}\d $|$)/" statement to define the regular expression object reg; 2. Use " The reg.test(specified value)" statement uses regular expressions to determine whether the specified value is a positive number. If it returns true, it is a positive number, otherwise it is not a positive number.

How to determine whether a specified value is a positive number in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript determines whether the specified value is a positive number

In javascript, you can use regular expressions to determine whether the specified value is a positive number.

Implementation code:

function validate(num) {
	if (num != 0) {
		var reg = /^\d+(?=\.{0,1}\d+$|$)/;
		if (reg.test(num)) {
			console.log(num+" 是正数");
		} else {
			console.log(num+" 不是正数");
		}
	}
	else{
		console.log("0既不是正数也不是负数");
	}
}
validate(-1);
validate(-2);
validate(0);
validate(1);
validate(1.5);
validate(2.365);
Copy after login

How to determine whether a specified value is a positive number in javascript

Description: The

test() method is used to detect whether a string matches a certain pattern. Returns true if there is a matching value in the string, false otherwise.

Grammar:

RegExpObject.test(string)
Copy after login

Extended knowledge: expressions to check numbers

  • Numbers: ^[0-9]*$

  • n-digit number: ^\d{n}$

  • At least n-digit number: ^\d{n,}$

  • ##m-n-digit number:

    ^\d{ m,n}$

  • Numbers starting with zero and non-zero:

    ^(0|[1-9][0-9]*)$

  • Numbers starting with non-zero and with up to two decimal places:

    ^([1-9][0-9]*) (.[0-9]{ 1,2})?$

  • Positive or negative number with 1-2 decimal places:

    ^(\-)?\d (\.\d {1,2})?$

  • Positive numbers, negative numbers, and decimals:

    ^(\-|\ )?\d (\.\d ) ?$

  • Positive real number with two decimal places:

    ^[0-9] (.[0-9]{2})?$

  • Positive real numbers with 1 to 3 decimal places:

    ^[0-9] (.[0-9]{1,3})?$

  • Non-zero positive integer:

    ^[1-9]\d*$ or ^([1-9][0-9]* ){1,3}$ or ^\ ?[1-9][0-9]*$

  • Non-zero negative integer:

    ^\-[1-9][]0-9"*$ or ^-[1-9]\d*$

  • Non-negative integer:

    ^\d $ or ^[1-9]\d*|0$

  • Non-positive integer :

    ^-[1-9]\d*|0$ or ^((-\d )|(0 ))$

  • Non-negative floating point number:

    ^\d (\.\d )?$ or ^[1-9]\d*\.\d*|0\.\d*[ 1-9]\d*|0?\.0 |0$

  • Non-positive floating point number:

    ^((-\d (\.\d )?)|(0 (\.0 )?))$ or ^(-([1-9]\d*\.\d*|0\.\d*[1-9 ]\d*))|0?\.0 |0$

  • Positive floating point number:

    ^[1-9]\d*\.\d *|0\.\d*[1-9]\d*$ or ^(([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:

    ^-([1-9]\d*\.\d*|0\.\d*[1-9 ]\d*)$ or ^(-(([0-9] \.[0-9]*[1-9][0-9]*)|([0-9] *[1-9][0-9]*\.[0-9] )|([0-9]*[1-9][0-9]*)))$

  • Floating point number:

    ^(-?\d )(\.\d )?$ or ^-?([1-9]\d*\. \d*|0\.\d*[1-9]\d*|0?\.0 |0)$

  • ##[Related recommendations:
JavaScript learning tutorial

The above is the detailed content of How to determine whether a specified value is a positive number in javascript. 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