Date Formatting Textbox

a place to get help

Moderator: 1.2 Moderators

Forum rules
  • Every time you need help create a new thread (but please use the search function before, as your problem maybe has already been solved)
  • use code highlighting (help)
  • After your problem has been solved you have to rename the subject to "[solved] <Topicname>" AND set the Topic icon as solved (the green check)

Date Formatting Textbox

Postby TJNevis on Sat Nov 15, 2008 7:48 pm

Hey everyone,

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.

  1. $$(".formatDate").addEvent('keyup', function(event) {
  2.         var txtbox = event.target;
  3.         if (event.key != 'delete' && event.key != 'backspace') {
  4.             txtbox.value = txtbox.value.format_date();
  5.         }
  6.  
  7.     });
  8.  
  9.  
  10. String.implement({
  11.         format_date: function() {
  12.             var newDate = this.replace(/[^\d]/g, "");
  13.             var dateMatches = newDate.match(/^(\d\d)(\d{0,3})(\d{0,4})/);
  14.             if (dateMatches[1].length > 0) {
  15.                 newDate = "(" + dateMatches[1];
  16.             }
  17.             if (dateMatches[1].length == 3) {
  18.                 newDate += ") " + dateMatches[2];
  19.                 if (dateMatches[2].length == 3) {
  20.                     newDate += "-" + dateMatches[3];
  21.                 }
  22.             }
  23.             return newDate;
  24.         }
  25.     });


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
TJNevis
 
Posts: 6
Joined: Sun Oct 26, 2008 7:28 pm

Return to Help

Who is online

Users browsing this forum: No registered users and 3 guests