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);
}
}
}
javascript reactjs
add a comment |
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);
}
}
}
javascript reactjs
Why don't you just check if the value is a number?Number(value)
?
– Katie.Sun
Nov 19 at 16:07
add a comment |
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);
}
}
}
javascript reactjs
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
javascript reactjs
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
add a comment |
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
add a comment |
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
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 accessisNumber = (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
add a comment |
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"
add a comment |
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
}
add a comment |
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
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 accessisNumber = (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
add a comment |
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
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 accessisNumber = (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
add a comment |
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
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
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 accessisNumber = (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
add a comment |
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 accessisNumber = (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
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
edited Nov 20 at 9:20
answered Nov 19 at 15:58
Gilad Bar
603312
603312
add a comment |
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Nov 19 at 17:16
lutakyn
418
418
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Why don't you just check if the value is a number?
Number(value)
?– Katie.Sun
Nov 19 at 16:07