Home > Web Front-end > JS Tutorial > js function code to determine whether the input is a positive integer, floating point number and other numbers_javascript skills

js function code to determine whether the input is a positive integer, floating point number and other numbers_javascript skills

WBOY
Release: 2016-05-16 18:16:11
Original
1116 people have browsed it
1. Listed below are some regular expressions for judging value types
Copy code The code is as follows:

/^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 (.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 (.d )?)|(0 (. 0 )?))$/ //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)(.d)?$/ //Floating point number

2. How to use it in the program
Copy code The code is as follows:

var r = /^[0-9]* [1-9][0-9]*$/ //Positive integer
r.test(str); //str is the character you want to judge and returns true or false

3. Test a regular function, which can be used to determine the js function of digital, character, date and email
Copy code The code is as follows:

function TestRgexp(re, s){ // Parameter description re is the regular expression and s is the character to be judged
return re.test(s)
}

4. Function application
Copy code The code is as follows:


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