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';
    
    const validationRules = new Map([
        ['alphabetical', /^[a-z]+$/i],
        ['numeric', /^[0-9]+$/]
    ]);
    
    // Implementation
    function createValidationQueries(inputs) {
        return Array.from(inputs).map(input => ({
            name: input.name,
            type: input.dataset.validation,
            value: input.value
        }));
    }
    
    function validateItem(validation, validationRules) {
        if (!validationRules.has(validation.type)) {
            return false;
        }

        return validationRules.get(validation.type).test(validation.value);
    }
    
    function validateForm(form) {
        const result = {
            get isValid() {
                return this.errors.length === 0;
            },
            
            errors: []
        };

        for (let validation of createValidationQueries(form.querySelectorAll('input'))) {
            let isValid = validateItem(validation, validationRules);
            
            if (!isValid) {
                result.errors.push(
                    new Error(`${validation.value} is not a valid ${validation.name} value`)
                );
            }
        }

        return result;
    }
    
    // Test Setup
    mocha.setup('bdd');
    const { expect } = chai;
    
    describe('the form validator', function () {
        let form;
        
        beforeEach(function () {
            form = document.querySelector('.test-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');
            });
        });
        
        describe('the createValidationQueries function', function () {
            it(
                'should map input elements with a data-validation attribute to an array of validation objects',

                function () {
                    const name = form.querySelector('input[name="first-name"]');
                    const age = form.querySelector('input[name="age"]');

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

                    const validations = createValidationQueries([name, age]);

                    expect(validations.length).to.equal(2);

                    expect(validations[0].name).to.equal('first-name');
                    expect(validations[0].type).to.equal('alphabetical');
                    expect(validations[0].value).to.equal('Bob');

                    expect(validations[1].name).to.equal('age');
                    expect(validations[1].type).to.equal('numeric');
                    expect(validations[1].value).to.equal('42');
                }
            );
        });
        
        describe('the validateItem function', function () {
            const validationRules = new Map([
                ['alphabetical', /^[a-z]+$/i]
            ]);

            it(
                'should return true when the passed item is deemed valid against the supplied validation rules',

                function () {
                    const validation = {
                        type: 'alphabetical',
                        value: 'Bob'
                    };

                    const isValid = validateItem(validation, validationRules);
                    expect(isValid).to.be.true;
                }
            );
            
            it(
                'should return false when the passed item is deemed invalid',

                function () {
                    const validation = {
                        type: 'alphabetical',
                        value: '42'
                    };

                    const isValid = validateItem(validation, validationRules);
                    expect(isValid).to.be.false;
                }
            );
            
            it(
                'should return false when the specified validation type is not found',
                
                function () {
                    const validation = {
                        type: 'foo',
                        value: '42'
                    };
                    
                    const isValid = validateItem(validation, validationRules);
                    expect(isValid).to.be.false;
                }
            );
        });
    });
    
    mocha.run();
}());
            
          
!
999px

Console