Giter VIP home page Giter VIP logo

form-amd's Introduction

Form-amd

Form-amd library

form.js is a small library to help form manipulations and validation. This library uses amd structure.

Build Status

It uses html5 form attributes validate specification and works on browsers that does not support html5 validation. Then, we built it from scratch.

Installation

Install with Bower: bower install form-amd

Dependencies

Form-amd depends on an amd implementation. We suggest async-define implementation for dependency lookup. Form-amd also depends on doc-amd.

Methods

submitOnChange

.submitOnChange(selectorOrDocElement[, callback])

Description:

Submit the parent form when event change is triggered.

Parameters:

selectorOrDocElement: doc-amd object or String //A CSS selector. Note that, if it is a class name with dots, the dots must be escaped. E.g.: doc(".my\\.class")

callback: Function() //A function to call before the event is triggered

Sample:
define(['form'], function(form) {
  form.submitOnChange($('#country')); //Submit the parent form when the country is selected
  form.submitOnChange('#country', function(){...}); //Run the callback function and then submit the parent form when the country is selected
});

submitOnBlur

.submitOnBlur(selector)

Description:

Submit the parent form when event blur is triggered.

Parameters:

selector: String

Sample:
define(['form'], function(form) {
  form.submitOnBlur('#name'); //Submit the parent form when the form element loses focus
});

focus

.focus(selector)

Description:

Focus on selected element. If the device is mobile, it calls scrollIntoView function.

Parameters:

selector: String

Sample:
define(['form'], function(form) {
  form.focus('#input'); //Focus on the element #input
});

validate

.validate(selectorOrDocElement[, object])

Description:

Validate the form using almost all the html5 attributes validate spec.

Parameters:

selectorOrDocElement: doc-amd object or String //A CSS selector. Note that, if it is a class name with dots, the dots must be escaped. E.g.: doc(".my\\.class")

object: Object //An object with the properties messages ("required", "min", "maxlength", "pattern" or "email"), success (function callback) or error (function callback)

Sample:
define(['form'], function(form) {
  form.validate($('#form')); //Validate the form with default messages
  form.validate('#form', {
    messages: {
      'required': 'Field required.',
      'min': 'Enter a value greater than or equal to {0}.',
      'maxlength': 'Enter a value with max length less than or equal to {0}.',
      'pattern': 'Enter a valid value.',
      'email': 'Enter a valid email address.'
    }, //Validate the form with this messages
    success: function(){
      // success callback
    },
    error: function(){
      // error callback
    }
  });
});
Default messages:
 required: This field is required
 min: Please enter a value greater than or equal to {0}
 maxlength: Please enter a value with max length less than or equal to {0}
 pattern: Please enter a valid value
 email: Please enter a valid email address

appendMessage

.append(selector, text)

Description:

Append validation messages

Parameters:

selector: String

text: String

Sample:
define(['form'], function(form) {
  form.append('label[for="date"]', 'dd/mm/yyyy'); //This will append <span class="message">dd/mm/yyyy</span>. Note that this element will be removed when the user starts to type another value.
});

removeValidationErrors

.removeValidationErrors(selector)

Description:

Removes all validation messages from selected form

Parameters:

selector: String

Sample:
define(['form'], function(form) {
  form.removeValidationErrors('#form'); //This will remove all validation messages appended
});

License

Form-amd is released under the BSD. Have at it.


Copyright Šī¸ 2016 Elo7# form-amd

form-amd's People

Contributors

erikatakahara avatar fernandabernardo avatar mariofts avatar rapahaeru avatar tisvasconcelos 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.