3

how to make date Auto Format in input textbox with javascript

when input yyyyMMdd format in textbox, that will automatically change to yyyy/MM/dd format

for example: if your type 20120427, that will change to 2012/04/27

2 Answers 2

5

you can try this javaScript Function and Call it from onChange event of your textbox

function dateFormat(el){
    value = el.value;       
    el.value = value.replace(/^([\d]{4})([\d]{2})([\d]{2})$/,"$1/$2/$3");        
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is great, but allows for dates like 0000/00/00, and 9999/99/99 which are invalid dates.
-2

First, you need to write a function to do the convert,

function convert(original){};

then listen to onkeyup evnets, and pass the value of the input to the convert function, set the input's value to its result

Comments

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.