Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <span>Entrez une date</span>
  <input type="text" class="input-date-text input-placeholder" placeholder="jj/mm/aaaa"/>
  <div class="value">The value is : <span id="value"><span class="nothing">Nothing yet!</span></span></div>
</div>
              
            
!

CSS

              
                body {
  font-family: Ubuntu, sans-serif;
  font-size: 18px;
  color: #454545;
  text-align: center;

  .container {
    display: inline-flex;
    flex-direction: column;
    text-align: left;
    
  
    .input-date-text {
      font-family: Ubuntu, sans-serif;
      width: 10em;
      font-size: 2em;
      color: #676767;
      
      &::placeholder {
        color: #cdcdcd;
        font-size: 0.8em;
      }
    }
    
    .value {
      margin-top: 2em;
      
      .nothing {
        font-size: 0.9em;
        color: #898989;
      }
    }
  }
}

/*
 * Animation, blink
 */
@keyframes blink {
  0% {
    background: #9b0c0c;
  }
  70% {
    background: transparent;
  }
}

.blink5 {
  animation: 1s ease 1s 5 blink;
  background: transparent;
}

@import url('https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;700&display=swap');

$top: 6px;
$left: 15px;
$diff: 2px;
$gap: 10px;
$font-size: 2em;
$font-family: inherit;
$color-before: #a8a8a8;
$color-after: #64b5f6ff;
 
.pHWrap {
   position: relative;
}
.input-placeholder {
   line-height: 0;
   padding-left: $left;
   padding-top: $top;
   padding-right: 5px;
   padding-bottom: 10px;
   font-family: $font-family;
   font-size: $font-size;
 
   &::placeholder {
      color: $color-before;
   }
 
   + span {
      pointer-events: none;
      position: absolute;
      top: $top;
      left: 0;
      font-family: $font-family;
      font-size: $font-size;
      color: $color-before;
      padding-left: $left + $diff;
      padding-top: $top + $diff;
      padding-right: 5px;
      padding-bottom: 10px;
      transition: 0.2s;
   }
 
   &.focus {
 
      outline: 0;
      padding-top: $top + $gap;
 
      + span {
         color: $color-after;
         top: -7px;
         left: -10px;
         font-size: 0.8em;
      }
   }
}
              
            
!

JS

              
                /* For the example, fill the #value span */
const exampleValue = e => {
  const value = document.getElementById('value');
     if (e.target.value) {
       value.innerText = e.target.value;
     }
     else {
       value.innerHTML = '<span class="nothing">Nothing yet!</span>';
     }
}

/**
 * Check if element contains class input-date-text
 */
Element.prototype.isDateTextElement = function() {
   return this.classList.contains('input-date-text');
}

/**
 * frameNumber method, returns a string value not greater than max and not lower than min
 * @param  int max Max value: 12 or 31
 * @return string
 */
String.prototype.frameNumber = function(max, min) {
   max = max || 31;
   min = min || 1;
   const val = parseInt(this);
   let response = '';
   if (val < min) response = `${min}`;
   else if (val >= max) response = `${max}`;
   else response = `${this}`;
   return response.length == 1 ? `0${response}` : response;
}

/**
 * Keydown event Handler, allow only digits, backspace, shift, delete, arrows
 */
const authorized_keys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'Backspace', 'Shift', 'Delete', 'ArrowLeft', 'ArrowRight'];
document.addEventListener('keydown', e => {
   if (e.target.isDateTextElement() && ! authorized_keys.includes(e.key)) {
      e.preventDefault();
   }
});

/**
 * Input event handler
 */
document.addEventListener('input', e => {

   if (e.target.isDateTextElement()) {
      let day, month, year;

      // If backspace and last char is a slash
      if (e.target.value.slice(-1) == '/') {
         e.target.value = e.target.value.slice(0, -1);
         return;
      }

      // day
      if (e.target.value.length == 2) {
         day = e.target.value.frameNumber(31);
         e.target.value = `${day}/`;
      }
      // month
      if (e.target.value.length == 5) {
         day = e.target.value.slice(0,2).frameNumber(31);
         month = e.target.value.slice(3).frameNumber(12);
         e.target.value = `${day}/${month}/`;
      }
      // year
      if (e.target.value.length > 9) {
         e.target.value = e.target.value.substring(0, 10);
         day = e.target.value.slice(0, 2).frameNumber(31);
         month = e.target.value.slice(3, 5).frameNumber(12);
         const current_year = new Date().getFullYear();
         const one150before = current_year - 150;
         year = e.target.value.slice(6, 10);
         year = year.frameNumber(current_year, one150before);
         e.target.value = `${day}/${month}/${year}`;
         e.target.blur();
      }
      
     /* For the example, fill the #value span */
     exampleValue(e);
     
   }
});

/**
 * Focusout event handler
 */
document.addEventListener('focusout', e => {
   if (e.target.isDateTextElement()) {
      if (! /^\d{2}\/\d{2}\/\d{4}$/.test(e.target.value)) {
         e.target.classList.add('blink5');
         const value = e.target.value;
         setTimeout(() => {
            e.target.value = '';
            e.target.classList.remove('blink5', 'focus');
            // e.target.focus();
            exampleValue(e);  
         }, 4000);
      }
      else {
         e.target.classList.remove('blink5');
         exampleValue(e);  
      }
   }
});

/*
 * Input Effect
 */

Element.prototype.inputEffect = function() {
   const wrap = document.createElement('div');
   this.insertAdjacentElement('afterend', wrap);
   wrap.appendChild(this);
   wrap.classList.add('pHWrap');
 
   const span = document.createElement('span');
 
   const placeholder = this.placeholder;
   span.innerText = placeholder;
   this.insertAdjacentElement('afterend', span);
   this.placeholder = '';
 
   this.addEventListener('focus', e => {
      span.innerText = placeholder;
      this.classList.add('focus');
   });
   this.addEventListener('blur', e => {
     console.log('e.target.value: ', e.target.value); 
     if (! e.target.value) {
            span.innerText = placeholder;
            this.classList.remove('focus');
      }
   });
 
   // if value is already filled, add class `focus` to the input
   if (this.value) {
      this.classList.add('focus');
   }
};

[...document.getElementsByClassName('input-placeholder')].forEach(el => el.inputEffect());


              
            
!
999px

Console