Reputation: 21
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.
Upvotes: 1
Views: 334
Reputation: 21
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 "%[email protected]" it should be "[email protected]" 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
Upvotes: 1
Reputation: 1620
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
Upvotes: 1