Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-form-validate

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-form-validate

[![Bower](https://img.shields.io/bower/v/jpkleemans-angular-validate.svg)](https://github.com/jpkleemans/angular-validate/releases/latest) [![GitHub license](https://img.shields.io/github/license/jpkleemans/angular-validate.svg)](https://github.com/jpkle

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
123
increased by30.85%
Maintainers
1
Weekly downloads
 
Created
Source

Angular Validate

Bower GitHub license

Painless form validation for AngularJS. Powered by the jQuery Validation Plugin.

Table of contents

  1. Installation
  2. Usage
  3. Built-in validation rules
  4. Configuration

Installation

Download Angular Validate:

  • With Bower:
$ bower install jpkleemans-angular-validate
  • With Git:
$ git clone https://github.com/jpkleemans/angular-validate.git

When using one of the last two methods make sure you also download the latest release of the jQuery Validation Plugin.

Include both jquery.validate.min.js and angular-validate.min.js in your HTML page:

<!-- jQuery scripts -->
<script src="path/to/jquery.min.js"></script>
<script src="path/to/jquery.validate.min.js"></script>

<!-- Angular scripts -->
<script src="path/to/angular.min.js"></script>
<script src="path/to/angular-validate.min.js"></script>

Inject the ngValidate module as a dependency into your Angular application:

angular.module('myApp', ['ngValidate']);

Usage

Add the ng-validate directive to your form and pass the validation options as value:

<form name="registerform" ng-submit="register(registerform)" ng-validate="validationOptions">
    <input type="email" name="email">
    <input type="password" name="password">
</form>
Set validation options

Then set the validation options in your controller:

$scope.validationOptions = {
    rules: {
        email: {
            required: true,
            email: true
        },
        password: {
            required: true,
            minlength: 6
        }
    },
    messages: {
        email: {
            required: "We need your email address to contact you",
            email: "Your email address must be in the format of name@domain.com"
        },
        password: {
            required: "You must enter a password",
            minlength: "Your password must have a minimum length of 6 characters"
        }
    }
}

Or (for simple forms) insert the options directly without using a controller:

<form name="simpleform" ng-submit="register(simpleform)" ng-validate="{rules: {name: "required"}}">

For all available options, see: http://jqueryvalidation.org/validate#validate-options

Check form validity

Now you can validate the form by calling validate() on the form instance:

$scope.register = function (form) {
    if(form.validate()) {
        // Form is valid!
    }
}

You can also pass your validation options as the first parameter of validate().

Get number of invalid fields
$window.alert("There are " + form.numberOfInvalids() + " invalid fields.");

Built-in validation rules

A set of standard validation rules is provided by the jQuery Validation Plugin:

  • required – Makes the element required.
  • remote – Requests a resource to check the element for validity.
  • minlength – Makes the element require a given minimum length.
  • maxlength – Makes the element require a given maxmimum length.
  • rangelength – Makes the element require a given value range.
  • min – Makes the element require a given minimum.
  • max – Makes the element require a given maximum.
  • range – Makes the element require a given value range.
  • email – Makes the element require a valid email.
  • url – Makes the element require a valid url.
  • date – Makes the element require a date.
  • dateISO – Makes the element require an ISO date.
  • number – Makes the element require a decimal number.
  • digits – Makes the element require digits only.
  • creditcard – Makes the element require a credit card number.
  • equalTo – Requires the element to be the same as another one.

More info: http://jqueryvalidation.org/documentation/#link-list-of-built-in-validation-methods

Configuration

Angular Validate ships with a $validatorProvider, that you can use to configure default validation options and custom validation rules.

Default validation options
angular.module('myApp')
    .config(function ($validatorProvider) {
        $validatorProvider.setDefaults({
            errorElement: 'span',
            errorClass: 'help-block'
        });
    });

More info: http://jqueryvalidation.org/jQuery.validator.setDefaults

Custom validation rules
angular.module('myApp')
    .config(function ($validatorProvider) {
        $validatorProvider.addMethod("domain", function (value, element) {
            return this.optional(element) || /^http:\/\/mydomain.com/.test(value);
        }, "Please specify the correct domain for your documents");
    });

More info: http://jqueryvalidation.org/jQuery.validator.addMethod

FAQs

Package last updated on 02 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc