Angular Emoji Validation RegExp

May 27, 2024 · 1 min readLucian Ciobanu
Tags:AngularRegExp

One of the crucial tasks in web development is validating user input, ideally on both front and back end. Today I've created a regular expression which allows all alphabet letters (any kind of alphabet), numbers and common symbols while signaling out all emojis in the list below.

This project being in Angular, I've made the check with a reactive form custom validator.

RegExp live demo: https://regex101.com/r/h5JFwf/1

export const Component {
  const invalidPattern: RegExp = /[^\p{L}^/^\p{M}\p{N}\p{P}\p{Z}\p{Cf}\p{Sc}\p{Sm}\s]/u
  //the g at the end of the pattern we see in the live demo is not needed in the code

  emojiValidator(): ValidatorFn {
    return (control: AbstractControl): ValidationErrors | null => {
      const invalidEmoji = this.companyNaemPattern.test(control.value);
      return invalidEmoji ? { invalidEmoji: { value: control.value } } : null;
    };
  }
}

Of course, the emojiValidator property needs to be included at formControl declaration.

List to test:

▓ railway?? → Cats and dogs I'm on 🔥 Apples ⚛ ✅ Vi sign ♛ I'm the king ♛ Corée ♦ du Nord ☁ (French) gjør at både ◄╗ (Norwegian) Star me ★ Star ⭐ once more 早上好 ♛ (Chinese) Καλημέρα ✂ (Greek) another ✓ sign ✓ Лорем ипсум ✪ (Cyrillic) ◄ शुभ प्रभात ◄ (Hindi) ✪ ✰ ❈ ❧ Let's get together ★. We shall meet at 12/10/2018 10:00 AM at Tony's.❉ € test. 🖤✨ !@#$%^&*()-=+"`|_ 生転著辻大強記伊高権今専 (Japanese)

Hope this helped!

Cheers,
Lucian

Picture of author

Web Development & Software Delivery