npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

form-validatorjs

v0.0.3

Published

A simple form validator

Downloads

9

Readme

Form Validator

install
npm install form-validatorjs
use
var FormValidator = require('form-validatorjs');

if not using browerify/webpack simply copy form-validator.js and form-validator.css.

See the examples in example folder.

expected html
<div class="form">
    <div field-name='first-name'>
        <input type="text" placeholder="First Name">
    </div>
    <div  field-name='last-name'>
      <input type="text" placeholder="Last Name">
    </div>
    <div field-name='pswd'>
      <input type="text" placeholder="Password">
    </div>
    <div field-name='description'>
        textarea
    </div>
</div>

for the validator to work the add field-name to the parent of input fields, preferably the key you want to send to the server on successfull validation.

apply (Supports only input and textarea fields now)
var formValidator = new FormValidator({
    field: $('.form'),
    optionals: ['last-name'],
    validateAgainst: {
        'last-name': 'numberReg',
    },
    regObj: {
      numberReg: /^[0-9]*$/,
    },
    invalidMsg: {
      'last-name': 'This is not a number', // error message to display if invalid
    }
});

$('.button').click(function() {
    // use this function to handle user submit
    formValidator.run(function(data) {
        console.log(data); 
        // {isValid: false, result: [first-name: 'a', last-name:'b', pswd: 'c']};
    });
});

isValid will be returned true or false depending upon validation.result will contain key-value pair of fields.

Fields are validated onFocusout and reset when focused.

Options
  • field - the wrapper element that contains all the inputs.
  • optionals - fields in this array wont be tracked for empty field. But validation rules can still be applied.
  • validateAgainst - specify the fields to be validated as key-value pairs, with key as the field-name and value as one of the regexTypes from regObj.
  • regObj - specify the regexTypes for validation.
  • invalidMsg - specify the 'error' message if invalid.

eg: if you want to validate email-val field

<div field-name='email-val'>
    <input type="email" placeholder='Please enter your email'>
</div>
validateAgainst : {
    'email-val': 'emailReg'  // specifies to validate email-val against emailReg.
},
regObj: {
    emailReg: /some regex string/, // uses this string for email-val validation.
},
invalidMsg: {
    'email-val': 'Invalid Email'. // inserts this error message if invalid.
}

if the field is invalid,

<div class="invalid invalid-email-val">Invalid Email.</div>;

will be added inside the field='email-val' div.

Css

Copy the css from form-validator.css file for reference.

  • is-empty - class is applied if the field is empty. Optionals wont be affected.
  • is-valid - class is applied if the field is non-empty and does not break any validation rules.

isSame and tooltip Options

isSame
// formValidator options
isSame: [
  {'pswd': 'pswd-again', 'message': 'Passwords not same'},
  {'pri-addr': 'sec-addr','message': 'Address not same'}
],

isSame option matches the first two fields (eg. pswd and pswd-again) and if they dont match inserts

<div class="invalid">Passwords not same</div>;

inside the second field option div.

In the above case if the passwords dont match the div will be inserted inside the field-name=pswd-again div.

toolTip
// formValidator options
tooltip: {
  'pswd': 'The password should be atleast 6 digit long.',
},

This will show a tooltip alongside pswd field when you start typing.

For dynamic text inside tooltip, pass value as a function and return the message to be displayed. eg.

function checkLength($node, $tooltip) {
    var val = $node.val().length;

    if (val < 3) {
      $tooltip.addClass('danger');
      return 'Poor';
    } else if (val < 6) {
      $tooltip.addClass('warning');
      return 'Ok';
    } else {
      $tooltip.addClass('success');
      return 'Great';
    }
}

// formValidator options
tooltip: {
  'pswd': checkLength
}
Api
formValidator.empty() // empty all the fields