Code Monkey home page Code Monkey logo

validator's Introduction

Form validator

This is a vanilla javascript class for validating form fields easily.

Instroduction

First you have to compile the Validator.coffee using Coffee Script. documentation on installing and usage can be found at their website.

To compile Validator you'll have to write this in the terminal.

coffee -c -b path/to/Validator.coffee

This will create a compiled version of Validator. Please notice that the bare option is required to provide in order for it to work properly.

The whole concept behind this validator is that it's stupid. I doesn't do anything like adding classes, showing error messages or similar. That process of notifying the user it's totally up to you, and should be up to you.

Example Usage

Markup

Each input field with a data-validation-rules attribute gets validated.
Validator returns an array with the fields that did not validate.

Each rule is seperated with a | (pipe). Some rules requires a parameter seperated by a : (colon).

<form action="/" method="POST" id="form">
	<input name="name" data-validation-rules="required">
	<input name="age" data-validation-rules="required|numeric">
</form>

Example

var form = document.getElementById('form');

form.addEventListener('submit', function (e) {
	var validator = new Validator(form);
	
	if (validator.length > 0) {
		e.preventDefalut();
		
		for (i=1;i<validator.lenght;i++) {
			validator[i].className = validator[i].className + " error";
		}
	}
			
});

Example using jQuery

$('#form').on('submit', function (e) {
	var validator = new Validator(this);
	
	if (validator.length > 0) {
		e.preventDefalut();
		
		$.each(validator, function () {
			$(this).addClass('error');
		});
	}
});

Validation rules

  • required
  • min:{num}
  • max:{num}
  • excact:{num}
  • email
  • regex:{pattern}
  • numeric
  • alpha
  • alphanumeric
  • boolean ("0", "1", "true", "false")

If the required rule is not set, the field won't be validated unless it got a value. This means that a field only is validated as failed if the field has a value and dosent match the rule pattern.

Validating a single input

To validate a single input field you can use the validateField method which accepts a input field, just like with the form example above.

This method returns boolean and validates agaist the data-validation-rules attribute.

Extending

To extend Validator you simply need to pass in a functions that follows the rule pattern, like this:

Validator.prototype.validatemyrule = function (field, rule) {
	return field.value.trim() === 'foobar';
}

In the rule above the rule myrule has been made available for your fields. All rules follows the pattern of validate{rule} where {rule} is replaced with the name of your rule.

That means that you can now use your custom rule like so:

<input type="text" name="foo" data-validation-rules="required|myrule">

License

Validator is published under the MIT license, which means that you can do pretty much enything you wanna do with it. See the LICENSE file for more information.

validator's People

Contributors

tkjaergaard avatar

Watchers

James Cloos avatar  avatar  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.