1

Hi guys please help me with this.

i have some issue regarding some email validation that doesn't match my regex. What's my issues all about was i think are \n or \r but when i try

var count = $("input#email_address").val().length;

Got the result of 25 when copy from excel to my text box VS Manual input with the same text and the result is 24 characters.

var str = $("input#email_address").val().replace(/\r?\n/g, "");

i tried this and trim() and match to find \r \n or whitespaces but return null.

Bug Man
  • 21
  • 1
  • 5

2 Answers2

1

First remove all unreadable characters, just in case:

var str = $("input#email_address").val().replace(/[\x00-\x1f]+/g, "");

Then, you should also check if some special characters in the string are multibyte. In that case, you need a more elaborated approach, see:

String length in bytes in JavaScript

Community
  • 1
  • 1
Javier Rey
  • 1,539
  • 17
  • 27
1

Okay in case you encounter this. Here's the solution.

I checked the value of my input text by calling escape(str), and i got the result "%u200Btest@test.com" it should be "test@test.com" they called this zero width space(correct me if i'm wrong".

and here's the fix.

str = str.replace(/\u200B/g,'');

JavaScript remove ZERO WIDTH SPACE (unicode 8203) from string

Community
  • 1
  • 1
Bug Man
  • 21
  • 1
  • 5