I have a form with a field of date in which the user has to type the digits and the dots will automatically format the value. Ex: user tipes 056, when he presssed on 6 a dot after 5 was added. I want to make it in format dd.mm.yyyy but since it is just text input I am working on right now I do not have to define these three atributes. My code so far:
$(document).ready(function(){
$('#date').keyup(function(event){
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
var $this = $(this);
var num = $this.val().replace(/,/gi, "").split("").reverse().join("");
var num2 = RemoveRougeChar(num.replace(/(.{2})/g,"$1,").split("").reverse().join(""));
console.log(num2)
$this.val(num2);
if(convertString.substring(0,4) == ".") {
return convertString.substring(2, convertString.length) }
return convertString;}
When I type the first two digits a comma appears in front of the value, just after I type the third one the comma is moved. How can I write the code so that the comma is replaced by a dot and added twice: first after day digits and second after month digits?
You can use script i wrote for this purpose:
Save this as dateFormatter.js
In your html connect it before(!) any other scripts except jquery. In other script, where you do all stuff just add line:
var dtFormat = dateFormatter($selector1, $selector2, etc);
Change $selector1 with your selector (in my case it was input id #changesDate). You can pass multiple 'selector' parameters.
Example : http://embed.plnkr.co/2ahM90nNlhgRH4yCD6Y2/preview