2

Whenever a dot '.' is typed in my input (type=number) field, I want it replaced by a comma ','.

$(box).find('input[type=number]').keypress(function(evt){
        if(evt.which==46){
            $(this).val($(this).val()+',');
            evt.preventDefault();
        }
    });

The event is well fired, but instead, the field gets totally empty. What's wrong?

EDIT

The reason why I'm doing this is that Chrome (latest version), contrary to the HTML5 recommandation, use comma and discard dot in input type=number. I'm currently only developing for Chrome cause I can't test my app somewhere else for the moment. Any comment on this (abnormal ?) situation would be appreciated.

6
  • It is recommended to use keyup event instead. Commented Nov 23, 2012 at 16:41
  • does a alert($(this).val()) display anything Commented Nov 23, 2012 at 16:41
  • 1
    input type="number" will not accept comma ,. You may have to change input type=text Commented Nov 23, 2012 at 16:51
  • @MuthuKumaran : actually that's what W3C says about type=number, but Chrome on the contrary only accepts commas, and delete dots and anything coming afterwards. That's why I wanted to do this in the first place. Commented Nov 26, 2012 at 16:03
  • @VisioN : keyup does not even fire the event. Commented Nov 26, 2012 at 16:06

6 Answers 6

3
$(this).val($(this).val() + ',');

This might work if you are typing and always want to put the ',' sign at the end of the text. But what if you put the cursor in the middle of the text and then press a '.' sign?

Sign up to request clarification or add additional context in comments.

1 Comment

1

Maybe your document isn't already ready:

jQuery(document).ready(function() {
  // your code
});

1 Comment

the OP specifically states "The event is well fired, but.."
0

You could just use a substring like so:

str.substring(0,str.length - 1) + ','

However this is probably not an ideal solution.

Comments

0

Your code seems to work while running it on jsfiddle:

$(document).find('input').keypress(function(evt){ 
        if(evt.which==46){
            $(this).val($(this).val()+',');
            evt.preventDefault();
        }
    });​

http://jsfiddle.net/cornel_gav/cDE3L/

1 Comment

It will work for input type=text but OP is using input type=number.
0

here we are my friend, don't know why, it seems the useful keycode is 190 instead of 110.

try :

$(document).ready(function(){

     $(document).find('input[type=number]').live('keydown',function(evt){
            if(evt.which == 190){
                $(this).val($(this).val()+",");
                evt.preventDefault();
            }
        });

});

and this for a live preview http://jsfiddle.net/RTEcJ/8/

take a look at keycodes and you can try in the input to check which is the keycode you need: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

8 Comments

I'm using jQuery 1.6, on() won't work. More importantly, with keyup, evt.which matches the keycode and not the charcode. Instead of 46, it will be 110 for numpad dot, and others for other ways to do it (depending on keyboard mapping).
try now please, then i really suggest you to update you jQuery vers.
@pHCito fixed now sorry for delay ;)
No matter if using .keypress, .keyup, .keydown, .bind(press/up/down), .live(press/up/down). Same behaviour.
@pHCito can you tell me the jquery version you are using? did you checked the jsfiddle? if you want to replace points with commas when writing down in the text input, it works greatly!!
|
0

To make the char replacement work at any cursor position, you could bind to the keyup event and replace the already inserted Character in this.val().

$(document).on('keyup','.myField"]', function(e,i){
    if(e.keyCode==107)
    {
        var start = this.selectionStart,
            end = this.selectionEnd;
        $(this).val($(this).val().replace(/\+/,'|'));
        this.setSelectionRange(start, end);
    }
});

1 Comment

This doesn't work as val() doesn't return anything in a number of browsers if the input is invalid...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.