简体   繁体   中英

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. So I added validate property in rules object prop. But it throws me 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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