英文:
React Native Form - Check if number has a decimal point issue
问题
我有一个React Native视图中的基本表单 - 简而言之,它在以后的日期用于计算收集的货车里程。表单输入使用数字键盘,两个字段都是必填的并经过验证。我希望验证数字已被输入,并且用户没有使用小数点。
背景信息 - 这是表单的截图:
每个字段都是使用RN TextInput创建的,除了状态名称不同 - 代码如下 -
<View style={CheckInStyles.textBlock}>
<Text style={CheckInStyles.introTxt}>
3. 结束里程。<Text style={CheckInStyles.errorStyle}> * </Text>
</Text>
<Text style={CheckInStyles.introTxtOr}>
(请在完成行程后输入里程)。
</Text>
</View>
<View style={formStyles.textInputBlock}>
<TextInput
placeholder="输入车辆结束里程"
style={formStyles.textInputText}
value={this.state.endMileage}
autoCorrect={false}
returnKeyType="done"
keyboardType="numeric"
onSubmitEditing={Keyboard.dismiss}
onChangeText={text => this.setState({endMileage: text})}
/>
</View>
初始状态是数字 - 代码如下:
constructor(props) {
super(props);
this.state = {
startMileage: 0,
endMileage: 0,
}
}
在验证时,我有以下代码 -
if (this.state.startMileage && this.state.endMileage && this.state.tripReason && Number.isInteger(this.state.startMileage) && Number.isInteger(this.state.endMileage)) {
return true;
} else {
return false;
}
我需要帮助理解的部分是数字/小数点检查 - 我使用了
Number.isInteger()
如果我使用直接的数字,例如:
Number.isInteger(3.3)
结果是false - 这是预期的
Number.isInteger(3)
结果是true - 这是预期的
但是,如果我使用状态的数字值 -
Number.isInteger(this.state.endMileage))
无论值是整数还是包含小数点 - 结果始终为false。
这是因为状态输入使用文本输入,值被转换为字符串吗?有人能否提供解决方案/解释?谢谢。
英文:
I have a basic form within a React Native view - as a basic summary it collects van mileage used in calculations at a later date. The form input uses a numeric keyboard, both fields are required and validated. I'd like to validate the number has been entered and that the user hasn't used a decimal point.
For background - heres a shot of the form:
each field is created using a RN TextInput, both are identical bar the state names - code as follows -
<View style={CheckInStyles.textBlock}>
<Text style={CheckInStyles.introTxt}>
3. End Mileage.<Text style={CheckInStyles.errorStyle}> * </Text>
</Text>
<Text style={CheckInStyles.introTxtOr}>
(Please enter mileage upon completing your journey).
</Text>
</View>
<View style={formStyles.textInputBlock}>
<TextInput
placeholder="Enter Vehicle END Mileage"
style={formStyles.textInputText}
value={this.state.endMileage}
autoCorrect={false}
returnKeyType="done"
keyboardType="numeric"
onSubmitEditing={Keyboard.dismiss}
onChangeText={text => this.setState({endMileage: text})}
/>
</View>
the initial state is numeric - code below:
constructor(props) {
super(props);
this.state = {
startMileage:0,
endMileage:0,
}
}
on validation I have the following code -
if (this.state.startMileage && this.state.endMileage && this.state.tripReason && Number.isInteger(this.state.startMileage) && Number.isInteger(this.state.endMileage)) {
return true
}
else {
return false;
}
The part I need help understanding is the number / decimal point check - i'm using
Number.isInteger()
If I use a direct number ie:
Number.isInteger(3.3)
the result is false - as expected
Number.isInteger(3)
the result is true as expected
If however I use the state numeric value -
Number.isInteger(this.state.endMileage))
regardless of whether the value is a whole or contains a decimal point - the results is always false.
Is this because the state input is using a text input and the value is converted to a string? Can anyone advise a solution / explanation? Thanks.
答案1
得分: 0
"Is this because the state input is using a text input?" 是的,确切是这样。
Number.isInteger(value) 如果值不是数字,将始终返回false。
您可以查看Number.parseFloat,并可能传递keyboardType="number-pad"而不是numeric。
英文:
"Is this because the state input is using a text input?" Yes, it is exactly this.
Number.isInteger(value) will always return false if the value is not a number.
You could look into Number.parseFloat, and possibly pass keyboardType="number-pad" rather than numeric.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论