You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

popup-validation

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

popup-validation

Pure JavaScript/CSS library for validating DOM input fields

2.1.1
Source
npmnpm
Version published
Weekly downloads
8
166.67%
Maintainers
1
Weekly downloads
 
Created
Source

popup-validation

Pure JavaScript/CSS library for validating DOM input fields

Gemnasium Lib Size npm GitHub stars

Demo

JSFiddle

Install

npm install popup-validation --save

Read API

Usage

HTML

<link href="validation.css" rel="stylesheet">
<script src="validation.min.js"></script> 

<div>
  <label for="email">Email:</label>
  <input type="email" id="email" class="validate" 
         data-validate="required,email" />
</div>  

JS

Initialization

  • Track input fields inside of the <body>
validation.init();
  • Track input fields inside of a container or a form. Submit event will be prevented if there are any errors
validation.init("#myForm");
  • Set options: trigger events (when popups should be shown)
validation.init("#myForm", {
  events: ["change", "paste", "keyup"]
});

Usage

// Show errors
validation.highlight();

// Hide errors
validation.hide();

// Check if container is valid (body by default)
validation.isValid();

// Check if cotnainer is valid (body by default) + highlight
validation.validate();

Custom Class Validation

Some services like Braintree use iframes to control the inputs on a page. That also can be useful if some javascript logic sets and removes a certain class to/from a div or input field that indicates that the field is not validated.

HTML

<div id="customClassValidation" class="validate-class">
  Click at me to toggle custom class validation
</div> 

JS

validation.addClassValidation(".my-class-invalid");

// Test
document.getElementById("customClassValidation").addEventListener("click", e => {
  e.target.classList.toggle(".my-class-invalid");
});

Predefined Rules

  • required
  • email

The set of rules can be easily extended. Please take a look at the example

API

validation.init(el, options) => self

Initialize the validation fields inside of the el container. If el is a <form> element then submit event will be prevented if there are any errors

Returns: object - Self validation instance for chain calls

ParamTypeDescription
elElementContainer or <form> Element
optionsObject[Optional] Set of the properties

Default Options

{
  events: ["change", "paste", "blur", "keyup"]
}

Affects all input fields with validate class

data-validate attr can contain the list of the rules

Example:

<input class="validate" data-validate="required" />
<input type="email" class="validate" data-validate="required,email" />

validation.destroy(el) => self

Deactivate the validation fields inside of the el container

Returns: object - Self validation instance for chain calls

ParamTypeDescription
elElementContainer or <form> Element

validation.hide(el) => self

Hide all errors inside of the el container

Returns: object - Self validation instance for chain calls

ParamTypeDescription
elElementContainer or <form> Element

validation.highlight(el) => self

Highlight all errors inside of the el container

Returns: object - Self validation instance for chain calls

ParamTypeDescription
elElementContainer or <form> Element

validation.isValid(el) => boolean

Check if all input fields inside of the el container are valid

Returns: boolean - True if all input fields inside of the container are valid

ParamTypeDescription
elElementContainer or <form> Element

validation.validate(el) => boolean

Validate all input fields inside of the el container

Returns: boolean - True if all input fields inside of the container are valid

ParamTypeDescription
elElementContainer or <form> Element

validation.rules object

The set of the predefined rules

Rule signature

el => boolean

ParamTypeDescription
elElementinput field

Returns: boolean - True if the field is validated

Example of extending rules

JS

validation.rules["integer"] = {
  message: "Value is not an integer",
  method: el => {
    return el.value.length === 0 || !isNaN(parseInt(el.value, 10));
  }    
}

HTML

<input class="validate" data-validate="required,integer" />

validation.addClassValidation(selector) => self

Add class validation. For external libraries that can set/remove className of the element. For instance, braintree-hosted-fields-invalid class is set by braintree client library when iframe with the input field detects an error, More info here: https://developers.braintreepayments.com/guides/hosted-fields/styling/javascript/v2

Returns: object - Self validation instance for chain calls

ParamTypeDescription
selectorstringSelector that indicates that the field is invalid

Browsers support made by godban

IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
Opera
Opera
iOS Safari
iOS Safari
Chrome for Android
Chrome for Android
IE9+last 12 versionslast 10 versionslast 15 versionslast 14 versionslast 12 versionslast 13 versions

License

MIT

Keywords

validate

FAQs

Package last updated on 20 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.