1.0.12 • Published 7 years ago

validate-manager v1.0.12

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

Validate Manager - Form Validation

A JavaScript tool for form validation.

DEMO

http://hassanhibbert.com/validate-manager/

Download project

If you wish to download the latest project you can use the link below.

Download Project Here

Or get specific files.

You can download the styles and the minified version of ValidateManager.

NPM Install

First make sure you have installed the latest version of node.js (You may need to restart your computer after this step).

Use the command line below to install

npm install validate-manager

Setup

  • Load the script into your page.
  • Add a data-vm validate attribute to your form element (List of attribute methods below).
  • Pass the form name to ValidateManager('formname').
  • Then call the validate method.

Sample styles for error messages can be found in the demo/css directory of the project.

Note: Form element names are required.

<form name="myform">
  <input name="field1" data-vm-required="true">
</form>
<script src="validate.manager.js"></script>
<script>
  ValidateManager('myform').validate();
</script>

HTML Attributes

The below table displays all of the default attribute validation rules.

AttributeTypeDescriptionMethod
data-vm-letters-only="true"BooleanTest for valid alphabetic characterslettersOnly
data-vm-required="true"BooleanTest if the form field has a valuerequired
data-vm-email="true"BooleanTest for a valid emailemail
data-vm-minlength="2"NumberTest if the value is at a minimum lengthminlength
data-vm-maxlength="2"NumberTest if the value is at a maximum lengthmaxlength
data-vm-range="[5,10]"ArrayTest if the value is between a range of two numbersrange
data-vm-equal-to="elementName"StringTest if the value is equal to another elements valueequalTo
data-vm-digits="true"BooleanTest if the value is a valid digitdigits

HTML Attributes - Usage

Input (lettersOnly and required)

Using the attribute data-vm-required="true" and data-vm-letters-only="true"> will make the input field required and the value to be validated for alphabetic letters only.

<input name="fullname" type="text" data-vm-required="true" data-vm-letters-only="true">
Input (email)

Using the attribute data-vm-email="true" will validate input for a valid email address.

<input name="email" type="text" data-vm-email="true">
Input Checkbox (required and minlength)

When validating a group such as checkboxes or radio buttons. Only the first form element in the group needs the validation attributes. Error messages will be displayed after the last element in the group.

Tip: using data-vm-minlength with checkboxes allows validation for that set amount to be selected

<input type="checkbox" name="newsletter" id="sports" value="sports" 
       data-vm-required="true" 
       data-vm-minlength="2"/>
<input type="checkbox" name="newsletter" id="computers" value="computers"/>
<input type="checkbox" name="newsletter" id="photography" value="photography"/>

ValidateManager - config options

PropertyTypeDefaultDescription
formNameStringnullPass the name of the form to this property.
onChangeHandlerFunctionnullCallback for when a onchange event is triggered.
onSubmitHandlerFunctionnullCallback for when a onsubmit event is triggered.
debugBooleanfalseThis will prevent the form from being submitted.
validateOnChangeBooleantrueFlag for validating when onChange is triggered.
resetFormOnSubmitBooleantrueFlag for resetting the form when onsubmit is triggered and form is valid.

ValidateManager - config options structure

{
  formName: 'formname',
  onSubmitHandler: function (event, data, form) {
    // code here..
  },
  debug: false,
  validateOnChange: true,
  resetFormOnSubmit: true
}

ValidateManager Usage

There are three ways you can setup ValidateManager().

Example - #1 ValidateManager(formName)

This is the simplest way.

ValidateManager('formname').validate();
Example - #2 ValidateManager(formName, options)

This is where you can add a second argument which would be the config options.

var form = ValidateManager('formname', {
  // config options here...
});

form.validate();
Example - #3 ValidateManager(options)

This is where you can just pass in one config object. Make sure to assign the form name to the property formName.

var form = ValidateManager({
  formName: 'formname', // <-- Required form name
  // config options here...
});

form.validate();

ValidateManager - config options onSubmitHandler

var form = ValidateManager('formname', {
  onSubmitHandler: function (event, data, form) {
    // form.submit();
    // Submit form or process the `data` object
    // You can include your own Ajax calls within here as well
  }
});

form.validate();

onSubmitHandler callback function arguments

  • event
    • The onSubmit event object
  • data
    • An object containing the values of the form
  • form
    • The main form element

Methods

.validate(options)

The validate method kicks off the initialization of the form. Validate options can be used to programmatically set validation rules or create custom error messaging.

options structure
{
  'form-element-name': {
    
    // Setting rules
    rules: {
      required: true,
      equalTo: 'email' // other form element name to compare
    },
    
    // Creating custom error messages
    message: { 
      equalTo: 'Email address does not match.',
      required: 'Please enter the same email.'
    }
  }
}
Example - custom error message
<form name="myform">
  <input type="text" name="email" 
         data-vm-required="true" 
         data-vm-email="true">
         
  <input type="text" name="confirm-email" 
         data-vm-required="true" 
         data-vm-equal-to="email">
</form>

<script>
  var form = ValidateManager('myform');
  
  form.validate({
    'confirm-email': {
      message: { 
        equalTo: 'Email address does not match.',
        required: 'Please enter the same email.'
      }
    }
  });
</script>
Example - set rules programmatically
<form name="myform">
  <input type="text" name="firstname">
  <input type="text" name="lastname">
  <input type="text" name="email">
</form>

<script>
  var form = ValidateManager('myform');
  
  // set validation rules
  form.validate({
    'firstname': {
      rules: { required: true, lettersOnly: true, minlength: 2 }
    },
    'lastname': {
      rules: { required: true, lettersOnly: true, minlength: 2 }
    },
    'email': {
      rules: { email: true, required: true }
    }
  });
</script>

.destroy()

The destroy method would be useful for single page applications when the events would need to be cleaned up during the destroy phase of the app.

Example
<form name="myform">
  <input type="text" name="fullname" data-vm-required="true">
</form>

<script>
  var form = ValidateManager('myform');
  form.validate();
  
  // Sometime later in the application when you need to
  // clean up some event handlers.
  form.destroy();
</script>

.addMethod(methodName, callback, errorMessage)

With addMethod() you are able to add your own custom method for validation.

Note: When creating a method name that is camel case .addMethod('exampleMethod'...) use Kebab Case when implementing html attributes data-vm-example-method="true"

Example
<form name="myform">
  <input type="text" name="number-five" data-vm-is-five="true">
</form>

<script>
  var form = ValidateManager('myform');
  
  form.addMethod('isFive', function (value1, value2, formElement) {
    return parseInt(value1) === 5;
  }, 'Not equal to 5.');
  
  form.validate();
</script>

Details about the callback arguments

  • methodName
    • The name of your custom method
  • callback
    • Function used to test a value, taking three arguments:
      • value1
        • The value from the form field
      • value2
        • The value to test against.
        • Example data-vm-minlength="5" or rules: { minlength: 5 }. value2 will be 5.
      • formElement
        • The main form element.
  • errorMessage
    • The error message what will be displayed when validation fails
1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago