GithubHelp home page GithubHelp logo

form's Introduction

Check Form

GitHub release npm version npm downloads npm downloads

Simple yet powerful way to build and validate HTML forms.

Install:

$ npm install check-form

What Check Form Looks Like

app.html:

<form id="signup">
    <div class="row">
        <label for="given_name">Given Name</label>
        <input id="given_name" name="given_name" />
    </div>
    <div class="row">
        <label for="surname">Surname</label>
        <input id="surname" name="surname" />
    </div>
    <div class="row">
        <label for="email">Email</label>
        <input id="email" name="email" />
    </div>
    <div class="row">
        <label for="username">Username</label>
        <input id="username" name="username" />
    </div>
    <div class="row">
        <label for="password">Password</label>
        <input id="password" name="password" />
    </div>
    <div class="row">
        <button type="submit" name="submit">Signup</button>
    </div>
</form>

app.js:

var Form = require('check-form');

var EMAIL = /^[A-Za-z][A-Za-z0-9]{2,}[@][A-Za-z0-9\-]+[.][A-Za-z0-9]{2,}$/;

function validName(validation) {
    var value = validation.value.trim();
    var valid = /^[A-Za-z][A-Za-z\-]{2,}$/.test(value);

    validation.setValidation({
        valid: valid,
        value: value,
        message: valid ? '' : validation.key + ' must not contain non-letter characters.'
    });
}

var signupForm = new Form({
    fields: {
        given_name: validName,
        surname: validName,
        email: function (validation) {
            var value = validation.value.trim();
            var valid = EMAIL.test(value);

            validation.setValidation({
                valid: valid,
                value: value,
                message: valid ? '' : 'invalid email.'
            });
        },
        username: function (validation) {
            var value = validation.value.trim();
            var valid = /^[A-Za-z][A-Za-z0-9]{2,}$/.test(value);

            validation.setValidation({
                valid: valid,
                value: value,
                message: value.length < 3 ?
                'username must be at least 3 characters.' :
                valid ?
                '' :
                'username must not contain special characters.'
            });
        },
        password: function (validation) {
            var value = validation.value;
            var valid = value.length >= 6;

            validation.setValidation({
                valid: valid,
                value: value,
                message: valid ?
                '' : 'password must be at least 6 characters.'
            });
        }
    },
    action: function (data, callback) {
        // do something with data
        callback(/*err, res*/);
    }
});

// signupForm.fill(data) returns self
// signupForm.serialize(FormElement) returns self
// signupForm.validate([key]) returns (true|false|null)
// signupForm.submit(callback) returns (true|false|null)
// signupForm.reset() returns self
// signupForm.bind(formElement)
// signupForm.unbind(formElement)

form's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.