Is it possible to dynamically set the pattern attribute of a form input to correspond with the value entered in another input within the same form?

The issue at hand is pretty straightforward - I have a form with the usual email setup: an Email input along with a Confirm Email input, where the values must match for validation.

I'm curious if it's possible to dynamically set the pattern attribute on the Confirm Email input to be a regex pattern that corresponds to whatever the user types into the initial Email field during keyup or focusout events. Is this concept clear and feasible?

I can handle creating the event listeners for keyup/focusout, but I'm uncertain about crafting a suitable regex for this scenario, and whether dynamically generating the pattern attribute in this manner would actually work as intended.

Lastly, please refrain from suggesting jquery solutions, as my current project requires vanilla JavaScript implementation. ES6, typescript, etc. are acceptable alternatives, just not jquery. Thank you in advance!

Answer №1

It's important to note that a regex can simply be a standard string, making it quite straightforward. If you restrict illegal characters in the email input, there is no need to escape any characters for the regex. However, if you were to permit something like [ in the email, you would have to escape it as \[

const i1 = document.getElementById('1');
const i2 = document.getElementById('2');

i1.addEventListener('change', (ev) => {
  i2.setAttribute('pattern', ev.target.value);
});
<form>
  <input id="1" required pattern="[\w\-\+\.]+@[\w\-\+\.]+" title="Email ex. a@b" />
  <br />
  <br />
  <input id="2" required pattern="" title="Match email above"/>
  <br />
  <br />
  <input type="submit" />
</form>

The email regex provided seems sufficient, as it verifies the presence of an @ symbol and ensures no illegal characters are included. For thorough validation of emails, sending a confirmation link is recommended.

To understand the complexity of validating emails with a regex, refer to this discussion thread: How can I validate an email address using a regular expression?

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

How to Eliminate the Hashtag from the URL in AngularJS Version 1.x

I'm currently in the process of creating a web application using Angular 1.x (not Ionic, just a regular website). My goal is to eliminate the '#' from the URLs. I've already set html5mode to true and added a base tag within the head sec ...

Exploring the world of JMeter: capturing sessions with JavaScript and jQuery

I need to capture a user session in order to conduct a performance test. I have been using the JMeter HTTP(S) Test Script Recorder, but unfortunately it is not recognizing javascript and jquery. The error message I'm receiving is: JQuery is not def ...

Using TypeScript to import a Vue 2 component into a Vue 3 application

Recently, I embarked on a new project with Vue CLI and Vite, utilizing Vue version 3.3.4 alongside TypeScript. In the process, I attempted to incorporate the vue-concise-slider into one of my components. You can find it here: https://github.com/warpcgd/vu ...

Tips for preventing the need to input letters into a date selector field

Is there a way to prevent entering letters in a date picker field? I'm currently utilizing bsDatePicker I attempted using type="number", however I received an error message and was unable to choose a date "The specified value "02/03/2020" is not a ...

What is the best way to send the selected option from a dropdown to a button click function within the controller?

I need to pass the selected value from a user's country selection dropdown to the ng-click="saveChanges()" function on the submit button. Is there a correct method for achieving this? I want to be able to access the user's selection from the dro ...

Parsing XML to JSON using JavaScript

I came across this JavaScript function on Stack Overflow that I've been using to convert XML to JSON: function xmlToJson(xml) { try { var obj = {}; if (xml.nodeType == 1) { if (xml.attributes.length > 0) { ...

What is the best way to transform data from a single form into a JSON array?

explore (handlebars) {{!< main}} <form class="ui form" action="/design/add" method="POST"> <div class="order_list"&yt; <h1>Add Product</h1> <br> {{!-- <input type="f ...

Is there a TypeScript equivalent to C#'s generic type constraint that allows for extending a class?

I'm working on creating a protected abstract class that allows for the subclass type to be passed as a type argument in the constructor of the superclass. What I need is something similar to C#'s Generic Type Constraint (using the where keyword) ...

Retrieve JavaScript functions as JSON or text in AngularJS and execute them

Currently, I am working on developing an AngularJS application and have come across a particular challenge that needs to be addressed: After making a web service call, I receive JavaScript code that looks like this: { "script":"function foo(a, b) { c = ...

Sending SMS from an Angular application to mobile devices can be achieved through several methods

Does anyone have experience sending SMS from an Angular6 web application? I would appreciate any guidance, such as reference links or code samples. Thank you! ...

Whenever a user tries to retrieve a page using ajax and then proceeds to submit the form associated with that page, they encounter

In my addQuestions.php file, I have four options available - single, multiple, matrix, and true false type questions with values 1-4 assigned to each respectively. To dynamically load the appropriate question type based on user selection, I am using AJAX w ...

Preserving drop-down options in Ngx-Bootstrap Typeahead even after input is cleared

Recently, I updated my ngx-bootstrap from version 1.8.1 to 3.0.1 and encountered an issue with the typeahead functionality not working as expected. I am following this example: The issue arises when using [typeaheadMinLength]="3". When searching for "abcd ...

Exploring the possibilities of Vue.js and Bootstrap 4 on the PlayStation 4 internet browser

Are there any workarounds for achieving Vuejs and Bootstrap 4 compatibility on the Playstation 4's Internet browser? When I access my website, it only displays our background color and nothing else. I am open to suggestions. Is there a fix for this i ...

One limitation is that you cannot use JQuery to make multiple rows editable simultaneously

I have a unique challenge with implementing an edit button on each row of a dynamic Bootstrap table. I am attempting to toggle the button's icons and, depending on its current state, enable the corresponding row for editing. <td><button typ ...

A guide on creating a clickable button using react-router-dom's Link component in Material-UI version 5

Issue Currently, I am in the process of migrating from MUI v4 to v5 and encountering a problem while using the styled function to customize a Button component. Instead of Button, when I use MyButton, there seems to be an issue with the component prop acco ...

"Encountering a problem with Typescript when working with arrays

There are different types that I am working with type Asset = { id: string, name: string, recordedBy: string } type User = { id: string, name: string, dob?: Date } type Device = { id: string, name: string, location: [n ...

Sending information from React JS to MongoDB

I am currently facing a challenge in sending data from the front-end (react js) to the back-end (node js), and then to a mongodb database for storage. While I have successfully called the server with the data, I am encountering an issue when attempting to ...

React Ant Design: Encountered a warning for 2 elements having non-unique properties

I have incorporated the Carousel component, with each one containing a different component. One contains a form, while the other includes an external library component called react-input-otp. https://codesandbox.io/s/pensive-einstein-uucvm?fontsize=14& ...

Exploring the wonders of HTTP requests through pure pipes in Angular 5

I've been working on a pipe that converts currency values from one to another by making an HTTP call. The idea is to pass in the source and destination currencies as parameters. @Pipe({ name: "currencyConverter" }) export class CurrencyConverterP ...

Manipulate JavaScript programmatically using Python and Selenium

Is it possible to programatically set up a Javascript override on a webpage using Selenium (either Geckodriver or Chromedriver) and Python? I have the modified JavaScript saved on my local computer. The procedure outlined in this Stack Overflow article w ...