Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date validation to DD-MON-YY

Status
Not open for further replies.

kuolung

Programmer
Sep 2, 1999
51
US
hello everyone,

does any of you know any codes out there to validate any types of entry date such as mm/dd/yyyy, ddmmyy, etc...to DD-MON-YY (07-MAR-01).

I found codes that do the other way around, but couldn't find one that does a conversion to DD-MON-YY, when you enter for example, mm/dd/yy

thank you so much!!!
 
this works:

<script>
function convertDate(field)
{
var str = field.value;
var months = new Array(&quot;JAN&quot;, &quot;FEB&quot;, &quot;MAR&quot;, &quot;APR&quot;, &quot;MAY&quot;, &quot;JUN&quot;, &quot;JUL&quot;, &quot;AUG&quot;, &quot;SEP&quot;, &quot;OCT&quot;, &quot;NOV&quot;, &quot;DEC&quot;);
var reg = /^\d{2}\/\d{2}\/\d{4}$/;
var res = /^\d{6}$/;
var ret = /^\d{2}-\w{3}-\d{2}$/;
if(reg.test(str))
{
var s = str.split(&quot;/&quot;);
s[0] = parseInt(s[0]);
if(s[0] <= 12)
{
field.value = s[1]+&quot;-&quot;+months[(s[0] - 1)]+&quot;-&quot;+s[2];
}
else{alert('please enter a valid date');field.focus();field.value = '';return false;}
}
else if(res.test(str))
{
var s = new Array(str.substring(0,2), str.substring(2,4), str.substring(4,6));
s[1] = parseInt(s[1]);
if(s[0] <= 12)
{
field.value = s[1]+&quot;-&quot;+months[(s[1] - 1)]+&quot;-&quot;+s[2];
}
else{alert('please enter a valid date');field.focus();field.value = '';}
}
else if(ret.test(str))
{
return true;
}
else
{
alert('please put in a valid date format.');
field.focus();
field.select();
return false;
}
}
</script>
<input type=&quot;text&quot; onblur=&quot;convertDate(this);&quot;> luciddream@subdimension.com
 
luciddream,

thank you very much. however, when i enter a date value into the text box (mm/dd/yy), it clears out when i hit enter, is there a way to have a date converted automatically right on the text box when i hit enter after entering???

thanksssssssssssss..
 
well, you could use it on submit of your form...


<form onsubmit=&quot;convertDate(this.fieldname)&quot;> luciddream@subdimension.com
 
thanks, you're great! i have one more question, hope you don't mind. that is, where do i change in the codes to have a year with only 2-digit instead of 4 right now (DD-MON-YYYY), i really want only DD-MON-YY. thanks a ton!!!!!!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top