Home > Net >  Prevent pressing dot key in React Native numeric type keyboard
Prevent pressing dot key in React Native numeric type keyboard

Time:05-05

I developed an application where user can enter their mobile numbers and I want to prevent users from pressing the dot and dash key, How can I achieve it?

CodePudding user response:

you can use react-phone-number-input

import React, { useState } from 'react'
import PhoneInput from 'react-phone-number-input/react-native-input'

function Example() {
  const [value, setValue] = useState()
  return (
    <PhoneInput
      style={...}
      defaultCountry="US"
      value={value}
      onChange={setValue} />
  )
}

see here demo

CodePudding user response:

For the sake of not integrating a third party package just to check the phone number, you can simply create a regex that matches dot and dash keys and show a message "Please don't use "-" and "." while users are entering their phone number. This solution will provide more space for you to tweak things later if the needs from this component changes.

  • Related