• Stars
    star
    98
  • Rank 345,882 (Top 7 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated over 5 years ago

Reviews

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

Repository Details

Ajax & server side validation for Angularjs

#Ajax Validation for Angularjs#

ngRemoveValidate makes it easy for you to validate form fields agents data from your server. For example, a sign up form may need to check if the email entered is already registered before submitting the form.

Features:

  • Drop in solution for Ajax validation of any text or password input

  • Works with Angulars built in validation and can be accessed at formName.inputName.$error.ngRemoteValidate

  • Throttles server requests (default 400ms) and can be set with ng-remote-throttle="550"

  • Allows HTTP method definition (default POST) with ng-remote-method="GET"

##Getting started - Example##

Adding ngRemoteValidate to your project

bower install ng-remote-validate

OR

Grab either the minified version or the standard source from the release folder and add it to your project.

<script type="text/javascript" src="../your/path/ngRemoteValidate.js"></script>

Include ngRemoteValidate in you Angular app

var app = angular.module( 'myApp', [ 'remoteValidation' ] );

Using it in your forms

This will be a basic change password form that requires the user to enter their current password as well as the new password.

<h3>Change password</h3>
<form name="changePasswordForm">
    <label for="currentPassword">Current</label>
    <input type="password" 
           name="currentPassword" 
           placeholder="Current password" 
           ng-model="password.current" 
           ng-remote-validate="/customer/validpassword" 
           required>
    <span ng-show="changePasswordForm.currentPassword.$error.required && changePasswordForm.confirmPassword.$dirty">
        Required
    </span>
    <span ng-show="changePasswordForm.currentPassword.$error.ngRemoteValidate">
        Incorrect current password. Please enter your current account password.
    </span>

    <label for="newPassword">New</label>
    <input type="password"
           name="newPassword"
           placeholder="New password"
           ng-model="password.new"
           required>
    
    <label for="confirmPassword">Confirm</label>
    <input ng-disabled=""
           type="password"
           name="confirmPassword"
           placeholder="Confirm password"
           ng-model="password.confirm"
           ng-match="password.new"
           required>
    <span ng-show="changePasswordForm.confirmPassword.$error.match">
        New and confirm do not match
    </span>
    
    <div>
        <button type="submit" 
                ng-disabled="changePasswordForm.$invalid || changePasswordForm.$pending" 
                ng-click="changePassword(password.new, changePasswordForm);reset();">
            Change password
        </button>
    </div>
</form>

##Options## There are a few defaults that can be overwritten with options. They are:

  • ng-remote-validate takes a string, an Array of string i.e. ng-remote-validate="/url/one" or ng-remote-validate="[ '/url/one', '/url/two' ]", or an Object of string/validation pairs i.e. ng-remote-validate="{ '/url/validate/unique' : 'unique', '/url/validate/blacklist' : 'blacklisted'}", which would respectively set formName.inputName.$error.unique and formName.inputName.$error.blacklisted in addition to the catch-all formName.inputName.$error.ngRemoteValidate.
  • ng-remote-throttle (default: 400) Users inactivity length before sending validation requests to the server
  • ng-remote-method (default: 'POST') Type of request you would like to send

Example using all

<input type="password" 
       name="currentPassword" 
       placeholder="Current password" 
       ng-model="password.current" 
       ng-remote-validate="/customer/validpassword"
       ng-remote-throttle="550"
       ng-remote-method="GET"
       required>

<input type="text" 
       name="email" 
       placeholder="Email address" 
       ng-model="email" 
       ng-remote-validate="[ '/customer/email-registered', '/customer/email-restricted' ]"
       ng-remote-throttle="800"
       ng-remote-method="POST"
       required>

ngRemote will add a $pending property on your model and the containing form. You can use these to show loading animations and to disable the form submit button:

<span class="message" ng-show="myForm.inputName.$pending">validating...</span>

...

<button type="submit" ng-disabled="myForm.$invalid || myForm.$pending" ng-click="...">Go!</button>

##Server side##

Data sent to the server

By default, ngRemoteValidate will send a simple JSON string to the server formatted like so:

{ "value": "inputValue" }

If you would like to change what data is sent to the server, you can create an inputNameSetArgs callback on your controllers $scope. This callback should return the data you want sent to the server.

$scope.currentPasswordSetArgs = function( val, el, attrs, ngModel ) {
    return { value: val, otherData: attrs.otherData };
}; 

Server response

ngRemoteValidate wants a specific JSON response from your servers. The response should look as follows:

{
    isValid: bool, //Is the value received valid 
    value: 'myPassword!' //value received from server
}

More Repositories

1

Honeypot-MVC

A simple honeypot to replace your standard reCAPTCHA for ASP.NET MVC2+
JavaScript
24
star
2

Structuremap.MVC5

Puppet
21
star
3

Structuremap.MVC4

Adds the latest version of structuremap and configures it as the default Dependency Resolver. Works with 'Controllers' and 'ApiControllers'.
Puppet
16
star
4

Orchard-SiteMap

SiteMap module for Orchard CMS
C#
14
star
5

signals.js

A lightweight (1k minified) pure JavaScript PubSub library
JavaScript
13
star
6

Structuremap.WebAPI2

Puppet
12
star
7

Structuremap-MVC3

9
star
8

Secure-Page-manager-for-asp.net

Manage what pages need to be HTTPS and force all others to use HTTP
JavaScript
7
star
9

Orchard-ContentReference

Orchard Content Reference Field Module
C#
5
star
10

takeCommand

jQuery Ajax wrapper
JavaScript
4
star
11

Orchard-Flicker-Gallery

Flickr Gallery for Orchard CMS
C#
4
star
12

jsLINQ

Simple LINQ like extensions for JavaScript Arrays
JavaScript
3
star
13

JSON-Constructor-Model-Mapper

JSON to Constructor Model Mapper
JavaScript
2
star
14

Mailchimp

Mailchimp for ASP.NET
C#
2
star
15

Orchard-Forms

Forms module for Orchard CMS
2
star
16

SimpleTags-ASP.NET-C-

Gives a .NET developer the ability to create HTML elements with a jQuery like syntax
C#
2
star
17

jMoney

simple js framework to run unit tests/behaviors
JavaScript
2
star
18

MotionMail.Net

MotionMail API Lib for ASP.NET
C#
1
star
19

maxBox

Limit the amount of text entered into a text area with preview of remaining characters
1
star
20

TinyMCE-Simple-Link-Plugin

TinyMCE Simple Link Plugin
JavaScript
1
star
21

gLoad

Lazy load images for galleries/albums
1
star
22

javascript-parse-url-query

Creates a hash and easy methods to access url query values
JavaScript
1
star
23

wnk

JavaScript
1
star
24

JavaScript-formatWith-string-extension

JavaScript .formatWith() string extension
JavaScript
1
star
25

jquery-infinite-scroll-lazy-loading-FatBoy

jQuery Infinite scrolling of <T> (Lazy Loading)
JavaScript
1
star