2

I want to prevent the user from deleting any "/" character in a textbox when pressing backspace or canc.

For example:

If the textbox value is 2014/09/, and the user presses backspace, the "/" character will not be removed.

How can I do it in the keyup or keydown evenets?

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

   // any idea

});
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
SH.Developer
  • 167
  • 2
  • 3
  • 16

3 Answers3

4

You have three situations:

  1. The user presses backspace and deletes the char before the cursor
  2. The user presses canc and deletes the char after the cursor
  3. The user selects the text and presses backspace or canc and deletes the selected text

So you have to work around all these different situations.

This code will work:

$('#textareaId')[0].addEventListener("keydown", function(e) {
    var start = this.selectionStart,
        end = this.selectionEnd,
        value = this.value,
        key = e.keyCode;

    if (key == 8 && value[start-1] == '/') e.preventDefault();
    if (key == 46 && value[start] == '/') e.preventDefault();
    if ((key == 8 || key == 46) && value.substring(start, end).indexOf('/') != -1) e.preventDefault();

}, false);

WORKING EXAMPLE: http://jsfiddle.net/MeBeiM/7hdb3472/1

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • @AminJafari I didn't select the jQuery library, sorry. It works fine now. – Marco Bonelli Sep 05 '14 at 11:30
  • It works almost fine. The problem is when I completely select the textbox and type something will remove / also, but it works fine on select+delete. –  Mar 24 '15 at 11:03
0

I have done it using the string operations

Check my fiddle here http://jsfiddle.net/7ek11cfs/21/

$('textarea').keydown(function(e) {
   textValue = $(this).val();
});

$('textarea').keyup(function(e) {
 if(textValue.charAt(textValue.length-1)=='/')
 {
   $('textarea').val(textValue);
 }
});

Hope this helps

Khaleel
  • 1,212
  • 2
  • 16
  • 34
0

here you go => DEMO

(function ($, undefined) {
    $.fn.getCursorPosition = function() {
        var el = $(this).get(0);
        var pos = 0;
        if('selectionStart' in el) {
            pos = el.selectionStart;
        } else if('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength;
        }
        return pos;
    }
})(jQuery);

$("#textboxId").keydown(function(e){
    var currentPos=$(this).getCursorPosition();
    var lastChar=$(this).val().substr((currentPos- $(this).val().length)- 1);
    if(e.which==8 && lastChar.match(/^\//g) ){
        e.preventDefault();
        return false;
    }
});

I took a bit help from THIS ANSWER for the function to identify the current position of cursor.

NOTE that in oppose to all the other answers and ideas found, this will ACTUALLY work like a mask and will not be removed even if the cursor is in the middle of the text!

Community
  • 1
  • 1
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43