javascript - How To Add A Back Slash To The TextBox For Date Format -



javascript - How To Add A Back Slash To The TextBox For Date Format -

this javascript:

`

function validatedate(txt, keycode) { if(keycode==16) isshift = false; var val=txt.value; var lblmesg = document.getelementbyid("<%=lblmesg.clientid%>") ; if(val.length == 10) { var splits = val.split("/"); var dt = new date(splits[1] + "/" + splits[0] + "/" + splits[2]); //validation dates if(dt.getdate()==splits[0] && dt.getmonth()+1==splits[1] && dt.getfullyear()==splits[2]) { lblmesg.style.color="green"; lblmesg.innerhtml = "valid date"; } else { lblmesg.style.color="red"; lblmesg.innerhtml = "invalid date"; return; } //range validation if(txt.id.indexof("txtrange") != -1) rangevalidation(dt); //birthdate validation if(txt.id.indexof("txtbirthdate") != -1) birthdatevalidation(dt) } else if(val.length < 10) { lblmesg.style.color="blue"; lblmesg.innerhtml = "required dd/mm/yy format. slashes come automatically."; } }`

and textbox:

> <asp:textbox id="txtvalidate" runat="server" maxlength = "10" onkeyup = "validatedate(this, event.keycode)" onkeydown = "return dateformat(this, event.keycode)"></asp:textbox>

i want include slash within textbox typing date.. don't want generate slash while typing...it should static when page loaded.... possible without using ajaxtoolkit ???

i made simple illustration purpose:

var date = document.getelementbyid('date'); date.addeventlistener('keypress', function (event) { var char = string.fromcharcode(event.which), offset = date.selectionstart; if (/\d/.test(char) && offset < 8) { if (offset === 2 || offset === 5) { offset += 1; } date.value = date.value.substr(0, offset) + char + date.value.substr(offset + 1); date.selectionstart = date.selectionend = offset + 1; } if (!event.keycode) { event.preventdefault(); } });

here fiddle

javascript date textbox backslash maskedtextbox

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 -