jquery - Special dot masked input HTML-JavaScript -



jquery - Special dot masked input HTML-JavaScript -

i'm trying accomplish special masked input in html using jquery , themask plugin igor escobar (http://igorescobar.github.io/jquery-mask-plugin/)

i need write numbers[0-9] , next conversions:

input:

123456789

12345678

1234567

123456

desidered output:

123.456.789

12.345.678

1.234.567

123.456

is possible achive plugin? or exist way it? reading :)

edit:

i did using plugin (numeral.js):http://numeraljs.com/

this working code:

$("#myinput").blur(function() { this.value=numeral(this.value).format('0,0[.]00').replace(/\,/g, '.'); });

but not validated @ end, ie (onblur), there way on fly? - is, gradually validate (keypress).

you don't need library that. kept jquery it's used select input, can ditch quite easily.

$("#myinput").keyup(function(){ // prevent every character except numbers if(!this.value.slice(-1).match(/^[0-9]+\.?[0-9]*$/) ){ this.value = this.value.slice(0, -1); return; } // remove dots, split string , reverse var = this.value.replace(/\./g, '').split('').reverse(); // start 3 , long there's number // add together dot every 3 digits. var pos = 3; while(a[pos] !== undefined){ a.splice(pos,0,'.'); pos = pos + 4; } // reverse, bring together , reassign value this.value = a.reverse().join(''); });

you can seek here , see if job. hope helps.

edit: while above code works has shortcomings, such as:

it not work when copy/pasting it not allow moving arrow keys the cursor goes end of input, when inserting number in middle.

as needed total back upwards cases evolved in tiny script can find on github along demo , test spec.

javascript jquery html masking

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -