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

is-validate

v1.0.2

Published

NPM package to check form field validation.

Downloads

19

Readme

An NPM package to validate form fields

Img

I have published a post some days ago on How to Create and Publish Your First NPM Package. I realize that publishing an npm package is super easy. I have decided to create a form field validator in a very easy manner and ask developers to contribute to that.

Read on medium.com

I have created an npm library and published it to NPM and Github to validate form fields very easily. Let’s see what it can do?

Installation

npm install is-validate

Import

Method 1 to import

const { isEmail } =  require("is-validate")

Method 2 to import

const isValidate = require('is-validate');

All methods

- isEmail

Example:

//method 1
const { isEmail } =  require("is-validate")
console.log(isEmail('[email protected]'))
// true

// method 2
const isValidate = require('is-validate');
console.log(isValidate.isEmail('[email protected]'))
// true

All other functions will work, So I am not going to provide examples for each method

- isPhone

- isUrl

- isDate

- isTime

- isNumber

- isString

- isObject

- isBoolean

- isType

- isContain

Example:

console.log(isContains('Hello world', 'Hello'))

Package code

/*
 * This method accepts a string as its argument and returns true or false whether given string is email or not
*/
const isEmail = (email) => {
    let re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(String(email).toLowerCase());
    return re;
}

/*
 * This method accepts a string as its argument and returns true or false whether a given string is phone or not
*/
const isPhone = (phone) => {
    let is_phone = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im.test(phone);
    return is_phone;
}

/*
 * This method accepts a string as its argument and returns true or false whether given string is URL or not
*/
const isUrl = (string) => {
    let url;
    try { url = new URL(string); } catch (_) { return false; }
    return url.protocol === "http:" || url.protocol === "https:";
}
/*
 * This method accepts a string as its argument and returns true or false whether given string is date or not
*/
const isDate = (date) => {
    return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
}
/*
 * This method accepts a string as its argument and returns true or false whether given string is time or not HH: MM
*/
const isTime = (time) => {
    var isValid = /^([0-1][0-9]|2[0-3]):([0-5][0-9])$/.test(time);
    return isValid;
}

/*
 * This method given argument is number or not
*/
const isNumber = (number) => {
    if(typeof number == 'number'){ return true;}
    return false;
}

/*
 * This method given argument is a string or not
*/

const isString = (string) => {
    if(typeof string == 'string'){ return true;}
    return false;
}

/*
 * This method given argument is boolean or not
*/
const isBoolean = (boolean) => {
    if(typeof boolean == 'boolean'){ return true;}
    return false;
}

/*
 * This method given argument is object or not
*/
const isObject = (object) => {
    if(typeof object == 'object'){ return true;}
    return false;
}

/*
 * This method will return the type of argument passed
*/

const isType = (data) => {
    return typeof data;
}
/*
 * This method will return true or false based on a string containing a substring.
*/

const isContains = (string, substring) => {
    return string.includes(substring)
}

module.exports = {
    isEmail, isPhone, isUrl, isDate, isTime, isNumber, isString, isObject, isBoolean, isType, isContains
};

In the index.js file I have written code for validation and defined functions to perform validation.

Why contribute?

This is the first question that comes to mind whenever we think to start contributing to an open-source project. Refer to this URL to read why you should need to contribute to an open-source.

How to contribute?

This is a super easy package for form fields validation and I think it is very useful while developing any application. To contribute with me you need to do the following steps.

git clone https://github.com/harendra21/is-validate

  • Create a future branch

You have to create a new branch with the name feature-validate-email

  • Add a new function to index.js and export it

  • Test newly created function

To test your package locally (very important) you have to set the environment. Please refer to this article’s testing part.

  • Push code

  • Done

After that, I will manually review the code and merge it with master and publish changes to NPM.

Thank you for reading this article, Don’t forget to follow me.