Git Product home page Git Product logo

angular-form-validator's Introduction

DEMO: https://rasparac.github.io/angular-form-validator/

Install

  • Using bower and running bower install angularjs-form-validator

TODO

  • add more validation rules
  • tests
  • add on blur validation
  • minify

How to add to the project

	<script type="text/javascript" src="../bower_components/dist/angular-form-validator.js"></script>
	angular.module('app', ['formValidator']);

Code examples

Each input field must have [name] attribute. To use angularjs form validator just include formValidator into your project. Attach a [validation] attribute to input.

Simple form without options

In this example validator is going to check if the input field is filled.

	<form ng-submit="demo.noOptionsFormSubmit()" novalidate name="noOptionsForm">
        <div class="form-group">
            <label for="username">Username</label>
            <input
                name="username"
                validation
                required
                ng-model="demo.noOptionsFormData.username"
                type="text"
                class="form-control"
                id="username"
                placeholder="Username">
        </div>
        <div class="form-group">
            <button
                ng-disabled="noOptionsForm.$invalid"
                class="btn btn-default btn-md btn-block">
                Submit
            </button>
        </div>
    </form>

Validation options

You can set validation options via [validation-options] attribute. You just need to pass a JSON object with the options.

    {
        above: true,
        customMessages: {
            pattern: 'You must enter \'username\'',
            required: 'Custom required message!',
            minlength: 'Custom to short message!',
            maxlength: 'Custom to long message',
            max: 'Custom max message',
            min: 'Custom min message'
        },
        parentElement: 'custom-parent-element',
        parentValidationClass: 'parent-element-custom-class'
        errorElementClass: 'error-element-custom-class',
        errorElement: 'custom-error-element'
    }

HTML

    <form ng-submit="demo.customOptionsFromSubmit()" novalidate name="customOptionsFrom">
        <div class="custom-parent-element">
            <div class="form-group">
                <label for="username">Username</label>
                <input
                    name="username"
                    validation
                    validation-options="{ 'parentElement: 'custom-parent-element', 'above': true, customMessages: { pattern: 'You must enter \'username\'', required: 'Custom required message!' }}"
                    required
                    ng-minlength="5"
                    ng-maxlength="10"
                    ng-pattern="/username/"
                    ng-model="demo.customOptionsFromData.username"
                    type="text"
                    class="form-control"
                    id="username"
                    placeholder="Username">
            </div>
        </div>
        <div class="form-group">
            <button
                ng-disabled="customOptionsFrom.$invalid"
                class="btn btn-default btn-md btn-block">
                Submit
            </button>
        </div>
    </form>

Custom validation function

You can also pass a custom validation to the validator. In the example below you can see how to add a custom validation to the validator. Custom validation function must return an object with two params, isValid and message; Example:

    {
        isValid: true,
        message: 'Custom message!'
    }

Javascript code

    angular
        .module('demo', ['formValidator'])

    angular
        .module('demo')
        .controller('DemoCtrl', DemoCtrl);

    function DemoCtrl(users, $q) {
        var vm = this;

        vm.customValidation = function(confirmPassword) {
            var customValidationObj = {
                isValid: true,
                message: ''
            };

            if (!confirmPassword) return customValidationObj;

            if (vm.customValidationFromData.password != confirmPassword) {
                customValidationObj.isValid = false;
                customValidationObj.message = 'Passwords must match!';
            }

            return customValidationObj;
        }
    }

##Async validation function You can pass a async validation function to the validator. Async function must return a promise. Documentation (https://docs.angularjs.org/guide/forms). If you use async validation you should also use [async-message] attribute.

Javascript code

    vm.asyncValidation = function() {
        return function(modelView, viewValue) {
            return users
                    .checkUsername(viewValue)
                    .then(success, error)
        }

        function success(response) {
            return true;
        }

        function error(error) {
            vm.asyncMessage = error;
            return $q.reject();
        }
    }

HTML

    <form ng-submit="demo.asyncFormValidationSubmit()" novalidate name="asyncForm">
        <div class="form-group">
            <label for="username">Username</label>
            <input
                name="username"
                validation
                async-validation="demo.asyncValidation()"
                async-message="demo.asyncMessage"
                required
                ng-model="demo.asyncFormFormData.username"
                type="text"
                class="form-control"
                id="username"
                placeholder="Username">
        </div>
        <div class="form-group">
            <button
                ng-disabled="asyncForm.$invalid"
                class="btn btn-default btn-block">
            Submit
            </button>
        </div>
    </form>

angular-form-validator

AngularJS form validation directive Inspired by https://github.com/turinggroup/angular-validator.

angular-form-validator's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

leandroaps

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.