jquery - Special dot masked input HTML-JavaScript -
i'm trying achieve special masked input in html using jquery , themask plugin igor escobar (http://igorescobar.github.io/jquery-mask-plugin/)
i need write numbers[0-9] , following 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 dot every 3 digits. var pos = 3; while(a[pos] !== undefined){ a.splice(pos,0,'.'); pos = pos + 4; } // reverse, join , reassign value this.value = a.reverse().join(''); });
you can try 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 full support cases evolved in tiny script can find on github along demo , test spec.
Comments
Post a Comment