cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <form>
  <div class="left-column">
    <p id="message">
      <span class="bad">Some fields were not completed properly.</span>
      <span class="good">Form is ready to send.</span>
    </p>
    <h3>Instructions</h3>
    <p>Fill in the form and check for errors. While filling the form, note the 2px indicator to the right of the form. The indicator will turn green when the required conditions have been met.</p>
    <button id="error-trigger" type="button">Check Errors</button> 
  </div>
  <h1>HTML5 Validation</h1>
  <label for="name">Name - <em>Cannot be empty</em></label>
  <input id="name" type="text" required>
  <label for="email">Email - <em>Must be valid email from 'specific-domain.com'</em></label>
  <input id="email" type="email" required pattern="^[A-Za-z0-9._%+-]+@specific-domain.com$">
  <label for="phone">Phone - <em>Accepts Intl and USA formats (+99(99)9999-9999 or 999-999-9999)</em></label>
  <input id="phone" type="text" required pattern="[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}|\d{3}[\-]\d{3}[\-]\d{4}">
   
  <label for="form-message">Message - <em>Send a message or not, whatever</em></label>
  <textarea id="form-message"></textarea> 
</form>

            
          
!
            
              $success: #007621;
$required: #B00000;
$error: #DC0809;
$black: #000000;
$grey: #aaaaaa;

html, body{
  margin:0;
  height:100%;
  font-family: Helvetica, Arial, sans-serif;
}

body {
  background: rgb(244,239,241);
  background: -moz-radial-gradient(center, ellipse cover,  rgba(244,239,241,1) 0%, rgba(223,210,247,1) 100%);
  background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(244,239,241,1)), color-stop(100%,rgba(223,210,247,1)));
  background: -webkit-radial-gradient(center, ellipse cover,  rgba(244,239,241,1) 0%,rgba(223,210,247,1) 100%);
  background: -o-radial-gradient(center, ellipse cover,  rgba(244,239,241,1) 0%,rgba(223,210,247,1) 100%);
  background: -ms-radial-gradient(center, ellipse cover,  rgba(244,239,241,1) 0%,rgba(223,210,247,1) 100%);
  background: radial-gradient(ellipse at center,  rgba(244,239,241,1) 0%,rgba(223,210,247,1) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f4eff1', endColorstr='#dfd2f7',GradientType=1 );
}

.left-column {
  float:left;
  height:100%;
  width:25%;
  margin-right:20%;
}

form {
padding:20px;
  height:100%;
}

label {
  display:block;
}

input[type=text], 
input[type=email],
textarea {
  margin-bottom:20px;
  border: 1px solid $grey;
  padding: 5px;
  width:50%;  
  transition:border-color 500ms;
  
  /* Mark required fields */
  &:required {
  border-right: 2px solid $required;
}
  /*
  :required:valid will change the red required mark to green when requirement is met.
  good for input patterns:
  https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-pattern
*/
  &:required:valid {
  border-right: 2px solid $success;
}
  &.error {
    border: 1px solid $error;
  }
  
  &:focus {
    outline: 0 none;
  }
}

/* Laser beam */
.error-line {
  height: 0;
  position: absolute;
  width: 1px;
  background-color: $error;
  z-index: 1000;
  transform-origin: top left;
  transition: height 300ms;
}

#message {
  background-color: $success;
  color: #fff;
  padding: 10px;
  
  .bad{
    display: none;
  }
  
  &.error{
    background-color: $error;
    .bad{
      display: block;
    }
    .good {display: none;}
  }
}

em {
  font-size: small;
  color:$grey;
}

            
          
!
            
              var $message = $(document.getElementById('message'));

// Handle validation errors
if ($message.length) {
    triggerLasers();
}

$(document.getElementById('error-trigger')).click(triggerLasers);

// FIRE!
function triggerLasers() {
    console.log('pew pew');
    var errorDelay = 250,
        $invalidFields = $('input:required:invalid'),
        hasErrors = !!$invalidFields.length;
    $('.error-line').remove();
  
    if (hasErrors) {

        // select required fields that are currently invalid
        $invalidFields.each(function() {
            var error = $(this);
            setTimeout(function() {
                linkErrors(error);
            }, errorDelay);
            errorDelay += 250;
        });
    }

    $message.toggleClass('error', hasErrors);

}

// Show fields with validation errors
function linkErrors(error) {
    var linkLine = $(document.createElement('div')).addClass('error-line').appendTo('body'),
        $messageOffset = $message.offset(),
        errorOffset = error.offset();

    $messageOffset.left += $message.outerWidth();
    $messageOffset.top += $message.outerHeight();

    var originX = $messageOffset.left,
        originY = $messageOffset.top - 2,
        lineLength = Math.sqrt((errorOffset.left - originX) * (errorOffset.left - originX) + (errorOffset.top - originY) * (errorOffset.top - originY)),
        angle = 180 / 3.1415 * Math.acos((errorOffset.top - originY) / lineLength),
        inverseAngle = 180 - angle;

    // invert angle if to the right of origin
    if (errorOffset.left > originX) {
        angle *= -1;
    }

    // set linkLine origin
    linkLine.css({
        'top': $messageOffset.top,
        'left': $messageOffset.left
    });

    // draw line from vague error message to field with error
    linkLine.css({
        'height': lineLength,
        '-webkit-transform': 'rotate(' + angle + 'deg)',
        '-moz-transform': 'rotate(' + angle + 'deg)',
        '-o-transform': 'rotate(' + angle + 'deg)',
        '-ms-transform': 'rotate(' + angle + 'deg)'
    });

    // flip line, hide and add error class to field with error
    setTimeout(function() {
        linkLine.css({
            'top': errorOffset.top,
            'left': errorOffset.left,
            'height': 0,
            '-webkit-transform': 'rotate(' + inverseAngle + 'deg)',
            '-moz-transform': 'rotate(' + inverseAngle + 'deg)',
            '-o-transform': 'rotate(' + inverseAngle + 'deg)',
            '-ms-transform': 'rotate(' + inverseAngle + 'deg)'
        });
        error.addClass('error');
        error.focus(function() {
            $(this).removeClass('error');
        });
    }, 300);

}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console