JavaScript code used to determine whether it is a number, an integer, or a floating point number
Regular Expression Method
The Code is as follows:
Function checkRate (input)
{
Var re =/^ [0-9] + .? [0-9] * $/; // determines whether the string is a number. // determines a positive integer./^ [1-9] + [0-9] * $/
If (! Re. test (input. rate. value ))
{
Alert ("enter a number (for example, 0.02 )");
Input. rate. focus ();
Return false;
}
}
The following describes how to write common functions.
The Code is as follows:
Function BASEisNotNum (theNum)
{
// Determine whether it is a number
If (BASEtrim (theNum) = "")
Return true;
For (var I = 0; I OneNum = theNum. substring (I, I + 1 );
If (oneNum <"0" | oneNum> "9 ")
Return true;
}
Return false;
}
Function BASEisNotInt (theInt)
{
// Judge 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; I OneNum = 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;