简体   繁体   中英

JavaScript utf8 encoding or regex pattern with Unicode letters support?

I have been recently requested, to adapt an app's input, to support Unicode letters, on some of the inputs within the web app.

That app, already does some validation with regex, with the pattern html attribute. Like so:

<input required="true" pattern="[a-zA-Z0-9_\-]+" type="text" name="name">

Now, since I have to adapt some inputs to the new requirements, I was wondering what would be better to do?

or

Javascript is by definition completely in unicode (Except websites with non unicode encoding, but there the solution may still work), so just add letters you need to regexp. If you need to add them by charcode use \\x0000

I had decided to go for editing the regex option, since when my view starts being processed, there is a module that will set the pattern attribute, with the defined regex, on specific inputs.

So, it is better to edit the regex pattern and then set in on inputs when the view is loaded, then doing:

  1. Load view and set pattern attributes for each input
  2. Write js to analyze specific inputs, to see if they contain decoded Unicode characters
  3. If so, encode those characters while typing/before submit, since I have a regex pattern that doesn't allow such characters

Essentially, I'm saving me time on writing useless code, and most important, a lot of browser processing (it was going to be to much, for what it is needed).

I should've gone for this option since the beginning (duh!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM