Home > Software design >  ASCII value is different when I copy the # symbol from UI
ASCII value is different when I copy the # symbol from UI

Time:02-16

So I have a form that would need user to enter input contains two pound key ( ## )
For example, if user enter "ABC##1", it is valid

However, it only works when user enter the input by keyboard,
it does not work when user try to copy the values from the UI page, then paste to the input field.

The UI have rows of names that user can copy if they want, In this case, I copy RCP##1 from the UI, then I paste it into the input box, then it does not pass the validation.
select the text and copy

UPDATE:
I tried to debug by check the "#" ascii value, if I manually enter a "#", the acsii value is 35 which is good.
But If I copy one "#" from the UI, it somehow shows that the "#" has length of 2, and it has acsii value of 8203 and 35 Here's the code I use to check the ascii value :

let string = "​#"; // this pound key is copied from the UI page
console.log('length',string.length)
for (var i = 0; i < string.length; i  ) {
  console.log(string.charCodeAt(i));
}
// the output is 
length2
8203
35

Any one knows what is causing this weird behavior?

CodePudding user response:

The UI is not using ASCII. ASCII is an obsolete encoding; Unicode has been around since 1990. Most reasonable applications will be using some form of Unicode. Your program is using Unicode, not ASCII.

What you have there (in hex) is 200B followed by 0023, which is zero-width space followed by number sign. You copied two characters, not one -- understandable, since one of the two takes up no width at all.

In general, cutting and pasting from a web page should be approached with caution. As in this case, there are layout characters you probably don't want.

  • Related