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

Packages

Add Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import or require. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="mocha"></div>

<form class="test-form">
    <input name="first-name" type="text" data-validation="alphabetical" />
    <input name="age" type="text" data-validation="numeric" />
</form>
            
          
!
            
              .test-form {
    display: none;
}
            
          
!
            
              (function () {
    'use strict';
    
    function validateForm(form) {
        const result = {
            errors: []
        };
        
        const inputs = Array.from(form.querySelectorAll('input'));
        let isValid = true;
        
        for (let input of inputs) {
            if (input.dataset.validation === 'alphabetical') {
                isValid = isValid && /^[a-z]+$/i.test(input.value);
                
                if (!isValid) {
                    result.errors.push(
                        new Error(`${input.value} is not a valid ${input.name} value`)
                    );
                }
            } else if (input.dataset.validation === 'numeric') {
                isValid = isValid && /^[0-9]+$/.test(input.value);
                
                if (!isValid) {
                    result.errors.push(
                        new Error(`${input.value} is not a valid ${input.name} value`)
                    );
                }
            }
        }

        result.isValid = isValid;
        return result;
    }
    
    // Tests
    mocha.setup('bdd');
    const { expect } = chai;
    
    describe('the form validator', function () {
        let form = document.querySelector('.test-form');
        
        beforeEach(function () {
            form = form.cloneNode(true);
        });
        
        describe('the validateForm function', function () {
        
            it('should validate a form with all of the possible validation types', function () {
                const name = form.querySelector('input[name="first-name"]');
                const age = form.querySelector('input[name="age"]');

                name.value = 'Bob';
                age.value = '42';

                const result = validateForm(form);
                expect(result.isValid).to.be.true;
                expect(result.errors.length).to.equal(0);
            });

            it('should return an error when a name is invalid', function () {
                const name = form.querySelector('input[name="first-name"]');
                const age = form.querySelector('input[name="age"]');

                name.value = '!!!';
                age.value = '42';

                const result = validateForm(form);

                expect(result.isValid).to.be.false;
                expect(result.errors[0]).to.be.instanceof(Error);
                expect(result.errors[0].message).to.equal('!!! is not a valid first-name value');
            });

            it('should return an error when an age is invalid', function () {
                const name = form.querySelector('input[name="first-name"]');
                const age = form.querySelector('input[name="age"]');

                name.value = 'Greg';
                age.value = 'a';

                const result = validateForm(form);

                expect(result.isValid).to.be.false;
                expect(result.errors[0]).to.be.instanceof(Error);
                expect(result.errors[0].message).to.equal('a is not a valid age value');
            });

            it('should return multiple errors if more than one field is invalid', function () {
                const name = form.querySelector('input[name="first-name"]');
                const age = form.querySelector('input[name="age"]');

                name.value = '!!!';
                age.value = 'a';

                const result = validateForm(form);

                expect(result.isValid).to.be.false;
                expect(result.errors[0]).to.be.instanceof(Error);
                expect(result.errors[0].message).to.equal('!!! is not a valid first-name value');
                expect(result.errors[1]).to.be.instanceof(Error);
                expect(result.errors[1].message).to.equal('a is not a valid age value');
            });
        });
    });
    
    mocha.run();
}());
            
          
!
999px

Console