React Native表单 – 检查数字是否有小数点问题

huangapple go评论61阅读模式
英文:

React Native Form - Check if number has a decimal point issue

问题

我有一个React Native视图中的基本表单 - 简而言之,它在以后的日期用于计算收集的货车里程。表单输入使用数字键盘,两个字段都是必填的并经过验证。我希望验证数字已被输入,并且用户没有使用小数点。

背景信息 - 这是表单的截图:

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:

React Native表单 – 检查数字是否有小数点问题

each field is created using a RN TextInput, both are identical bar the state names - code as follows -

&lt;View style={CheckInStyles.textBlock}&gt;
          &lt;Text style={CheckInStyles.introTxt}&gt;
            3. End Mileage.&lt;Text style={CheckInStyles.errorStyle}&gt; * &lt;/Text&gt;
          &lt;/Text&gt;
          &lt;Text style={CheckInStyles.introTxtOr}&gt;
            (Please enter mileage upon completing your journey).
          &lt;/Text&gt;
        &lt;/View&gt;
        &lt;View style={formStyles.textInputBlock}&gt;
          &lt;TextInput
            placeholder=&quot;Enter Vehicle END Mileage&quot;
            style={formStyles.textInputText}
            value={this.state.endMileage}
            autoCorrect={false}
            returnKeyType=&quot;done&quot;
            keyboardType=&quot;numeric&quot;
            onSubmitEditing={Keyboard.dismiss}
            onChangeText={text =&gt; this.setState({endMileage: text})}
          /&gt;
        &lt;/View&gt;

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 &amp;&amp; this.state.endMileage &amp;&amp; this.state.tripReason &amp;&amp; Number.isInteger(this.state.startMileage) &amp;&amp; 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.

huangapple
  • 本文由 发表于 2023年4月4日 18:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定