0
$('input').keypress(function(e){
    if(($(this).val().split('a').length - 1) > 0){
        console.log($('input').val());
        $('input').val($('input').val().replace('a', ''));
    }
})

jsfiddle: http://jsfiddle.net/Ht8rU/

I want have only one "a" in input. I check if length a > 1 and next remove "a" from input, but this not working good. I would like remove only second a from this input. One "a" is allow.

6 Answers6

2

Edit: Oh I see now... If you want to keep only the first a you can try this:

$('input').keypress(function(e) {
  var key = String.fromCharCode(e.which);
  if (/a/i.test(key) && /a+/i.test(this.value)) {
    e.preventDefault();
  }
});

Demo: http://jsfiddle.net/elclanrs/Ht8rU/6/


You have to check if the current letter being typed is a:

if (String.fromCharCode(e.which) == 'a')

But here's a simplified version. You don't need to use val() if you can use value, specially because it makes your code cleaner. Also you might want to check for A or a so a regex might be a better option. Here's the code:

$('input').keypress(function(e) {

    var A = /a/gi,
        letter = String.fromCharCode(e.which);

    if (A.test(letter)) {
        $(this).val(this.value.replace(A,''));
    }
});

Demo: http://jsfiddle.net/elclanrs/Ht8rU/3/

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    This creates issues when you type another letter besides 'a', however this is a problem with `.keypress()` (notice the value of `$(this).val()`). Might want to use `.change()` instead? – Dom Jun 15 '13 at 22:36
  • type `a s d f a` and problem should occur – Dom Jun 15 '13 at 22:37
  • What problem? This is what OP wants right? Or am I missing something? – elclanrs Jun 15 '13 at 22:38
  • "I would like remove only second a from this input. One "a" is allow." <-- your code removes the first one and leaves the last one, I believe it should be the opposite – Niccolò Campolungo Jun 15 '13 at 22:42
  • this solution lets you add as many a's as you want pasting it with `ctrl+v` – urraka Jun 15 '13 at 22:58
2

I suggest using preventDefault to stop the key from being pressed:

$('input').keypress(function(e) {
    if (e.keyCode === 97 && $(this).val().split('a').length > 1) {
        e.preventDefault();
    }
});

JSFiddle

Jude Osborn
  • 1,788
  • 16
  • 24
0

This code may seem long and without any usefulness, but it works.

$('input').keyup(function(e) {
    var e = $(this),
        val = e.val(),
        aPos = val.indexOf('a'),
        spl1 = val.substring(0, aPos + 1),
        spl2 = val.substring(aPos, val.length).replace(/a/gi, ''),
        v = spl1 + spl2;
    e.val(v);
});

Here is a working JSFiddle of this.

Niccolò Campolungo
  • 11,824
  • 4
  • 32
  • 39
0

I would try something like this. Not sure how well supported is the input event currently, though.

(function() {
    var elem = $('input');
    var value = elem.val();

    elem.bind("input propertychange", function(e) {
        if (elem.val().split('a').length - 1 > 1)
            elem.val(value);
        else
            value = elem.val();
    });
})();

http://jsfiddle.net/Ht8rU/8/

urraka
  • 997
  • 7
  • 9
0

When the user presses 'a' or 'A', you can check if there is one 'a' or 'A' already present, if there is one already then you don't add it to the input.

$('input').keypress(function(e){
    if ((e.keyCode === 65 || e.keyCode === 97) & $(this).val().match(/a/gi) !== null) e.preventDefault();
})

Updated jsFiddle

Jérôme
  • 2,070
  • 15
  • 21
0

Here's a modified version of your fiddle that works: http://jsfiddle.net/orlenko/zmebS/2/

$('input').keypress(function(e){
    var that = $(this);
    var parts = that.val().split('a');
    if (parts.length > 2) {
        parts.splice(1, 0, 'a');
        that.val(parts.join(''));
    } else {
        // no need to replace
    }    
})

Note that we only replace the contents of the input if we have to - otherwise, constant rewriting of the contents will make it impossible to type in the midle or at the beginning of the text.

If you want to further improve it and make it possible to type at the beginning even when we are replacing the contents, check out this question about detecting and restoring selection: How to get selected text/caret position of an input that doesn't have focus?

Community
  • 1
  • 1
orlenko
  • 1,251
  • 8
  • 11