简体   繁体   English

"如何在 react-hook-form 中仅输入数字"

[英]How to input only number in react-hook-form

I'm using react-hook-form for my input components, but there is one problem.我正在为我的输入组件使用 react-hook-form,但是有一个问题。 In some text field, for example, text field for validation that take only number, i don't know how to do that, with normal textInput , we can use regex, like在某些文本字段中,例如,仅接受数字的验证文本字段,我不知道该怎么做,使用普通的textInput ,我们可以使用正则表达式,例如

 const [numberInput, setNumberInput] = useState("")
  function onTextChanged(value) {
    setNumberInput(value.replace(/[^0-9]/, "")) 
  }

and put that function and hook value on onTextChange and value respectively , i tried same method above on react-hook-form, but it won't work!并将该函数和挂钩值分别放在onTextChangevalue上,我在 react-hook-form 上尝试了上述相同的方法,但它不起作用! i still can input other character like "+" or "-", of course using numeric keyboard我仍然可以输入其他字符,例如“+”或“-”,当然使用数字键盘

So here is TextField component所以这里是 TextField 组件

export interface HTextFieldProps extends TextFieldProps {
  control: Control<any>
  name: string
  defaultValue?: string
}

/**
 * Describe your component here
 */
export const HTextField = function HookformTextField(props: HTextFieldProps) {
  const { name, control, defaultValue = "", ...restProps } = props

  return (
    <Controller
      control={control}
      name={name}
      render={({ field: { onChange, value }, fieldState: { error } }) => (
        <TextField
          {...restProps}
          onChangeText={onChange}
          value={value}
          defaultValue={defaultValue}
          error={(error && error.message) as TxKeyPath}
        />
      )}
      defaultValue={defaultValue}
    />
  )
}

Here is when i use this这是我使用这个的时候

         <HTextField
            onChangeText={(value) => onTextChanged(value)}
            value={numberInput}
            name={"times"}
            control={control}
            autoCapitalize="none"
            keyboardType={Platform.OS === "android" ? "numeric" : "number-pad"}
            returnKeyType="done"
            inputStyle={INPUT_STYLE}
            required
          />

So how can i use only number in react-hook-form look like this, thank you a lots那么我如何才能在 react-hook-form 中只使用数字,看起来像这样,非常感谢

use something liket this使用类似这样的东西

const allowOnlyNumber=(value)=>{
   return value.replace(/[^0-9]/g, '')
}

return (
    <Controller
      control={control}
      name={name}
      render={({ field: { onChange, value }, fieldState: { error } }) => (
        <TextField
          {...restProps}
          onChangeText={(text)=>onChange(allowOnlyNumber(text))}
          value={value}
          defaultValue={defaultValue}
          error={(error && error.message) as TxKeyPath}
        />
      )}
      defaultValue={defaultValue}
    />
  )

You can just set <TextField /> prop type to number and then there will be only numbers allowed.您可以将<TextField />道具typenumber ,然后只允许数字。

<Controller
  control={control}
  name={name}
  render={({ field: { onChange, value }, fieldState: { error } }) => (
    <TextField
      {...restProps}
      onChange={onChange}
      value={value}
      fullWidth
      label="Times"
      defaultValue={defaultValue}
      type="number"
      error={error && error.message}
    />
  )}
  defaultValue={defaultValue}
/>

编辑 React Hook 表单 - Typescript(分叉)

Using react-hook-form v7 the this is working for me:使用 react-hook-form v7 这对我有用:

<input {...register('age', {
    pattern: {
        value: /^[0-9]+$/,
        message: 'Please enter a number',
    },
})} />

Check the docs (V7): https:\/\/react-hook-form.com\/api\/useform\/register<\/a>检查文档(V7): https<\/a> :\/\/react-hook-form.com\/api\/useform\/register

<input
  type="number"
  {...register("test", {
    valueAsNumber: true,
  })}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM