-1

I want to define the input key up in English, Thai and a space character. Other characters will be deleted.

$("#username").keyup(function () {


if (this.value.match(/d/g)){


this.value = this.value.replace(/d+/g,'');


}

else {

}

});
  • Keyup: %'^%'& (delete)
  • Keyup : Jack Boo (ok)
  • Keyup : คน Boo (ok)
  • Keyup : คน%^+ (delete)
  • Keyup : คน452 (delete)

Help :))

frankosa
  • 15
  • 6
  • Why would you want to limit input? Ok, reworded, why would you want to limit input as it is happening? What stops a person from pasting whatever they want to your field? – GetSet May 03 '20 at 21:09
  • I want to limit the username to special characters and numbers. Space character, English character and Thai character will be released. – frankosa May 03 '20 at 21:14
  • Wouldn't it be easier to validate after the fact? That is when the field changes event `onchange` then you can check for validity? – GetSet May 03 '20 at 21:15
  • Are you talking about .change jquery function? – frankosa May 03 '20 at 21:17
  • Whatever the name is, my point is what purpose is served by preventing the user from typing whatever they want? You can validate after – GetSet May 03 '20 at 21:18
  • Use `.replace(/[^\sA-Za-z\u0E00-\u0E7F]+/g, '')`. See https://regex101.com/r/W4Dg3b/1 – Wiktor Stribiżew May 03 '20 at 21:31
  • Thank youu....... – frankosa May 04 '20 at 22:03

1 Answers1

0

Use input event, so you can also filter pasted text.

$("#username").on("input", function(e) {
  $(this).val(function(i, v) {
    return v.replace(/[^a-z\u0E00-\u0E7F ]/gi, "");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="username">
loumadev
  • 371
  • 3
  • 12