Home > Web Front-end > JS Tutorial > body text

JS code to determine whether it is a number, whether it is an integer, whether it is a floating point number_javascript skills

WBOY
Release: 2016-05-16 18:28:40
Original
1583 people have browsed it
Regular expression method
Copy code The code is as follows:

function checkRate (input)
{
var re = /^[0-9] .?[0-9]*$/; //Judge whether the string is a number//Judge a positive integer/^[1-9 ] [0-9]*]*$/
if (!re.test(input.rate.value))
{
alert("Please enter a number (eg: 0.02)");
input.rate.focus();
return false;
}
}

The following is how to write the ordinary function
Copy code The code is as follows:

function BASEisNotNum(theNum)
{
//Determine whether it is a number
if (BASEtrim(theNum)=="")
return true;
for(var i=0;ioneNum=theNum.substring(i, i 1);
if (oneNum<"0" || oneNum>"9")
return true;
}
return false;
}
function BASEisNotInt(theInt)
{
//Determine whether it is an integer
theInt=BASEtrim(theInt);
if ((theInt.length>1 && theInt.substring(0,1)=="0") | | BASEisNotNum(theInt)){
return true;
}
return false;
}
function BASEisNotFloat(theFloat)
{
//Determine whether it is a floating point number
len=theFloat.length;
dotNum=0;
if (len==0)
return true;
for(var i=0;ioneNum=theFloat.substring(i,i 1);
if (oneNum==".")
dotNum ;
if ( ((oneNum<"0" || oneNum>"9") && oneNum!=".") || dotNum>1)
return true;
}
if (len>1 && theFloat.substring(0,1)=="0"){
if (theFloat.substring(1,2)!=".")
return true;
}
return false;
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