0

Here is my regex for validating a e-mailadress. But it keeps removing letters with a apostrophe. For example (Hélen, becomes Hlen)

var firstname = $("#FirstName").val().replace(/å/gi, "a").replace(/ä/gi,"a").replace(/ö/gi, "o").replace(/[^a-z0-9\s]/gi, '');

var lastname = $("#LastName").val().replace(/å/gi, "a").replace(/ä/gi, "a").replace(/ö/gi, "o").replace(/[^a-z0-9\s]/gi, '');

$("#Mail").val(firstname + "." + lastname + '@customer.Email');
  • What is your question, what do you want your regex to do? Removing, replacing or keeping them? – Charly Apr 29 '16 at 08:57
  • 1
    That's not an apostrophe. – nnnnnn Apr 29 '16 at 08:59
  • 3
    `[^a-z0-9\s]` matches anything that isn't a letter between `a` and `z` or a number between `0` and `9`, and removes it. This is not the right way to validate an email. – adeneo Apr 29 '16 at 08:59
  • [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – adeneo Apr 29 '16 at 09:00
  • He is constructing email ids from firstname and lastname. The `replace(/[^a-z0-9\s]/gi, '')` is removing your é with "" therefore Hélen becomes Hlen – avck Apr 29 '16 at 09:02
  • Okay how should the Regex look like instead? – Tempest-Marie Norlin Apr 29 '16 at 09:08
  • The duplicate question shows you how to do what you want to do - removing accents and keeping the base letter, so the email address is as close to the real name as possible. – Tomalak Apr 29 '16 at 09:11
  • add checks for each of the accented letters as you have done for `å,ä,ö` like `replace(/é/gi,"e")` – avck Apr 29 '16 at 09:11

2 Answers2

0

I think that what you need is actually replace accented char with non accented version.

I found this solution here in SO: Remove accents/diacritics in a string in JavaScript

hope this helps

Community
  • 1
  • 1
rick
  • 1,869
  • 13
  • 22
0

With : replace(/[^a-z0-9\s]/gi, '') you are replacing anything that isn't a non-accented letter , by an empty string. That's why it is erased.

For that not to happen, you have to do with "é" the same thing you do with "å" or "ä", replace it with the non accented letter.

For example to replace "é", "è", "ê" and "ë" by e you can use replace(/[éèêë]/gi, "e") before using replace(/[^a-z0-9\s]/gi, '').

You'll have to do the same with "à" and "ô" etc ...

Ekusu
  • 13
  • 4