Validation of emails containing unicode characters in Angular JS

Currently, I am dealing with a sign-up form for an application where AngularJS is handling the validation process.

An obstacle arose when AngularJS refused to accept an email address containing an apostrophe, for example: "Pear'[email protected]".

Upon investigation, it was revealed that AngularJS has issues with unicode characters in email addresses.

If anyone else has encountered this problem before, I would greatly appreciate any advice on resolving this bug within AngularJS.

Your insights and suggestions will be highly valued. Thank you!

Answer №1

If it's not absolutely necessary to have html5 <input type=email />, you have the option of using <input type=text /> with pattern validation

 <input type="text" ng-model="field" ng-pattern="EMAIL_REGEXP" />

Additionally, you can implement the regex provided by @Andy Joslin in his answer

 $scope.EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;

Answer №2

To verify an email address, AngularJS utilizes a specific regular expression. You can create your custom directive by following the same approach and modifying the regular expression to suit your requirements. Here's how you can do it:

Simply copy the directive code from AngularJS and adjust the regex pattern as needed: https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L606-L614

myApp.directive('nanuEmail', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elm, attrs, model) {
      //modify this:
      var EMAIL_REGEXP = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
      var emailValidator = function(value) {
      if (!value || EMAIL_REGEXP.test(value)) {
        model.$setValidity('email', true);
        return value;
      } else {
        model.$setValidity('email', false);
        return undefined;
      }
      model.$parsers.push(emailValidator);
      model.$formatters.push(emailValidator);
    }
  };
});

Once done, you can use the directive in your HTML like so:

<input nanu-email ng-model="userEmail">

Answer №3

After making a simple update to the regex in the angular.js file by adding a single quote, everything started working smoothly without any further modifications needed.

