The user login validation with regex seems to be malfunctioning

I am trying to ensure that the user login input only contains Latin and Russian letters, digits 0-9, and some specific symbols like dots. I have attempted using regular expressions for validation but so far none of them have been successful.

errors["error-name"] = '';
var nameRegex = '[a-zA-Z0-9А-Яа-я.+~_-!?*]/i';

if(!values['name'].match(nameRegex)) {
      errors["error-name"] += "Incorrect login<br>";
    }

Can anyone help identify what might be wrong with my approach?

Answer №1

In order to encompass all Russian letters, relying solely on the [А-Яа-я] range is insufficient. It is necessary to also include the letter [ёЁ] in the range as it is not covered by that particular set.

Additionally, when there is an unescaped hyphen positioned between literal symbols within a character class, it forms a range, and it is advisable to place it at the beginning or end of the character class for clarity.

If you want to impose limitations like ensuring there are at least N occurrences of something, anchored lookaheads need to be incorporated.

var nameRegex = /^(?=[^A-ZА-ЯЁ]*[A-ZА-ЯЁ])(?=[^0-9]*[0-9])[-A-Z0-9А-ЯЁ.+~_!?*]+$/i;

Below is the demonstration link

In this context, ^ anchors the pattern at the beginning of the string, $ anchors it at the end, (?=[^A-ZА-ЯЁ]*[A-ZА-ЯЁ]) necessitates at least one letter, while (?=[^0-9]*[0-9]) demands at least one digit.

Note that I excluded all lowercase letters because the case-insensitive modifier /i renders them unnecessary.

To exclusively match symbols from the specified list, employ a simple + quantifier:

var nameRegex = /^[-A-Z0-9А-ЯЁ.+~_!?*]+$/i;
                                      ^

If allowing an empty string, opt for * over +.

Answer №2

You have the option to utilize lookahead regular expressions for validation -

/^(?=.*\d)(?=.*[\wА-Яа-я.+~\-!?*]).+$/

In the provided regular expression, the initial lookahead (?=.*\d) verifies the presence of digits, while the subsequent lookahead (?=.*[\wА-Яа-я.+~\-!?*]) ensures the inclusion of ASCII and Russian characters alongside special symbols.

If you wish to enforce a minimum password length (e.g., 8 characters), the pattern can be adjusted as follows -

/^(?=.*\d)(?=.*[\wА-Яа-я.+~\-!?*]).{8,}$/

Please note: The range for matching Russian letters corresponds to what was mentioned in your inquiry. If further modification is needed, you may update the character set within the second lookahead condition.

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

Error 56 EROFS encountered when trying to save a file in Node.js filesystem every 2 seconds

I've set up a node.js environment on my raspbian system and I'm attempting to save/update a file every 2/3 seconds using the code below: var saveFileSaving = false; function loop() { mainLoop = setTimeout(function() { // update data ...

Looking for results with partial user input in search feature

I've developed a basic search program that retrieves and displays the first and last name of a user when their username is entered. While it works fine for exact matches in terms of characters and length, I want it to also return results if there are ...

Here is a way to trigger a function when a select option is changed, passing the selected option as a parameter to the function

Is there a way to call a function with specific parameters when the value of a select element changes in Angular? <div class="col-sm-6 col-md-4"> <label class="mobileNumberLabel " for="mobilrNumber">Select Service</label> <div cla ...

Placing elements in Chrome compared to IE

I'm currently attempting to position elements in two rows using mathematical calculations. One of the elements, thumb_container, is a div that is absolutely positioned. Within this container, I am dynamically loading and appending image thumbnails usi ...

The Vue app for logging in with Metamask is unable to communicate with the Metamask extension due to a lack of interaction between the store components and the frontend interface

After following a tutorial to create a Metamask login app with Vue, I ran into some issues. The code provided lacked a defined project structure, so I decided to organize it in my own Github repo here. However, despite compiling successfully, the button to ...

Is it possible to incorporate a button and a click event into this JavaScript function?

For over a day, I've been attempting to include three functionalities in this code, but have yet to succeed. 1° My goal is to insert a button on this page. I've attempted to create a standard button using HTML and CSS, but the fireworks displa ...

Generating a string indicating the range of days chosen

Imagine a scenario where there is a selection of days available to the user (they can choose multiple). The list includes Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, each with an associated number from 0 to 6. For instance, Sunday ...

A guide on verifying a phone number using just one character

I am looking to validate a phone number with only one character being allowed. For example, the format should be XXX-XXXXXXX where "-" is the only allowed character. Below is my validation function: function validatePhoneNumber() { if(addform.staff_m ...

Filtering an Array of Objects on the Fly in Vue.js

I'm currently working on a Vue.js app where I need to dynamically apply filter values to an Array of objects based on their field values. Each object in the Array has various fields that I want to filter by. The challenge is that each field can have m ...

What is the location within an object3d where I can access the dynamic point coordinates?

Watching one singular point rotate around the Y axis is quite intriguing. I am eager to witness the shift in X coordinate as it moves along its trajectory. Although the starting point remains unchanged, I wonder where the dynamic coordinates lie. Cou ...

Clicking on a different texture/image allows for the texture to change in Three.js

I am working on creating a 360 image view using Threejs. I have a total of 4 images, with one linked to Threejs and the other 3 displayed as thumbnails at the bottom of the page. When a thumbnail is clicked, the 360 image view should change accordingly. Y ...

Is there a way for us to determine the time at which the user last took a screenshot or photo?

I am currently developing a website using Django and I have a unique requirement. I need to access the last image that a user has taken on their phone, without the image being shared by anyone else. The photo must be captured by the user's device itse ...

Transforming an object containing methods into a string using Javascript specifically for Photoshop

I have a unique universal object that is essential for my Photoshop scripts. It contains various properties and methods like this: var Object = {}; Object.text = "this is some text"; Object.toolbox = new ToolBox(); // specialized object with its own funct ...

Encountered an issue while trying to create a database using JS, Node, and SQ

Although I have come across similar Stack questions addressing my issue, the provided answers don't seem to work for me. I want to create a small database to manage all of my storage items. After hearing that SQLite is user-friendly and lightweight, ...

Error message: `$injector:modulerr - Angular JS` - Indicates an

Currently, I am trying to delve into the world of Angular JS by taking the codeschool course "Shaping up with angular js". The instructor in the videos emphasizes the importance of wrapping code in function(){}. However, upon attempting to do so, an error ...

What is the best approach to preserving and retrieving a Three.js scene?

I've come across loaders for individual objects and elements, but is there a way to save and load entire scenes in three.js? ...

Should I implement this practice when developing an AJAX website? Is it recommended to enable PHP code within .html files, or should I switch to using .php files instead?

Query: I am interested in executing PHP within HTML documents to include HTML using PHP include();. Question: Would it be more beneficial to change .php to .txt for my AJAX-loaded pages and switch my .html files to .php? This approach might resolve the ...

Determine whether an input is currently in a "checked" state

I am working with this simple HTML layout: <fieldset> <input/> <label></label> <div class="toggle_box"> <div class="switch"></div> </div> </fieldset> My goal is to achieve the ...

How can one create a function that delays the execution of code until AJAX data is received

I developed a CKEditor plugin that fetches data via ajax to create RichCombo functionality. The plugin functions correctly, however, when there are multiple instances of the editor on a single page, each plugin ends up sending its own ajax request, leading ...

Is it possible to utilize react for constructing a static HTML, CSS, and JavaScript document?

As a front end content developer, my main focus is on creating content assets. I typically use vscode to build my html, css, and js files, and then upload these static files to the content management system. However, this approach doesn't give me the ...