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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" type="text/css" />

<div class="holder">
  <div class="generic_notification">
    <i class="icon"></i>
    <span class="txt"></span>
  </div>
</div>


<form id="form1" class="go-top">
  <h2>Form</h2>
  
  <div class="form_row">
    <input id="name" name="name" type="text" required data-msg="Please enter Your Name">
    <label for="name">Your Name</label>
  </div>
  <div class="form_row">
    <input id="email" name="email" type="email" required data-msg="Please enter a valid Email">
    <label for="email">Email</label>
  </div>
  <div class="form_row">
    <textarea id="message" name="message" required data-msg="Please a Message"></textarea>
    <label for="message">Message</label>
  </div>
    <a href="Javascript: validate();">Submit</a>
</form>

<form id="form2" class="go-right">
  <h2>Form 2 (to show that validation is locked to form1)</h2>
  <div class="form_row">
    <input id="name" name="name" type="text" required>
    <label for="name">Your Name</label>
  </div>
  <div class="form_row">
    <input id="phone" name="phone" type="tel" required>
    <label for="phone">Primary Phone</label>
  </div>
  <div class="form_row">
    <textarea id="message" name="phone" required></textarea>
    <label for="message">Message</label>
  </div>
</form>
              
            
!

CSS

              
                @import "compass/css3";

* {
  box-sizing: border-box;
}

html {
  font: 14px/1.4 Sans-Serif;
}

form {
  width: 320px;
  float: left;
  margin: 20px;
  > div {
    margin-top:20px;
    position: relative;
  }
  input, textarea {
    width: 100%;
    border: 2px solid gray; 
    background: none;
    position: relative;
    top: 0;
    left: 0;
    z-index: 1;
    padding: 8px 12px;
    outline: 0;
    &:valid {
      // Hides the label
      background: white; 
    }
    &:focus {
      border-color: blue;
    }
    &:focus + label {
      color: blue;
      font-size: 70%;
      padding: 1px 6px;
      z-index: 2;
      text-transform: uppercase;
    }
  }
  label {
    transition: // not padding
      background 0.2s,
      color 0.2s,
      top 0.2s,
      bottom 0.2s,
      right 0.2s,
      left 0.2s;
    position: absolute;
    color: #999;
    padding: 7px 6px;
  }
  textarea {
    display: block;
    resize: vertical;
  }
}

form.go-top {
  input, textarea {
    padding: 12px 12px 12px 12px; 
  }
  label {
    top: 0;
    bottom: 0;
    left: 0;
    width: 100%;
  }
  input:focus, textarea:focus {
    
  }
  input:focus + label, textarea:focus + label {
    top: -15px;
  }
}
form.go-right {
  label {
    top: 2px;
    right: 100%;
    width: 100%;
    margin-right: -100%;
    bottom: 2px;
  }
  input:focus + label, textarea:focus + label {
    right: 0;
    margin-right: 0;
    width: 40%;
    padding-top: 5px;
  }
}

.form_row > label.incomplete{color:red;}
.form_row.incomplete {
	background:#FDE19F;
}

