How To Detect Decimal as a Number in React











up vote
1
down vote

favorite












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|improve this question
























  • Why don't you just check if the value is a number? Number(value) ?
    – Katie.Sun
    Nov 19 at 16:07















up vote
1
down vote

favorite












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|improve this question
























  • Why don't you just check if the value is a number? Number(value) ?
    – Katie.Sun
    Nov 19 at 16:07













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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|improve this question















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);
}
}
}








javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 15:53









mplungjan

86k20121180




86k20121180










asked Nov 19 at 15:51









lutakyn

418




418












  • Why don't you just check if the value is a number? Number(value) ?
    – Katie.Sun
    Nov 19 at 16:07


















  • Why don't you just check if the value is a number? Number(value) ?
    – Katie.Sun
    Nov 19 at 16:07
















Why don't you just check if the value is a number? Number(value) ?
– Katie.Sun
Nov 19 at 16:07




Why don't you just check if the value is a number? Number(value) ?
– Katie.Sun
Nov 19 at 16:07












3 Answers
3






active

oldest

votes

















up vote
2
down vote













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








share|improve this answer





















  • static isInt(value: any) { return Number.isFinite(value) },it this going to work?
    – lutakyn
    Nov 19 at 16:15










  • It should. You can quickly try this in browser console.
    – Dinesh Pandiyan
    Nov 19 at 16:16










  • yes I did that,validation still throws error that it is not a number,
    – lutakyn
    Nov 19 at 16:20










  • You try this for safe access isNumber = (number) => number && Number.isFinite(number);
    – Dinesh Pandiyan
    Nov 19 at 16:21










  • Number.isFinite works,i think the problem is from the checkNumber() I wrote
    – lutakyn
    Nov 19 at 16:27


















up vote
1
down vote













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"






share|improve this answer






























    up vote
    0
    down vote













    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

    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378262%2fhow-to-detect-decimal-as-a-number-in-react%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      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








      share|improve this answer





















      • static isInt(value: any) { return Number.isFinite(value) },it this going to work?
        – lutakyn
        Nov 19 at 16:15










      • It should. You can quickly try this in browser console.
        – Dinesh Pandiyan
        Nov 19 at 16:16










      • yes I did that,validation still throws error that it is not a number,
        – lutakyn
        Nov 19 at 16:20










      • You try this for safe access isNumber = (number) => number && Number.isFinite(number);
        – Dinesh Pandiyan
        Nov 19 at 16:21










      • Number.isFinite works,i think the problem is from the checkNumber() I wrote
        – lutakyn
        Nov 19 at 16:27















      up vote
      2
      down vote













      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








      share|improve this answer





















      • static isInt(value: any) { return Number.isFinite(value) },it this going to work?
        – lutakyn
        Nov 19 at 16:15










      • It should. You can quickly try this in browser console.
        – Dinesh Pandiyan
        Nov 19 at 16:16










      • yes I did that,validation still throws error that it is not a number,
        – lutakyn
        Nov 19 at 16:20










      • You try this for safe access isNumber = (number) => number && Number.isFinite(number);
        – Dinesh Pandiyan
        Nov 19 at 16:21










      • Number.isFinite works,i think the problem is from the checkNumber() I wrote
        – lutakyn
        Nov 19 at 16:27













      up vote
      2
      down vote










      up vote
      2
      down vote









      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








      share|improve this answer












      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








      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





      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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 19 at 15:58









      Dinesh Pandiyan

      1,454722




      1,454722












      • static isInt(value: any) { return Number.isFinite(value) },it this going to work?
        – lutakyn
        Nov 19 at 16:15










      • It should. You can quickly try this in browser console.
        – Dinesh Pandiyan
        Nov 19 at 16:16










      • yes I did that,validation still throws error that it is not a number,
        – lutakyn
        Nov 19 at 16:20










      • You try this for safe access isNumber = (number) => number && Number.isFinite(number);
        – Dinesh Pandiyan
        Nov 19 at 16:21










      • Number.isFinite works,i think the problem is from the checkNumber() I wrote
        – lutakyn
        Nov 19 at 16:27


















      • static isInt(value: any) { return Number.isFinite(value) },it this going to work?
        – lutakyn
        Nov 19 at 16:15










      • It should. You can quickly try this in browser console.
        – Dinesh Pandiyan
        Nov 19 at 16:16










      • yes I did that,validation still throws error that it is not a number,
        – lutakyn
        Nov 19 at 16:20










      • You try this for safe access isNumber = (number) => number && Number.isFinite(number);
        – Dinesh Pandiyan
        Nov 19 at 16:21










      • Number.isFinite works,i think the problem is from the checkNumber() I wrote
        – lutakyn
        Nov 19 at 16:27
















      static isInt(value: any) { return Number.isFinite(value) },it this going to work?
      – lutakyn
      Nov 19 at 16:15




      static isInt(value: any) { return Number.isFinite(value) },it this going to work?
      – lutakyn
      Nov 19 at 16:15












      It should. You can quickly try this in browser console.
      – Dinesh Pandiyan
      Nov 19 at 16:16




      It should. You can quickly try this in browser console.
      – Dinesh Pandiyan
      Nov 19 at 16:16












      yes I did that,validation still throws error that it is not a number,
      – lutakyn
      Nov 19 at 16:20




      yes I did that,validation still throws error that it is not a number,
      – lutakyn
      Nov 19 at 16:20












      You try this for safe access isNumber = (number) => number && Number.isFinite(number);
      – Dinesh Pandiyan
      Nov 19 at 16:21




      You try this for safe access isNumber = (number) => number && Number.isFinite(number);
      – Dinesh Pandiyan
      Nov 19 at 16:21












      Number.isFinite works,i think the problem is from the checkNumber() I wrote
      – lutakyn
      Nov 19 at 16:27




      Number.isFinite works,i think the problem is from the checkNumber() I wrote
      – lutakyn
      Nov 19 at 16:27












      up vote
      1
      down vote













      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"






      share|improve this answer



























        up vote
        1
        down vote













        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"






        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          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"






          share|improve this answer














          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"







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 9:20

























          answered Nov 19 at 15:58









          Gilad Bar

          603312




          603312






















              up vote
              0
              down vote













              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

              }





              share|improve this answer

























                up vote
                0
                down vote













                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

                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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

                  }





                  share|improve this answer












                  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

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 17:16









                  lutakyn

                  418




                  418






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378262%2fhow-to-detect-decimal-as-a-number-in-react%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen