Get code from sms in React Native

01 February 2023

React Native sms text input validation
Photo by jamie452

How to autofill code from messages

When working with forms that require you to enter a code sent to you via text message, make sure to include a prop called "textContentType" with a value of "oneTimeCode" for iOS, and you can complete with "autoComplete" and "one-time-code". This will help the form recognize the code you enter, and automatically fill the TextInput.

It can be used for various purposes, such as:

  • SMS codes
  • SMS text messages
  • SMS login codes
  • Apple ID codes
  • Two-factor authentication
  • Verification codes

For iOS 12+, the oneTimeCode attribute can be used to indicate that a field can be auto-filled by a code arriving in an SMS.

import { TextInput } from 'react-native';

<TextInput
textContentType="oneTimeCode"
autoComplete="one-time-code"
...
/>

Reference

https://reactnative.dev/docs/textinput#autocomplete

https://reactnative.dev/docs/textinput#textcontenttype-ios

Gravatar for dleuliette@gmail.com

Hi, I’m David, a french freelance developer working remotely. I’m the author of this blog, nice to meet you!

Subscribe?

Be the first to receive insightful articles and actionable resources that help you to elevate your skills across every touch point.