Guide to dynamically adjusting the input type

Within my Vue page, there are three distinct input fields:

<input
  id="id"
  class="flex-grow bg-white p-4 text-sm items-center focus:outline-none"
  type="text"
  name="id"
  v-model="id"
  placeholder="Enter your email or phone number..."
/>

<input
  v-show="phone"
  id="phone"
  class="flex-grow bg-white p-4 text-sm items-center focus:outline-none"
  type="tel"
  name="phone"
  v-model="phone"
  required
  placeholder="Enter your phone number..."
/>

<input
  v-show="email"
  id="email"
  class="flex-grow bg-white p-4 text-sm items-center focus:outline-none"
  type="email"
  name="email"
  v-model="email"
  required
  placeholder="Enter your email..."
/>

The initial "id" field is displayed by default on the page. A watcher function monitors the value of "id". If it's a string, it switches to the email field.

If the value is numeric, the tel field replaces it.

This is the watcher in action:

watch: {
  id(newVal, oldVal) {
    if (this.startsWithLetter(newVal)) {
      this.$nextTick(() => {
        this.phone = null
        this.email = newVal
        this.$el.querySelector('#email').focus()
      })
    } else {
      this.$nextTick(() => {
        this.email = null
        this.phone = newVal
        this.$el.querySelector('#phone').focus()
      })
    }
  },
},

Although this functionality works, there is an issue with the focus not being set properly when the new field is switched. The soft keyboard does not appear as expected.

Answer №1

I'm a bit uncertain about the goal you're aiming for with this code. It's a little complex for me to grasp at the moment.

However, here's a straightforward example showcasing how to create a dynamic input type based on the content entered:

<template>
  <div>input: <input v-model="userInput" :type="phoneOrText" /></div>
</template>

<script>
export default {
  data() {
    return {
      userInput: '',
    }
  },
  computed: {
    phoneOrText() {
      return /\d/.test(this.userInput) ? 'tel' : 'text'
    },
  },
}
</script>

The input field will display as text if it contains only text and switch to tel if any numbers are present, thanks to the /\d\ regex (which matches any digit). I used password in the screenshots for better visibility, but it can be customized further. The tel input works well on phones without requiring manual focus() or selection.

I'm not sure if this explanation clarifies things for you, but feel free to ask for more guidance if needed. Here are some reference images for visual aid: https://i.stack.imgur.com/kewqk.png https://i.stack.imgur.com/D6aQA.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jinja2.exceptions.UndefinedError: The variable 'asset' has not been defined

Currently in my project, I am using a Python backend to fetch data from an API and then rendering it through Flask to the Vue.js frontend. However, I have encountered an error titled that is causing some issues. I have double-checked and printed the varia ...

What is the most efficient method for transferring Flask variables to Vue?

I am currently developing a visualization application using a flask server and vue.js for the front end. Other discussions on this topic explore how to avoid conflicts between vue.js and flask variable syntax, as shown here. In my scenario, I'm inte ...

Executing a python script on the client side with the help of Vue.js

Because of restricted computation capabilities, I need to execute a python script on the user's browser. Currently, my website utilizes Vue.js for the frontend and Django for the backend. Do you have any suggestions on how I can specifically run thi ...

An error occurred: gyp ERR! stack - The command `python -c import sys; print "%s.%s.%s" % sys.version_info[:3]` has failed

While attempting to npm install in a Vue project, I encountered an error even after running vue create (name): npm ERR! gyp verb check python checking for Python executable "c:\Python310\python.exe" in the PATH npm ERR! gyp verb `which` ...

The Selenium test functions correctly in the production environment, however it encounters issues when run

I am facing an issue with my Vue.js application while writing Selenium tests. The test passes when run against the deployed production version of the app, but fails when run against a local version. When running the app locally, it is not from a build, bu ...

Flask static serving causes Vue app to not render

Currently, I am in the process of developing a basic web application utilizing Flask for the backend and Vue for the client side. Here is an overview of my folder structure: . ├── client/ │ ├── dist/ │ │ ├── css/ │ │ ...

Django Vue3 encounters access-control-allow-origin restriction

I am currently working on a Django rest-api project that uses Vue on the front-end. Unfortunately, I encountered an error while making requests via Vue: Console output: The following error is displayed: Access to XMLHttpRequest at 'https://api.iyziw ...

Solving the CORS problem between Vue.js and Flask: Troubleshooting XMLHttpRequest Blockade

Description: Currently working on developing a web application utilizing Vue.js for the frontend and Flask for the backend. The initial phase involves creating a simple login page, but encountering CORS (Cross-Origin Resource Sharing) issues when making r ...