1

I need to remove japanese from text input box using jQuery, but somehow this is not working

My code:

$('#email').keyup(function () {
    this.value = $(this).val().replace(/[^a-zA-Z0-9!.@#$%^&*()_-]/g,'');
});

// try my-email@yopmail.com確認用
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="email" id="email" size="50" />

If I type japanese after original email: (my-email@yopmail.com確認用) or after @ symbol:(my-email@確認用) then code isn't working. It doesn't replace japanese. I don't know the reason.

Phil
  • 157,677
  • 23
  • 242
  • 245
Luther Keng
  • 41
  • 1
  • 3
  • 1
    `'my-email@yopmail.com確認用'.replace(/[^a-zA-Z0-9!.@#$%^&*()_-]/g,'')` works for me. – nnnnnn Apr 11 '17 at 03:40
  • 1
    Your code seems all working, on what event it breaks ? Like while pasting, typing or setting value from some function, or ... ? – Gaurav Gandhi Apr 11 '17 at 03:53
  • 1
    i set input type is "email", if i change type to "text" then code working. Maybe error occurs when input type is email. You can test this case. Thank you – Luther Keng Apr 11 '17 at 04:10
  • 1
    Use the `input` event, some browsers (at least FF) do not fire the keyup event on character keys (it does only on special keys) so `$('#email').on('input', func...` should do. – Kaiido Apr 11 '17 at 04:53
  • 1
    Thanks for clarifying. The issue does appear to be around the `type="email"`. When I run this, I get an initial value of `my-email@yopmail.xn--com-es1hj6mio6a` – Phil Apr 11 '17 at 05:30
  • Possible duplicate of [*Input type email value in Chrome with accented characters wrong*](http://stackoverflow.com/questions/32117497/input-type-email-value-in-chrome-with-accented-characters-wrong) – Phil Apr 11 '17 at 05:32

1 Answers1

-1

Try this instead:

$('#email').keyup(function () {
   this.value = $(this).val().replace(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\
   [\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]
   {1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,'');
});

This is a regular email expression! Hope it helps

Huby03
  • 801
  • 2
  • 14
  • 28
  • 1
    Did you test this on the OP's input? (Or at all?) As soon as the user has entered a valid email address that matches that pattern the whole address will be replaced by an empty string. – nnnnnn Apr 11 '17 at 03:43
  • I used jquery validate to check email already. My goal is to remove japanese character from text input box – Luther Keng Apr 11 '17 at 03:45