The isNaN() number function is used to check any given Variable or State for Number value. This is mostly used to set validation on any variable in react native app. So in this tutorial we would going to create a react native android iOS app in which we would get the TextInput value on button click and Check Value Is Number Or Not Using isNaN() Validation function.
Note : If the value do not contain the number then it will return True and if the value contain number value then it will return us False.
Contents in this project Check Value Is Number Or Not Using isNaN() Validation in React Native:
1. Import StyleSheet, Text, View, Button, Alert and TextInput component in react native project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, Text, View, Button, Alert, TextInput } from ‘react-native’;
|
2. Create constructor() in your project and Inside the constructor() we would declare a State named as Value. This state is used to get and store TextInput value.
1
2
3
4
5
6
7
8
9
10
|
constructor()
{
super();
this.state={
Value : ”
}
}
|
3. Create a function named as CheckValueIsNumberOrNot(). Inside this function we would check the State Value using isNaN() function. We would use isNaN() function with IF – Else condition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
CheckValueIsNumberOrNot=()=>{
if(isNaN(this.state.Value))
{
// If the Given Value is Not Number Then It Will Return True and This Part Will Execute.
Alert.alert(“Value is Not Number”);
}
else
{
// If the Given Value is Number Then It Will Return False and This Part Will Execute.
Alert.alert(“Value is Number”);
}
}
|
4. Create a TextInput and Button in Root View in render’s return block. We would store the TextInput value in Value State using onChangeText() method. After that we would call the CheckValueIsNumberOrNot() function on Button onPress event.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
render() {
return (
<View style={styles.MainContainer}>
<TextInput
placeholder=“Enter Some Text here”
onChangeText={ TextInputValue => this.setState({Value: TextInputValue}) }
style={styles.TextInputStyle}
/>
<Button title=” Check Value Is Number Or Not “ onPress={this.CheckValueIsNumberOrNot} />
</View>
);
}
|
5. Create Style for Root View and TextInput .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’
},
TextInputStyle:{
textAlign: ‘center’,
height: 50,
width: ‘95%’,
marginBottom: 10
},
});
|
6. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
import React, { Component } from ‘react’;
import { StyleSheet, Text, View, Button, Alert, TextInput } from ‘react-native’;
export default class App extends Component<{}> {
constructor()
{
super();
this.state={
Value : ”
}
}
CheckValueIsNumberOrNot=()=>{
if(isNaN(this.state.Value))
{
// If the Given Value is Not Number Then It Will Return True and This Part Will Execute.
Alert.alert(“Value is Not Number”);
}
else
{
// If the Given Value is Number Then It Will Return False and This Part Will Execute.
Alert.alert(“Value is Number”);
}
}
render() {
return (
<View style={styles.MainContainer}>
<TextInput
placeholder=“Enter Some Text here”
onChangeText={ TextInputValue => this.setState({Value: TextInputValue}) }
style={styles.TextInputStyle}
/>
<Button title=” Check Value Is Number Or Not “ onPress={this.CheckValueIsNumberOrNot} />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’
},
TextInputStyle:{
textAlign: ‘center’,
height: 50,
width: ‘95%’,
marginBottom: 10
},
});
|
Screenshot in Android device :
Screenshot in iOS device :