Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                <form class="validate-form">
  <h1>Simple-validation.js</h1>
  <p>A simple approach to the nightmare that is form validation. Try it out by applying to be my friend below.</p>
  <div>
    <div class="input-holder">
      <input type="text" placeholder="*Your Name" class="validate" required/>
      <span class="message">Who are you stranger?</span>
    </div>
    <div class="input-holder">
      <input type="email" placeholder="*Your Email" class="validate" required/>
      <span class="message">How else will I contact you?</span>
    </div>
    <div class="input-holder">
      <textarea class="validate">*Your hobbies?</textarea>
      <span class="message">Ok. I'll go first. I'm a big fan of the New York Giants. You?</span>
    </div>
    <div class="input-holder">
      <textarea>Who is your favorite NFL Quarterback and why is it Eli Manning?</textarea>
    </div>
  </div>
  <button class="submit">Submit</button>
  <p>Don't worry. This form doesn't do anything, but if you truly want to be my friend, follow me on <a href="https://twitter.com/chdhmphry">twitter</a>!</p>
  <p>Further reading: <a href="http://hmphry.com/posts/formaphobia">Formaphobia</a>, <a href="https://github.com/chdhmphry/simple-validation">simple-validation.js on github</a>.</p>
</form>
              
            
!

CSS

              
                *{
  box-sizing: border-box;
}
body{
  background: #F3F3F4;
  color: #333;
  padding: 20px;
}
form{
  width: 400px;
  margin: 0 auto 0;
}
.input-holder{
  position: relative;
  margin: 0 0 20px;
}
input,
textarea{
  border: 2px solid #ccc;
  border-radius: 4px;
  padding: 10px;
  width: 100%;
  resize: none;
  transition: .3s;
  &:focus{
    border: 2px solid #999;
    outline: none;
  }
}
textarea{
  min-height: 100px;
}
.message{
  position: absolute;
  z-index: 9;
  display: none;
  width: 100%;
  padding: 10px;
  margin-top: 10px;
  background: #D9585C;
  color: #fff;
  text-align: center;
  &:after{
    content:'';
    position: absolute;
    top: -10px;
    left: 50%;
    display: block;
    margin-left: -10px;
    border: solid;
    border-color: #D9585C transparent #D9585C;
    border-width: 0 10px 10px;
  }
}
.is-valid{
  border-color: #AAD661 !important;
  transition: 0;
}
.not-valid{
  border-color: #D7595F;
  transition: 0;
  &+.message{
    display: block !important; 
  }
}
.submit{
  display: block;
  width: 100%;
  padding: 10px;
  border: 2px solid #ccc;
  border-radius: 4px;
  background: transparent;
  color: #ccc;
  transition: .3s;
  &:hover{
    color: #AAD661;
    border-color: #AAD661; 
  }
}
              
            
!

JS

              
                // Simple Validation - By Chad Humphrey - hmphry.com
// Released under the MIT license

// Get all forms
var simpleValidation = function(){

  var validateForm = $('form.validate-form');

  // Gets all forms to Validate
  validateForm.each(function(){
    // Defining basic variables, bro
    var validateForm = $(this);
    var validate = {};
    var validateThis = $(this).find('.validate');
    var validatingLength = $(this).find('.validate').length;
    var submitBtn = $(this).find('.submit');

    // For Loop Getting Elements to Validate
    for(var i = 1; i <= validatingLength; i++){
      // Adding Inputs to object, false for default
      validate['input'+i] = false;
    }

    $('.validate').blur(function(){
      var index =  $(this).prevAll().length+1;
      var validateThisVal = $(this).val();
      var validateThisType = $(this).attr('type');

      // Checks if input type is email
      if(validateThisType === "email"){

        // Email regex
        var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        // Condition to see if Email exists
        if(!validateThisVal.match(re)){
          $(this).addClass('not-valid');
          $(this).removeClass('is-valid');
          return validate['input'+index] = false;
        } else{
          $(this).addClass('is-valid');
          $(this).removeClass('not-valid');
          return validate['input'+index] = true;
        }
      } else{
        // Makes sure input is filled out
        if(validateThisVal == ""){
          $(this).addClass('not-valid');
          $(this).removeClass('is-valid');
          return validate['input'+index] = false;
        } else{
          $(this).addClass('is-valid');
          $(this).removeClass('not-valid');
          return validate['input'+index] = true;
        }
      }
    });


    validateForm.submit(function(event){
      // Prevents Default
      event.preventDefault();

      // Logging form errors
      var falseCtn = 0;
      for(var i = 1; i <= validatingLength; i++){
        if(validate['input'+i] == false){
          falseCtn++;
        }
      }

      // Checking if any falses exist
      if(falseCtn > 0){
        $(this).unbind('submit').submit();
        $(this).click();
      } else{
      }
    });

  });

};

simpleValidation()
              
            
!
999px

Console