With the inclusion of a single quote within the expression, EMAIL_REGEXP now reads /^[A-Za-z0-9._%+-']+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/ . I want to extend my gratitude to Vittore for suggesting this REGEX update. :)

Answer №4

What causes the return of undefined?

Here is a revised version of the function:

const validateEmail = function (email) {
  if ((typeof email !== "undefined") && email !== "") {
    return regex.test(email);
  }
};

Answer №5

In Angular, there is an issue with validating the apostrophe (') in email IDs. To address this, you can modify the regular expression as shown below:

(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)

Change it to:

/^[A-Za-z0-9._%+'-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/.

This adjustment should resolve the problem and allow for proper validation.

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

Locate commas within quotation marks using regex and replace them with their corresponding HTML entity

When dealing with a string like this: "Hello, Tim" Land of the free, and home of the brave I want it to be transformed into: "Hello&#44; Tim" Land of the free, and home of the brave This should be a simple task, but I'm struggling to find th ...

Is there a way to customize the look of the time selected in the <input matInput type="time" step="1" /> time picker?

Currently, I am utilizing the following code as a time picker in my Angular 9 application. My goal is to modify the selected time's color to a bright blue shade. How can I accomplish this task? <input matInput type="time" step="1&quo ...

Showing a pop-up dialog box once a value has been selected

When the ajax autocomplete value is selected in a certain textfield, I want to display a message box. To achieve this, I am utilizing the jquery-confirm JavaScript library which can be found here. Below is my code snippet: select: function( event, ui ) { ...

Implementing Table Updates in Yii2 using Ajax Calls

I've developed a JavaScript script and Controller Action to update a field when a button is clicked as shown below. Although I receive a success response, the table does not get updated. public function actionSetstarttime() { if (Yii ...

Convert req.query to an object while using Express with MongoDB

Attempting to filter my results from MongoDB using Express, I encountered the following console logs: req.query.filters from URL: http://localhost:3000/test?filters=%7Bpersonalbest%3A%7B%27%24gt%27%3A%27170%27%7D%2Cname%3A%7B%27%24gt%27%3A%27M%27%7D%7D G ...

Determining if an event is already associated with a marker in Google Maps API v3

I've set up a marker with a click event listener attached to it. However, I want to check if the click event has already been added to the marker, and if not, add the click event listener. // Add click event listener if it doesn't already exist ...

Set the style properties of one class to match the style properties of another class using JavaScript

I need to dynamically create a div using the Bootstrap panel for displaying it. The div's class will either be "panel panel-success" or "panel panel-danger" based on the server response, which can be either "success" or "failure." To assign the class ...

Tips on causing a forEach loop to pause for a regex substitution to occur

I have a project in progress for an email web app where I am working on replacing certain keywords (first name, last name, email) with the actual properties of the user. As of now, I am going through a list of recipients and modifying the email content to ...

Qt offers a powerful feature that allows the seamless exposure of any C++ object to Javascript

I am interested in making a C++ object accessible from my class to Javascript. Let me provide an example to better explain what I intend to do. In a specific project, I am working on creating a simple ShopApp. So how can I achieve something like this (wh ...

Looping through a set of API calls using JavaScript Web API

Currently, I am in the process of developing an application using angularjs and ionic. Within this app, I have an array containing IDs, and my objective is to retrieve their corresponding names. To achieve this, I attempted the following code snippet: var ...

LinkText is having trouble retrieving information from the UL user interface using angularjs

For some reason, the click event is not functioning properly when using Selenium WebDriver with an UL LI tag that has AngularJS. Below is a snippet of the code causing the issue: package automation.test; import org.openqa.selenium.By; import org.openqa.se ...

Displaying md-error within md-input-container using Angular without relying on Validators within the associated formControl

I am working on a form element with an input field <md-input-container class="margin-top-little"> <input mdInput placeholder="{{placeholder}}" [(ngModel)]="value" (blur)="onBlur()" name="{{name}}" [formControl]="validator" (change)="onCha ...

Flickering transitions in Phonegap and jQuery Mobile pages

As a beginner in Phonegap/jQuery Mobile, I have encountered a frustrating issue of a white screen appearing during page transitions. Despite trying various solutions found online, such as using -webkit-backface-visibility:hidden;, the problem persists. I ...

Is there a way to modify a CSS class in Chrome that is only visible when I perform a drag action?

For the website project I am working on, I have an element that only changes its style when I drag something over it. This is achieved by adding a CSS class to it. However, editing this style has proven to be challenging because I cannot live edit it in C ...

Is there a specific approach for linking together two AngularJs service calls and executing a function with the obtained data?

I am trying to chain two service calls together and then filter the data using a forEach loop. However, I am encountering a TypeError: "SocialMediaUserService.channelProfiles is not a function" error in Chrome. Interestingly, this code works perfectly fin ...

JavaScript method to clear a variable

Can JavaScript prototype be used to add a method to a variable that is undefined? For instance, we can use the following code: var foo = "bar"; String.prototype.doTheFoo = function(){ console.log("foo is better than you"); }; foo.doTheFoo(); This c ...

Javascript promises executing in a mixed-up sequence

Utilizing Javascript's native Promise, I created a modified version of fs.readFile called readFileAsync. This function reads and parses a JSON file, returning the object when resolving. function readFileAsync(file, options) { return new Promise(fun ...

Is there a way to prevent the back button from functioning in my web browser?

How can I prevent the back button from being used on my webpage? Can you provide a list of possible methods to achieve this? if($data->num_rows > 0){ while($row = $data->fetch_assoc()){ header('Location:../cashier.php&apo ...

Preventing links from functioning on dynamically generated web pages

I have implemented the following code to deactivate all links on a preview page: var disableLink = function(){ return false;}; $('a').bind('click', disableLink); While this successfully disables all static links, any anchor tags loade ...

Setting the state of a nested array within an array of objects in React

this is the current state of my app this.state = { notifications: [{ from: { id: someid, name: somename }, message: [somemessage] }, {..}, {..}, ] } If a n ...