- $$(".formatDate").addEvent('keyup', function(event) {
- var txtbox = event.target;
- if (event.key != 'delete' && event.key != 'backspace') {
- txtbox.value = txtbox.value.format_date();
- }
- });
- String.implement({
- format_date: function() {
- var newDate = this.replace(/[^\d]/g, "");
- var dateMatches = newDate.match(/^(\d\d)(\d{0,3})(\d{0,4})/);
- if (dateMatches[1].length > 0) {
- newDate = "(" + dateMatches[1];
- }
- if (dateMatches[1].length == 3) {
- newDate += ") " + dateMatches[2];
- if (dateMatches[2].length == 3) {
- newDate += "-" + dateMatches[3];
- }
- }
- return newDate;
- }
- });
I have this code to get the text boxes with the class "formatDate", adding a keyup event and attaching a string extender that is also below.
I found the string extender code, and I guess I am not sure how it works. It originally formatted a phone number as (xxx) xxx-xxxx. I changed a few things and now it formats (xx and that is all. I want it to format a date as mm/dd/yyyy, 2 digit month and day.
Can anybody help me with this? I have been searching for a few days and I have not found anything helpful, except a regex website, http://www.regexlib.com
Thank you!!
-TJ


