Mick Goorder
Mick Goorder

Reputation: 15

Remove only second character in jQuery

$('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.

Upvotes: 0

Views: 859

Answers (6)

orlenko
orlenko

Reputation: 1271

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?

Upvotes: 0

Jérôme
Jérôme

Reputation: 2090

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

Upvotes: 0

urraka
urraka

Reputation: 1017

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/

Upvotes: 0

Jude Osborn
Jude Osborn

Reputation: 1798

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

Upvotes: 2

Niccolò Campolungo
Niccolò Campolungo

Reputation: 12042

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.

Upvotes: 0

elclanrs
elclanrs

Reputation: 94101

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/

Upvotes: 2

Related Questions