/* ------------------------------------------------------------+ 
 												Notifications
+--------------------------------------------------------------*/
.holder {margin:10px;max-width:700px;}
.generic_notification						{height:0;overflow:hidden;position:relative;}
.generic_notification .icon			{margin-right:10px;font-size:1.5em;}
.generic_notification .txt			{display:inline-block;vertical-align:top;line-height:1.5em;}
.generic_notification.active		{overflow:show;height:auto;border-radius:7px;border:1px solid; margin:10px 0px; padding:15px 10px 15px 10px; background-repeat:no-repeat; background-position:10px center;}
.generic_notification.info			{color:#00529B;background-color:#4CC1BD;color:#fff;}
.generic_notification.success		{color:#4F8A10;background-color:#DFF2BF;}
.generic_notification.warning		{color:#9F6000;background-color:#FEEFB3;}
.generic_notification.error			{color:#D8000C;background-color:#FFBABA;}
              
            
!

JS

              
                $(document).ready(function(){
  markupValidation("form1");
});

/* 
	This is called when we hit Submit we call the validation function with the formID, and provide a call back function to 
	take the error messages (data-msg). If the form is not valid then we use the notification box.
  we could easily put the erDesc string into the notification box instead which would list the missing fields based on each
  elements data-msg attribute but we are not using this in the example.
*/
function validate(){
  validateDynamicForm("form1",function(isValid,erDesc){
      if(isValid){
        alert("form valid");
      }else{
        // here i show a notification box but you could
        // easily alert the erDesc variable for a list of 
        // data-msg values for missing form fields
        if($('.generic_notification').length){
          resetNotification('generic_notification');
          setNotification('generic_notification','active warning','Required fields missing, please see below<br>' + erDesc,'fa fa-exclamation-circle');
          $('html, body').animate({
            scrollTop: $(".generic_notification").offset().top
          }, 1000);
        }else{
          // a fall back
          alert('Required fields missing, missing fields have been marked in red');
        }
      }
    });
}


// here we have the function that adds the * to required fields.
function markupValidation(formID){
  var fields = $('#' + formID + ' INPUT, #' + formID + ' SELECT, #' + formID + ' TEXTAREA'); 
  for(var i=0; i < fields.length; i++) { 
    var $fieldID =  fields[i].id;
    var $label = $('#' + formID + ' LABEL[for=' + $fieldID + ']');

    if(fields[i].hasAttribute('required')) { 
      $label.text($label.text().replace('*',''));
      $label.text($label.text()+'*');
    }

    if(fields[i].hasAttribute('data-lbl')) { 
      var $dataLbl = $('#' + $fieldID).attr('data-lbl');
      if($dataLbl !== undefined){
        $label.text($label.text().replace($dataLbl,''));
        $label.text($label.text()+$dataLbl);
      }
    }
  }
}

/*  --------------------------- Notifications --------------------------- */
function resetNotification(notificationClass){
  $('.' + notificationClass).removeClass('active warning info success error');
  $('.' + notificationClass).find('.icon').removeClass().addClass('icon');
}

function setNotification(notificationClass,classNames,textHtml,iconClass){
  $('.' + notificationClass).addClass(classNames).find('.txt').html(textHtml);
  $('.' + notificationClass).find('.icon').addClass(iconClass);
}
/*  --------------------------- //Notifications --------------------------- */


function validateDynamicForm(formID,callback){
  /*
    -----------------------------------------------------------------------------------
     Validation Routine, loops through items with a "required" attribute.
    -----------------------------------------------------------------------------------
    Passed Values:
      formID - locks validation to within a certain fromID.
      callback - a function that gets form valid and the error description passed back.

    Form Attributes / Usage:
      data-lenth	- for text areas that need restricted character length
      data-msg		- return message if field fails validation
    -----------------------------------------------------------------------------------
  */

  var $formIsValid	 = true;
  var $formErMsgs		= "";
  var fields				= $('#' + formID + ' INPUT, #' + formID + ' SELECT, #' + formID + ' TEXTAREA'); 

  for(var i=0; i < fields.length; i++) { 
    if(fields[i].hasAttribute('required') && !fields[i].hasAttribute('disabled')) { 
      var $fieldID =  fields[i].id;
      var $fieldName =  fields[i].name;
      var $fieldType = fields[i].type;
      var $fieldLimit = "";
      var $fieldErrorDesc = "";
      var $fieldLabel = $('#' + formID + ' LABEL[for=' + $fieldID + ']').text();
      var $fieldLabelError = $('#' + formID + ' LABEL[for=' + $fieldID + ']');
      var $fieldRowError = $('#' + formID + ' LABEL[for=' + $fieldID + ']').closest('.form_row');
      $fieldLabelError.removeClass('incomplete');
      $fieldRowError.removeClass('incomplete');
      $fieldLimit = (fields[i].hasAttribute('data-length')) ? $('#' + formID + ' #' + $fieldID).attr("data-length") : "";
      $fieldErrorDesc =(fields[i].hasAttribute('data-msg')) ? $('#' + formID + ' #' + $fieldID).attr("data-msg") : "";
   
      switch($fieldType){
      case "text":
        if ($('#' + formID + ' #' + $fieldID).val() === '' && $('#' + formID + ' #' + $fieldID).is(':visible')){
          $formIsValid = false;
          $fieldLabelError.addClass('incomplete');
          $fieldRowError.addClass('incomplete');
          $formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
        }
        break;
      case "password":
        if ($('#' + formID + ' #' + $fieldID).val() === '' && $('#' + formID + ' #' + $fieldID).is(':visible')){
          $formIsValid = false;
          $fieldLabelError.addClass('incomplete');
          $fieldRowError.addClass('incomplete');
          $formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
        }
        break;
       case "email":
        if (validateEmail($('#' + formID + ' #' + $fieldID)) == -1){
          $formIsValid = false;
          $fieldLabelError.addClass('incomplete');
          $fieldRowError.addClass('incomplete');
          $formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
        }
        break;
      case "textarea":
        if ($('#' + formID + ' #' + $fieldID).val() === ''){
          $formIsValid = false;
          $fieldLabelError.addClass('incomplete');
          $fieldRowError.addClass('incomplete');
          $formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
        }
        break;
      case "select-one":
        if ($('#' + formID + ' #' + $fieldID + ' :selected').index() === 0){
          $formIsValid = false;
          $fieldLabelError.addClass('incomplete');
          $fieldRowError.addClass('incomplete');
          $formErMsgs = $formErMsgs + ' - ' + $fieldErrorDesc + ' <br/>';
        }
        break;
      default:
       //nothing
      }
    }
  }

  if (callback && typeof(callback) === "function") {  
    callback($formIsValid,$formErMsgs);
  }

}

function validateEmail(email) {
  var emailVal = email.val();
  if(typeof String.prototype.trim !== 'function') {
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g, ''); 
    };
  }
  if (emailVal !== '') {
    //return email.search(/.+@.+\..+/);
    emailVal = emailVal.trim();
    email.val(emailVal);
    return emailVal.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
  } else {
    return -1;
  }
}
              
            
!
999px

Console