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.

11
  • Are you trying to replace all, or just one time? Commented Oct 25, 2016 at 19:51
  • did i missed something here? im new to jquery and javascript. Commented Oct 25, 2016 at 19:51
  • @applecrusher just one time. Commented Oct 25, 2016 at 19:52
  • Try this, excel adds more tabs var str = $("input#email_address").val().replace(/[\r\n\t]/g, ""); Commented Oct 25, 2016 at 19:52
  • Take a look at the answers here. It might help stackoverflow.com/questions/1144783/… Commented Oct 25, 2016 at 19:53

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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 "%[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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.