Detect accentuation with regex in javascript on osx

I need some help because I have troubles with detection of accentuate characters.

I have an application (html, css, js) working on both PC and MAC. It contains some input fields with following properties :

  • Text has to be numbers only
  • It must contain only 8 characters (so 8 numbers)
  • Each input is verified right away

Since the HTML input must contain a specific number of character I can't use the type="number", so it has the form : <input type="tel" maxlength="8"

My js function is the following one :

onChangeUserId: function (e) {
  var theEvent = e.view.event;
  var key = theEvent.keyCode || theEvent.which;
  keyValue = String.fromCharCode(key);
  var regex = /[0-9]{1,8}/;
  if (key === 13) {
    // validation function
  } else if (!regex.test(keyValue)) {
    theEvent.returnValue = false;
    if (theEvent.preventDefault) theEvent.preventDefault();
  }
},

My problem is that this is working on Windows, but OSx seems to allow the following characters (which are all accents) : ^ ¨ `

I didn't make this code, I "just" have to fix this bug actually, so maybe I am missing some things? But I have searched for a loooong time on the Internet, tried many things (especially on the regex, but this part seems to be just fine in the end) and couldn't manage to find why I have this problem and how I can solve it :/

If some of you have the answer, I would be happy to hear it (I'm kind of desperate now é_è)

Thank you :)

EDIT

As vove suggested it here, I followed this stack link which leads me to this JSFiddle Demo . This demo works fine with my PC, but with my mac, it allows the characters ^ ¨ `, so even this example does not work for me :/ Is anyone else having the problem?

Answers


I stopped trying to solve my problem a while ago but from what I remember part of it was that my code was using "onKeyPressed" function but this is not called when those keys are pressed, neither is "onKeyUp" btw. So I had to use "onKeyDown" function instead to detect when the pressed of those specific keys.

I think this issue may interest other people.


You need to mark start and end of string in regex using $ and ^

your regex should be

/^[0-9]{1,8}$/

Example

/^[0-9]{1,8}$/.test("33333333") //returns true

and

/^[0-9]{1,8}$/.test("333333333") //returns false

Also, you need to test the entire input value, so replace your else-if with

else if (!regex.test(this.value)) {
  theEvent.returnValue = false;
  if (theEvent.preventDefault) theEvent.preventDefault();
}

You could use maxlength attribute set to 8 ; pattern attribute with RegExp \d{8} to prevent form submission if input value does not match eight digits; input event containing if condition with RegExp /\D/ utilizing RegExp.prototype.test() to check if input contains character that is not a digit; set value to empty string "" if it is not a digit

document.querySelector("input[type=tel]")
.addEventListener("input", function() {
  if (/\D/.test(this.value)) {
    this.value = "";
    alert("eight numbers required")
  }
})
<form>
  <input type="tel" pattern="\d{8}" maxlength="8" placeholder="input eight numbers" required />
  <input type="submit" />
</form>

Need Your Help

How to create word document in vb.net

vb.net

I need to create a word document at run time by using vb.net . how can I do that without using microsoft.office.interop? I created the excel file before, but don't know how use it for word . here i...

storm-deploy submit topology java.lang.NoClassDefFoundError

java amazon-web-services apache-storm apache-kafka

after running topology in local cluster I created a remote storm cluster (storm-deploy Nathan). i Have removed Storm jars from build path in eclipse before creating a runnable jar with "package