Merging two regular expressions for AngularJS form fields

How can I combine these two regex patterns for an AngularJS form field?

\d{4}([- ]*)\d{6,7} 

OR

[a-zA-Z0-9]{4,12}

I attempted to do this like so:

<input type="text" pattern="\d{4}([- ]*)\d{6,7} | [a-zA-Z0-9]{4,12}" class="form-control" ng-model="formData.username" required placeholder="username">

Answer №1

To find the alternative of two regular expressions, it is necessary to enclose both in parentheses:

<input type="text" pattern="(\d{4}([- ]*)\d{6,7})|([a-zA-Z0-9]{4,12})" class="form-control" ng-model="formData.username" required placeholder="username">

Answer №2

The issue lies in the fact that angular utilizes a single pipe symbol for filters, which cannot be used within templates. In most cases, this would typically represent a binary OR operation; however, your specific situation may encounter complications...

To address this, one solution is to establish a controller variable that stores your desired pattern and then reference this variable in the template:

<input type="text" ng-pattern="myctrl.pattern" class="form-control" ng-model="formData.username" required placeholder="username">

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

What is the best way to modify values within arrays of objects in JavaScript?

I need to update the values in an array of objects. The keys 1 and 2 are dynamic and pulled from a JSON file. var rows = [ { "1": "14", "2": "55", "named": "name1", "row&quo ...

Avoid duplicate calls to the same URL in node.js

I need to make multiple calls to the back-end with some of them targeting the same URLs. To optimize performance, I am caching the results. However, a problem arises when I call loadCached twice in quick succession with the same URL - it ends up triggering ...

Error: self has not been declared

We have come across an obstacle. "Reference Error: self is not defined' When attempting to utilize React-data-grid, the problem arises on the server side when building the nodejs application with webpack. The issue is noticeable in the generated b ...

Development versions of npm libraries

Lately, I came across a library called react-3d-components that offers some d3 react components with basic charts. It's an impressive collection of components. However, when trying to access the source code due to incomplete documentation, I found my ...

A step-by-step guide to showing images in React using a JSON URL array

I successfully transformed a JSON endpoint into a JavaScript array and have iterated through it to extract the required key values. While most of them are text values, one of them is an image that only displays the URL link. I attempted to iterate through ...

Looking to display an input field when a different option is chosen from the dropdown menu?

I'm currently utilizing ng-if to toggle the visibility of an input box. However, whenever I refresh the page, the input box automatically appears and then hides after selecting an option. Below is my code snippet. Any suggestions would be greatly appr ...

Skipping beforeRouteLeave in VueJS

In my Vue SPA, there is a component where I am using the beforeRouteLeave guard to prevent accidental page exits. However, within this component, I occasionally make an ajax request that, upon successful completion, needs to redirect the user to another lo ...

Sorting through JSON information based on specific attributes and criteria

Consider this JSON object: { 'name' : 'John', 'friends' : [ { 'id' : 1, 'name' : 'George', 'level' : 10 }, { 'id' : 2, 'name ...

Modify code on click using JavaScript

Here is some code I found for a custom style switcher: I am thinking about integrating it into my bootstrap dropdown button. The current code for the style switcher is as follows: <form id="switchform"> <input type="radio" name="choice" value= ...

The official sign-in buttons for Facebook, Google, and Twitter are all neatly aligned on the same row with a sleek and

https://i.sstatic.net/bXOMb.pngHello, I am currently working on integrating social media sign up functionality into my Oracle Apex login page. To achieve this, I am utilizing JavaScript APIs for various social media applications. However, I'm facing a ...

Switching button presses

http://jsfiddle.net/qVfy7/ HTML <div id='button'></div> <div id="mydiv">ko</div> JS $('#button').click(function () { $('#mydiv').text('ok'); }); CSS #button{ width:100px; he ...

How to Find and Count Duplicate Values in an Array using AngularJS

My form consists of 5 questions, each with 3 different answers to choose from. For example: q1. What is your favorite color? Radio button-1: blue Radio button-2: red Radio button-3: grey While most questions offer the same values (blue, red, grey), I am ...

Filtering JSON data becomes seamless with the use of AngularJS

Is there a way to pass JSON data obtained from services.js to filter.js for filtering purposes? For instance: JSON data: {"name" :"stackoverflow"} Services.js: Created a service to retrieve JSON data {"getjsondataservice" } Controller: Acce ...

What is the best way to transfer Flow type properties from one React component to another?

I'm in the process of developing a component that will wrap another component known as Button. The tricky part is that the library where Button is defined does not expose the type of its properties. In order to properly assign types to my component, ...

What is the best approach for sending a binary image post request to an API using nodejs?

I am receiving the image binary in this manner: axios.get("image-url.jpg") Now, I want to utilize the response to create a new POST request to another server const form = new FormData(); const url = "post-url-here.com"; form.appe ...

Angular JS has blocked the cross-origin request from $http

I have been working on implementing search suggestion functionality using a Suggestions API "". The implementation works fine with Ajax GET requests, but when I try to use it with Angular's $http service, I encounter an error in the console: Cross-Or ...

Go to the child router using the specified outlet

I am attempting to display the expanded item within a grid of components that are being rendered inside a named outlet. In order to achieve this, I have added children to my named outlet path. Currently using Angular 7, I have followed various guides from ...

What is the best way to trigger the jQuery-File-Upload event within this function?

Before uploading a file, I want to check the API first. $('.file_upload_button_wrapper').on('click', function () { // perform check here $.ajax({ url: '/?app=files&getfile=ajax%2Fupload.php', ...

Send the Post model along with 2 checkbox lists to the controller using Jquery's Ajax function

How can I efficiently send 2 lists containing values of checked checkboxes along with my model using JQuery Ajax from an EditorTemplate used as a partial view? Here's the code snippet: @model EsdpExport.View_Models.ProductLineCreateViewModel @using E ...

Send a PHP array to JavaScript to display a horizontal pop-up submenu on hover

Currently, I am engaged in the process of updating an old website. I have made modifications to the html, php, and switched from a MySQL database to an SQL database. All seems to be going well in these areas. My expertise lies in databases, PHP, SQL, and I ...