1

problem

  • i was implementing user cannot enter special character in textbox.

  • script is working on ie, safari, chrome but mozilla treating as special character.

what i need

  • i need backspace so that user can edit textbox values.

html code

    <input type="text" id="school" name="school"/>

js code

          $("#school").bind('keypress', function (event) {

        var regex = new RegExp("^[a-zA-Z0-9]+$");
        var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
        console.log(key);console.log(flag);
        if (!regex.test(key) && flag=="false") {
          console.log("if");

          //jQuery("#errmsg").html(localStorage.getItem("datas")).show();

          event.preventDefault();
          return false;
        }
        else
        {
          console.log("else");
        }
        //alert(data);



      });

i came up with solution : http://jsfiddle.net/HGJb3/297/ now it works

afeef
  • 4,396
  • 11
  • 35
  • 65
  • 1
    Just catch it before regex test. http://stackoverflow.com/questions/9906885/detect-backspace-and-del-on-input-event – emstawicki Sep 01 '16 at 12:44
  • Additionally the reason it works in Chrome is because chrome does not fire the keypress event for a Delete key operation. – Daniel Lane Sep 01 '16 at 12:47
  • Note that even if you fix the backspace problem a keypress event handler alone can't stop the user from entering whatever values they like (because the keyboard isn't the only way to edit the field value). – nnnnnn Sep 01 '16 at 12:48
  • i have solved the problem now it works – afeef Sep 01 '16 at 12:59

0 Answers0