Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to judge that an input must be a number with js? Include negative numbers and integers.
How to judge that an input must be a number with js? Include negative numbers and integers.
You can use the positive expression ~~JS to judge whether it is a number, an integer or a floating-point regular expression method 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 (! Retest (Input. Rate. Value))

{

Alert ("Please enter a number (such as 0.02)");

input . rate . focus();

Returns false

}

} The following is a common function writing function BASEisNotNum(theNum)

{

//Judge whether it is a number.

if (BASEtrim(theNum)= " ")

Return true

for(var I = 0; I < length; i++){

oneNum=theNum.substring(i,I+ 1);

if(oneNum & lt; " 0 " | | oneNum & gt"9")

Return true

}

Returns false

} function basis notint(theInt)

{

//Determine whether it is an integer.

theInt = base trim(theInt);

if((theint . length & gt; 1。 & amptheInt.substring(0, 1)= = " 0 ")| | BASEisNotNum(theInt)){

Return true

}

Returns false

} function BASEisNotFloat(theFloat)

{

//Determine whether it is a floating-point number.

len = theFloat.length

dot num = 0;

if (len==0)

Return true

for(var I = 0; I & ltleni++){

oneNum=theFloat.substring(i,I+ 1);

if (oneNum== "。" )

dot num++;

If ((one num < "0" || one num > "9")&; & amponeNum! =".")| | dotNum & gt 1)

Return true

}

if(len & gt; 1。 & amptheFloat.substring(0, 1)=="0"){

if (theFloat.substring( 1,2)! =".")

Return true

}

Returns false