[英]Special dot masked input HTML-JavaScript
I'm trying to achieve a special masked input in HTML using jQuery and themask plugin from Igor Escobar ( http://igorescobar.github.io/jQuery-Mask-Plugin/ ) 我正在尝试使用jQuery和Igor Escobar( http://igorescobar.github.io/jQuery-Mask-Plugin/ )的themask插件在HTML中实现特殊的蒙版输入。
I need write only numbers[0-9] and the following conversions: 我只需要写数字[0-9]和以下转换:
input: 输入:
123456789 123456789
12345678 12345678
1234567 1234567
123456 123456
desidered output: 期望的输出:
123.456.789 123.456.789
12.345.678 12.345.678
1.234.567 1.234.567
123.456 123.456
Is possible achive this with that plugin? 可以通过该插件实现吗? Or exist another way to do it?
还是存在另一种方法? Thanks for reading :)
谢谢阅读 :)
EDIT: 编辑:
I did it using another plugin (Numeral.js): http://numeraljs.com/ 我使用另一个插件(Numeral.js)做到了: http ://numeraljs.com/
This is my working code: 这是我的工作代码:
$("#myinput").blur(function()
{
this.value=numeral(this.value).format('0,0[.]00').replace(/\,/g, '.');
});
But I do not like to be validated at the end, ie (onblur), is there any way to do this on the fly? 但是我不喜欢最后被验证,即(onblur),有什么方法可以即时进行吗? - That is, gradually validate (keypress).
-即,逐渐验证(按键)。
You probably don't need a library for that. 您可能不需要为此的库。 I kept jQuery but it's really used to select the input, so you definitely can ditch it quite easily.
我保留了jQuery,但实际上它是用来选择输入的,因此您可以很轻松地放弃它。
$("#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 the dots, split the string and reverse it
var a = this.value.replace(/\./g, '').split('').reverse();
// start from 3 and as long as there's a number
// add a dot every three digits.
var pos = 3;
while(a[pos] !== undefined){
a.splice(pos,0,'.');
pos = pos + 4;
}
// reverse, join and reassign the value
this.value = a.reverse().join('');
});
You can try it yourself here and see if it does the job. 您可以在这里自己尝试一下,看看它是否起作用。 Hope it helps.
希望能帮助到你。
EDIT : while the above code works it has some shortcomings, such as: 编辑 :虽然上面的代码有效,但它有一些缺点,例如:
As I needed it with full support for those cases I evolved it in a tiny script you can find on Github along with a demo and test spec. 当我需要针对这些情况的全力支持时,我将其开发成一个很小的脚本,您可以在Github上找到它以及演示和测试规范。
$("#myinput").on('keyup keydown blur', function() {
this.value=numeral(this.value).format('0,0[.]00').replace(/\,/g, '.');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.