• Stars
    star
    162
  • Rank 225,430 (Top 5 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

AngularJS form validation.

angular-validator Build Status devDependency Status

MIT License

This is an AngularJS form validation written in CoffeeScript and thinking in AngularJS not jQuery.

Installation

bower

$ bower install https://github.com/kelp404/angular-validator.git\#0.2.9 -S

Frameworks

  1. jQuery 3.3.1

  2. AngularJS 1.5.11

  3. Bootstrap 3

If your error is string in rules you should include bootstrap3.css and use form-group to the input div.

$validator

angular.module 'yourApp', ['validator']

register

# .config
$validatorProvider.register = (name, object={}) ->
    ###
    Register the rule.
    @params name: The rule name.
    @params object:
        invoke: 'watch' or 'blur' or undefined(validate by yourself)
        init: function(scope, element, attrs, $injector)
        validator: RegExp() or function(value, scope, element, attrs, $injector)
        error: string or function(value, scope, element, attrs, $injector)
        success: function(value, scope, element, attrs, $injector)
    ###
# .run
$validator.register = (name, object={}) ->

validate

$validate.validate = (scope, model) =>
    ###
    Validate the model.
    @param scope: The scope.
    @param model: The model name of the scope or validator-group.
    @return:
        @promise success(): The success function.
        @promise error(): The error function.
    ###

reset

$validate.reset = (scope, model) =>
    ###
    Reset validated error messages of the model.
    @param scope: The scope.
    @param model: The model name of the scope or validator-group.
    ###

validator.directive

a = angular.module 'validator.directive', ['validator.provider']
validator = ($injector) ->
    restrict: 'A'
    require: 'ngModel'
    link: (scope, element, attrs) ->
        ###
        The link of `validator`.
        You could use `validator=[rule, rule]` or `validator=/^regex$/`.
        ###
validator.$inject = ['$injector']
a.directive 'validator', validator

validator="[rule, rule]", [required], [validator-required="true/false"], [validator-group="group"]

<div class="form-group">
    <label for="required0" class="col-md-2 control-label">Required</label>
    <div class="col-md-10">
        <input type="text" ng-model="formWatch.required" validator="[required]"
         class="form-control" id="required0" placeholder="Required"/>
    </div>
</div>

validator="/^regex$/", [validator-error="msg"], [validator-invoke="watch"], [required], [validator-required="true/false"], [validator-group="group"]

<div class="form-group">
    <label for="regexp0" class="col-md-2 control-label">RegExp [a-z]</label>
    <div class="col-md-10">
        <input type="text" ng-model="formWatch.regexp" validator="/[a-z]/"
         validator-invoke="watch" validator-error="it should be /[a-z]/"
         class="form-control" id="regexp0" placeholder="RegExp [a-z]"/>
    </div>
</div>

[required], [validator-required="true/false"]

If the element has this attribute, $validator will add the rule required into rules of the element.

validator.rules

angular.module 'yourApp', ['validator.rules']

There are default rules in this module.

  • required
  • number
  • email
  • url

Example

<!-- Bootstrap3 (not required) -->
<link type="text/css" rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"/>
<!-- jQuery -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- AngularJS -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.11/angular.min.js"></script>
<!-- $validator -->
<script type="text/javascript" src="dist/angular-validator.js"></script>
<!-- basic rules (not required) -->
<script type="text/javascript" src="dist/angular-validator-rules.js"></script>
<!-- submit -->
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">submit</h3>
    </div>
    <form class="form-horizontal panel-body">
        <div class="form-group">
            <label for="required2" class="col-md-2 control-label">Required</label>
            <div class="col-md-10">
                <input type="text" ng-model="formSubmit.required" validator="[requiredSubmit]" class="form-control" id="required2" placeholder="Required"/>
            </div>
        </div>
        <div class="form-group">
            <label for="regexp2" class="col-md-2 control-label">RegExp [a-z]</label>
            <div class="col-md-10">
                <input type="text" ng-model="formSubmit.regexp" validator="/[a-z]/" validator-error="it should be /[a-z]/" class="form-control" id="regexp2" placeholder="RegExp [a-z]"/>
            </div>
        </div>
        <div class="form-group">
            <label for="http2" class="col-md-2 control-label">$http</label>
            <div class="col-md-10">
                <input type="text" ng-model="formSubmit.http" validator="[backendSubmit]" class="form-control" id="http2" placeholder="do not use 'Kelp' or 'x'"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" ng-click="submit()" class="btn btn-default"/>
            </div>
        </div>
    </form>
    <div class="panel-footer">{{formSubmit}}</div>
</div>
a = angular.module 'app', ['validator', 'validator.rules']
a.config ($validatorProvider) ->    
    $validatorProvider.register 'backendSubmit',
        validator: (value, scope, element, attrs, $injector) ->
            $http = $injector.get '$http'
            h = $http.get 'example/data.json'
            h.then (data) ->
                if data and data.status < 400 and data.data
                    return no if value in (x.name for x in data.data)
                    return yes
                else
                    return no
        error: "do not use 'Kelp' or 'x'"
    # submit - required
    $validatorProvider.register 'requiredSubmit',
        validator: RegExp "^.+$"
        error: 'This field is required.'
# CoffeeScript
# the form model
$scope.formSubmit =
    required: ''
    regexp: ''
    http: ''
# the submit function
$scope.submit = ->
    v = $validator.validate $scope, 'formSubmit'
    v.success ->
        # validated success
        console.log 'success'
    v.error ->
        # validated error
        console.log 'error'
// JavaScript
// the form model
$scope.formSubmit = {
    required: '',
    regexp: '',
    http: ''
};
// the submit function
$scope.submit = function () {
    $validator.validate($scope, 'formSubmit')
    .success(function () {
        // validated success
        console.log('success');
    })
    .error(function () {
        // validated error
        console.log('error');
    });
};

Unit Test

$ grunt test

Development

# install node modules
$ npm install
# install bower components
$ bower install
# run the local server and the file watcher to compile CoffeeScript
$ grunt dev
# compile coffee script and minify
$ grunt build

More Repositories

1

CocoaSecurity

Encrypt/Decrypt: AES. Hash: MD5, SHA(SHA1, SHA224, SHA256, SHA384, SHA512). Encode/Decode: Base64, Hex.
Objective-C
1,132
star
2

angular-form-builder

Drag and drop to build bootstrap forms in AngularJS.
CoffeeScript
601
star
3

NyaruDB

A simple NoSQL database(key-value pair) in Objective-C. It runs on iOS and OS X.
Objective-C
140
star
4

Victory

An error reporting server in Python. It runs on Google App Engine.
Python
30
star
5

electron-s3-file-manager

A GUI AWS S3 file manager. It supports keyword search, download, upload and preview video.
JavaScript
22
star
6

Flask-GAE

This is my Flask Google App Engine template.
Python
19
star
7

Victorique

An error report server on Google App Engine.
Python
16
star
8

bull-admin-panel

A real time admin panel of Bull(Redis-based queue) based on Express and WebSocket.
JavaScript
14
star
9

enju

An Elasticsearch client on Node.js written in CoffeeScript.
CoffeeScript
13
star
10

AlertView

AlertView is a jQuery plugin that provides alerting message. That are like OS X notification window, and Twitter alert message.
CoffeeScript
12
star
11

mongoose-profiler

A performance tuning tool for Mongoose. Show explain results when the query is slow.
JavaScript
11
star
12

anya-s3-file-manager

An AWS S3 file manager. It supports keyword search, upload, preview video and archive files into a zip then download it.
JavaScript
7
star
13

CoffeeCocoa

Run CoffeeScript and JavaScript in Objective-C.
Objective-C
6
star
14

tina

An elasticsearch client on Python 3.4 for django.
Python
6
star
15

ValueInjector

Cocoa ValueInjector provides converting weak typing to strong typing. Injecting value from NSDictionary to custom class and initialization NSDictionary with custom class.
Objective-C
5
star
16

NyaruDB-Control

A management tool for NyaruDB which is a simple NoSQL database in Objective-C.
CoffeeScript
5
star
17

react-hooks-shared-state

A global state for React with Hooks API.
JavaScript
4
star
18

quick-jump

Quickly navigate the cursor to any position visible in the editor like AceJump.
CoffeeScript
4
star
19

Maguro

A contact us library for UserVoice.
Objective-C
4
star
20

Tower-of-Saviors

The Tower of Saviors gallery.
CoffeeScript
3
star
21

angularjs-bootstrap-datepicker

A Bootstrap3 datepicker for AngularJS.
CoffeeScript
3
star
22

capybara-router

Unfancy react router without flux and redux.
JavaScript
3
star
23

kue-admin-panel

An admin panel of Kue based on WebSocket.
JavaScript
2
star
24

current-device

This repository is for bower. $ bower install current-device -S
JavaScript
2
star
25

test

Dockerfile
2
star
26

phantom-worker

A PhantomJS worker. Execute the Single Page Application then return html.
JavaScript
2
star
27

poi-router

An AngularJS router.
CoffeeScript
2
star
28

dailydrinks

A drinks order web.
JavaScript
2
star
29

Django-GAE

The Django site on Google App Engine.
Python
2
star
30

d3-geo-projection

1
star
31

Victory-iOS

Victory iOS demo code. An error and crash reporter example code.
Objective-C
1
star
32

Salmon

Salmon issue tracker.
Python
1
star
33

photo-base

HTML
1
star
34

real-estate-lab

不動產投報率試算工具。
JavaScript
1
star
35

changelog.config

A conventional-changelog config.
JavaScript
1
star
36

react-datepicker

A react datepicker.
JavaScript
1
star