Skip to content
animorio
IT
All components

Self-formatting phone field

Forms, HoverGSAP, CSS onlyMediumCompressed weight: Demo 9.0 KBGSAP 28 KBFreeDownload the zip

The phone number field of a sign-up, on very light grey: as you type each digit springs in from below and the country’s brackets, spaces and dashes open up between the groups by themselves. Type a plus and a code and the country switches by itself, the code rolls and the pill widens; the menu offers six countries. If the number is short the field shakes and says how many digits are missing, when it is whole a green check draws itself and Send code wakes up and sends.

The code

A folder that runs on its own: index.html, style.css, script.js and the libraries from a CDN. The React and Vue tabs hold the same demo as a ready component, which switches its animations off when it leaves the page.

<!doctype html>
<!--
  "Self-formatting phone field" from Animorio · https://animorio.com
  License: free on your own sites and your clients’ sites, paid work included.
  Not for resale, redistribution, templates or code collections · https://animorio.com/en/license/
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>The phone number field that formats itself</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/mona-sans@5.3.0/wdth.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <main class="stage">
    <!-- the phone field of a sign-up: the digits fall into their groups as you type, the country code switches by itself
         when you type a plus, and once the number is whole a check draws itself and Send code wakes up -->
    <form class="phone" data-form autocomplete="off" novalidate>
      <label class="phone__label" for="tel">Phone number</label>

      <div class="field" data-field>
        <div class="country" data-country-wrap>
          <button class="country__btn" type="button" data-country aria-haspopup="listbox" aria-expanded="false" aria-label="Country code">
            <span class="country__iso" data-iso>US</span>
            <span class="country__code"><span class="country__roll" data-code>+1</span></span>
            <svg class="country__chev" viewBox="0 0 12 12" aria-hidden="true"><path d="M3 4.6 6 7.6l3-3"></path></svg>
          </button>
          <ul class="menu" data-menu role="listbox" aria-label="Country code"></ul>
        </div>

        <span class="field__rule" aria-hidden="true"></span>

        <div class="digits">
          <input class="digits__input" id="tel" type="tel" inputmode="tel" data-input aria-describedby="hint" spellcheck="false">
          <div class="digits__show" data-show aria-hidden="true"><span class="digits__ph" data-ph></span><span class="caret" data-caret></span></div>
        </div>

        <span class="ok" data-ok aria-hidden="true">
          <svg viewBox="0 0 20 20"><circle cx="10" cy="10" r="9"></circle><path data-tick d="M6 10.4 8.7 13 14 7.4" pathLength="1"></path></svg>
        </span>
      </div>

      <p class="hint" id="hint" data-hint aria-live="polite"><span data-hint-text>We’ll text you a six-digit code.</span></p>

      <button class="send" type="submit" data-send disabled>
        <span class="send__label" data-send-label>Send code</span>
        <svg class="send__spin" data-spin viewBox="0 0 20 20" aria-hidden="true"><circle cx="10" cy="10" r="7"></circle></svg>
        <svg class="send__done" data-done viewBox="0 0 20 20" aria-hidden="true"><path d="M5.5 10.4 8.6 13.4 14.5 7" pathLength="1"></path></svg>
      </button>
    </form>

    <svg class="ghost" data-ghost viewBox="0 0 24 24" aria-hidden="true"><path d="M5 3.2 19.2 11l-6.3 1.6-3.3 5.9Z"></path></svg>
  </main>

  <script src="https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/gsap.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

Playground

Change values, colours and text with the controls and watch the animation respond live. Or write in the code yourself. Then download the folder with your changes.

20 values to change

Or ask your AI assistant for it

With the Animorio connector, Claude Code, ChatGPT, Cursor or VS Code take it from here, guide included, and fit it into your project. Paste this request:

Take “Self-formatting phone field” from Animorio and fit it into my site
Platform licenseFree on your sites and your clients’ sites. Want it inside a website builder, a template platform or a client dashboard? There’s a license for agencies and platforms.

What it does

On a very light grey background, in the middle, the phone number field of a sign-up: the label Phone number, a white pill with a thin border and a wide shadow, on the left the country in a small grey tag (US) with the +1 code and the menu arrow, a thin rule, and the example number in light grey; below, the help line We’ll text you a six-digit code and the Send code button, grey and off. A fake pointer arrives and taps the field: the focus ring widens and the thin caret blinks. It types 415555013: each digit rises from below on a spring, the opening bracket arrives with the first one, then the closing one, the space and the dash open up between the groups and the digits already typed slide aside to make room; the help line counts 1 digit to go. It presses enter: the field shakes sideways, turns red and says Enter all 10 digits for United States. It types the last digit: the red goes, the border turns green, a green circle grows on the right with its check drawing itself and Send code turns black with a little bounce. Pressed it squeezes, the label lifts away, a loading ring spins, a check draws itself and Code sent arrives; the help line says which number it went to. Then the pointer opens the country menu, a white card that springs open with six countries and a grey bar following the pointer, and picks United Kingdom: the code rolls from +1 to +44, the tag changes and the pill widens; it types 7700 900123 and the number turns green. Last, it types a plus followed by 39 inside the field: the country switches to Italy by itself and the number groups as 312 345 6789. Then everything clears and starts again.

How it works

A real field under the drawn digits

Underneath is a real input type="tel", transparent, with its caret hidden: it takes the keyboard, copy and paste and the phone’s keypad. On every input event its value becomes the state (the country and the digits), and on top, in a layer that ignores the pointer, the digits are drawn one by one in spans with fixed-width figures.

The country’s format

Each country has its pattern, like (###) ###-#### or #### ######: walking the pattern, each # takes a digit, while separators only appear when the digit after them arrives. Every piece has a key, its position and its digit, so when the number is rebuilt we know who stays, who arrives and who leaves.

FLIP on the sliding digits

Before rebuilding the number we measure where each span is; afterwards, the ones that stayed start from their old place and return to zero on a short spring (back.out): when a bracket or a dash opens, the digits already typed slide aside instead of jumping. New ones rise by 11 pixels, deleted ones drop where they were, pinned in absolute position.

The plus that switches country

If the value starts with a plus we look for the countries whose code begins the digits, the longest first: when one is found the country changes, the code in the tag rolls (the old one goes up, the new one comes from below) and the pill’s width animates from the old size to the new; the digits after the code stay in the field.

The button’s states

With all the digits the border turns green, the check draws itself with stroke-dashoffset and Send code goes from grey to black with a bounce; with fewer digits sending shakes the field and the help line says how many are missing. Pressed, a timeline squeezes the button, spins the ring for SEND_TIME seconds, draws the check and writes Code sent.

Values to change

NameFileDefaultWhat it changes
START_COUNTRYscript.jsUSThe country the field starts from: US, GB, IT, DE, FR or JP.
TYPE_SPEEDscript.js1How fast the autopilot types.
SPRINGscript.js1How much the digits bounce as they arrive and slide (0 = no bounce).
SEND_TIMEscript.js1.1The seconds Send code spends sending.
VALIDscript.js#1f9d55The green of a valid number.
BUTTONscript.js#111113The colour of Send code when it can be pressed.

Try them in the Playground

When to use it

For signing up to an app, logging in with an SMS code, a shop checkout asking for a delivery number, booking a table or a room: anywhere an international phone number is asked for.

On phones

The same field, as wide as the screen minus the margins: the pill stays 52 pixels tall, the country menu opens under the tag and stays inside the screen. Tapping the field opens the number pad (inputmode tel) and tapping the tag opens the menu; pasting a number with a plus switches country here too.

With reduced motion

No fake pointer, no springs or slides: the digits fade in in place, the field does not shake but turns red, the code changes without rolling and the button goes from Send code to Code sent with a fade.

Pitfalls

  • Rebuilding the number from scratch on every digit would make everything jump: each span has a key (position and digit) and is reused, and only that way does FLIP know where the ones that stay should start from.
  • The real field has transparent text and caret but stays on top of the drawing: underneath it, a tap on the phone would not open the keyboard.
  • A one digit code can be the start of a longer one (4 for 44 and 49, 3 for 33 and 39): until a whole code is there the digits stay after the plus and the country does not change.
  • The tag’s width changes with the code: it is measured before and after and animated between the two, then clearProps leaves it free again.

Libraries

GSAP 3.15.0, Mona Sans (font variabile) 5.3.0.

Compressed with gzip, the three demo files weigh 9.0 KB; the libraries: GSAP 28 KB.

Testing

Tested on 25 October 2026

  • Desktop: Done
  • Phone: Done
  • Reduced motion: Done
  • Safari on iPhone: To do

License

Use it in your own projects and in your clients’ projects. Do not resell it or redistribute it as is. GSAP stays under its own Standard license. Full license.

All components