0

How to validate or mask some special characters while giving input in JavaScript/ JQuery

Suppose in password use of _ and & is allowed, So all other special characters when I use as an input show validation message immediately or should not be typed (get masked)

Lovel_leo
  • 15
  • 4
  • Maybe you can find an answer checking this question http://stackoverflow.com/questions/895659/how-do-i-block-or-restrict-special-characters-from-input-fields-with-jquery – Bojan Petkovski Oct 08 '14 at 13:53

1 Answers1

1

Whell Validation and masking are two different things. When you want to validate, you simply add onkeyup event

$('input').keyup(function(){
  var inputText = $(this).val();
  if ( !/[a-zA-Z0-9]+/.test(inputText) ){ // put your reg exp here, this will require only letters and numbers
    console.log('Display error');
  }else{
    console.log('Hide error');
   }
});

When you want to mask your characters, you need to create seocnd hidden input, next to your input and copy your input value. After you cope input value, you can replace spacial characters with some char and place it inside of your input.. This one is more complicated than first case.

Most ppl would advise to use validation.

Just remember not to do mutch operations here, if you are worried on performance, and you can accept validation on value change, I would advise onValueChange event instead.

Beri
  • 11,470
  • 4
  • 35
  • 57
  • If you want validation to take place fast, use onkeyup, if you want to be less damanding, add onchange event – Beri Oct 08 '14 at 13:55