javascript - How To Detect Decimal as a Number in React - Stack Overflow

admin2025-04-19  0

I have this function I am using to detect is value is an integer or not. Now when I put decimal numbers, I throws an error in the validation I set for numbers.How will I put values like 5.79. It works on only integers

This is the function to check for integer

      static isInt(value: any) {
        var x: any;

       return typeof value =="number";
    }

I used this validation to check if input value is a number

static checkNumber(value: number, minNumber: number, maxNumber: number): IValdationResuslt {
        if (value >= minNumber) {
            if (value <= maxNumber) {
                return { state: true, message: `Valid` } as Valid;
            }
            return { state: false, message: `Max Characters ${maxNumber} : Entered ${value}` } as IValdationResuslt;
        }
        return { state: false, message: `Min Number ${minNumber} : Entered ${value}` } as Invalid;
    }

This is the input validation check for numbers

static inputValidation(value: any, state: any) {
  switch (state.type) {
    case "number":
      //check if the value is a valid number
      if (this.isInt(value)) {
        //check if the minNumber value hase been exceeded
        return this.checkNumber(value, state.minNumber, state.maxNumber);
      }
      return {
        state: false,
        message: "The value entered is not a number"
      }
      as IValdationResuslt;
    case "telephone":
      //check if the max length value has been exceeded
      if (this.isInt(value)) {
        //check if the minNumber value hase been exceeded
        return this.checkNumber(value, state.minNumber, state.maxNumber);
      }
  }
}

I have this function I am using to detect is value is an integer or not. Now when I put decimal numbers, I throws an error in the validation I set for numbers.How will I put values like 5.79. It works on only integers

This is the function to check for integer

      static isInt(value: any) {
        var x: any;

       return typeof value =="number";
    }

I used this validation to check if input value is a number

static checkNumber(value: number, minNumber: number, maxNumber: number): IValdationResuslt {
        if (value >= minNumber) {
            if (value <= maxNumber) {
                return { state: true, message: `Valid` } as Valid;
            }
            return { state: false, message: `Max Characters ${maxNumber} : Entered ${value}` } as IValdationResuslt;
        }
        return { state: false, message: `Min Number ${minNumber} : Entered ${value}` } as Invalid;
    }

This is the input validation check for numbers

static inputValidation(value: any, state: any) {
  switch (state.type) {
    case "number":
      //check if the value is a valid number
      if (this.isInt(value)) {
        //check if the minNumber value hase been exceeded
        return this.checkNumber(value, state.minNumber, state.maxNumber);
      }
      return {
        state: false,
        message: "The value entered is not a number"
      }
      as IValdationResuslt;
    case "telephone":
      //check if the max length value has been exceeded
      if (this.isInt(value)) {
        //check if the minNumber value hase been exceeded
        return this.checkNumber(value, state.minNumber, state.maxNumber);
      }
  }
}
Share edited Nov 19, 2018 at 15:53 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Nov 19, 2018 at 15:51 lutakynlutakyn 4741 gold badge7 silver badges22 bronze badges 1
  • Why don't you just check if the value is a number? Number(value) ? – Katie.Sun Commented Nov 19, 2018 at 16:07
Add a ment  | 

3 Answers 3

Reset to default 3

You can use Number.isFinite() to evaluate if an input is number or not

const isNumber = (number) => Number.isFinite(number);

let num = 4;
console.log(`${num} is a number: ${isNumber(num)}`);
num = 4.54;
console.log(`${num} is a number: ${isNumber(num)}`);
num = 'a';
console.log(`${num} is a number: ${isNumber(num)}`);

// 4 is a number: true
// 4.54 is a number: true
// a is a number: false

javascript already has a function called isInteger in it's Number object. If you want to check if the value is a number but not an integer, just check for:

!Number.isInteger(value) && typeof value == "number"

When I parsed it as a float the error is gone. It was being seen as as string although I set it as a number in the form.This saves the situation.

static isInt(value: any) 
{       
       var check = parseFloat(value)       
       console.log(typeof check)  

       return check

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745054232a282299.html

最新回复(0)