简体   繁体   English

反应电话号码输入验证失败

[英]react-phone-number-input validation failure

I am trying to avoid submitting react form if any input errors occurs.如果发生任何输入错误,我试图避免提交反应表单。 But only for the phone number input field's validations which is handled by react-phone-number-input package I am having issues because it allows to submit even for invalid inputs.但仅对于由react-phone-number-input package 处理的电话号码输入字段的验证,我遇到了问题,因为它甚至允许提交无效输入。 So I added validate property in rules object prop.所以我在规则 object prop 中添加了validate属性。 But it throws me parsePhoneNumber.js:18 Uncaught (in promise) TypeError: A text for parsing must be a string.但它抛出我parsePhoneNumber.js:18 Uncaught (in promise) TypeError: A text for parsing must be a string. My attempt is as below.我的尝试如下。

 <Controller
    name="phoneNum"
    render={({ field }) => (
      <SPhone
         label={Properties.TEL}
         {...field}
         error={errors.phoneNum && true}
         helperText={errors.phoneNum && errors.phoneNum.msg}/>
      )}
    control={control}
    defaultValue=""
    rules={{validate: (value) => isValidPhoneNumber(value)}}
 />

So what is the correct way to assign the rules prop to satisfy with the above phone number validation?那么分配规则道具以满足上述电话号码验证的正确方法是什么?

<Controller
name="phoneNum"
render={({ field }) => (
  <SPhone
     label={Properties.TEL}
     {...field}
     error={errors.phoneNum && true}
     helperText={errors.phoneNum && errors.phoneNum.msg}/>
  )}
control={control}
defaultValue=""
rules={{validate: (value) => isValidPhoneNumber(`${value}`)}}
/>

template string works for me.模板字符串对我有用。

暂无
暂无

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

相关问题 React-Phone-Number-Input + React-Hook-Form:如何在控制器验证中获取当前国家代码? - React-Phone-Number-Input + React-Hook-Form: How to get current country code in controller validation? 如何使用 react-phone-number-input 插入数据?,插入电话号码时出现验证错误 - How can I insert a data using react-phone-number-input?, validation error when I'm inserting a phone number 如何使用 react-phone-number-input 库与 Formik 成为朋友? - How to friend Formik with react-phone-number-input lib? 如何使用Yarn安装react-phone-number-input? - How to install react-phone-number-input with Yarn? "没有国家选择器的 react-phone-number-input" - react-phone-number-input without the country selector 使用反应电话号码输入时如何为电话输入的边框颜色设置无 - How to set none for border color of the phone input when using react-phone-number-input 从 react-phone-number-input 导入电话号码验证器时出错 npm package - Getting an error while importing Phone number Validator from the react-phone-number-input npm package 当您尝试在 PhoneInput(React 和 React-phone-number-input)键入内容时,弹出窗口会自行关闭 - Popup closes byself when you try to type something at PhoneInput (React and React-phone-number-input) 如何将 react-phone-number-input 与 Formik 的 Field 组件一起使用 - How to use react-phone-number-input with Formik's Field component 如何将 react-phone-number-input 添加到-react-final-form? - How to add react-phone-number-input to -react-final-form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM