Code Monkey home page Code Monkey logo

validators's Introduction

validators

A very simple yet configurable error/hint message system for form validation.position:absolute so it doesn't affect your html layout.
image
Curently support validations(for input and textarea):

Validator Effect
required this input can't be ignored
not_required this input can be ignored
min_len(x) the length of input value can't be less than x
max_len(x) the length of input value can't be more than x
min_val(x) the input number can't be less than x
max_val(x) the input number can't be more than x
regex(REGEX_EXPRESSION) the input value must match REGEX_EXPRESSION

requirements:

1.jquery 1.11 (or maybe smaller, you could try)

usage:

Is there anything better than an real example? No, so let's start here:

<form enctype="multipart/form-data" method="post">
  <table>
    <tr>
      <td><label for="id_username">账号</label></td>
      <td><input id="id_username" name="username" type="text" /> </td>
    </tr>
    <tr>
      <td><label for="id_password">密码</label></td>
      <td><input id="id_password" name="password" type="password" /> </td>
    </tr>
  </table>
  <input type="submit" value="提交"/>
</form> 

1.add jquery, validators.css and validators.js to your html page (like home.html).

2.config form_fields object in validators.js like this(hint or validators are optional):

var form_fields = {
  username :{
    hint       : 'enter your username(2 chars at least, 10 at most)', 
    validators : [required, min_len(2), max_len(10)]
  }, 
  password :{
    validators : [required, regex(/^[\da-zA-Z@$_!-]{6,32}$/)]
  }, 
}

Above code means that :
1)username and password are required.
2)When you in username, a green hint message enter your username(2 chars at least, 10 at most) will show.
3)There must be 2 characters at least and 10 at most in username.

What if a input is not required, but you still want some validations when user offer value? you need to add not_required.like:

validators : [not_required,  min_len(2), max_len(10)]

3.enjoy it.

validators's People

Contributors

xiangnanscu avatar

Stargazers

 avatar  avatar

Watchers

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