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

simple-input-validators

v1.2.1

Published

functions for validate enter data

Downloads

16

Readme

simple-input-validators

Functions for validate enter data

npm version

Table of Contents

Quick start

Install

We support all platforms.

npm

For module bundlers such as Webpack or Browserify.

npm i simple-input-validators

Include with <script>

  1. Download lib
  2. Add script to html
<script src="simple-input-validators.js"></script>
CDN

Recommended for learning purposes, you can use the latest version:

<script src="https://cdn.jsdelivr.net/npm/simple-input-validators/dist/lib/simple-input-validators.js"></script>

Recommended for production for avoiding unexpected breakage from newer versions:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/lib/simple-input-validators.js"></script>

Initialization

ES6

simple-input-validators as an ES6 module.

import {
    isShorterThanLimit,
    isLongerThanLimit,
    isGreaterThanLimit,
    isLessThanLimit,
    isWrittenValueEmpty,
    isMailInvalid,
    isNumberValid,
    errorDataHandler
} from 'simple-input-validators';

let hasError = true,
    inputRules = {
        minLengthRules: {
            limit: 1,
            message: 'is short'
        },
        maxLengthRules: {
            limit: 3,
            message: 'is long'
        },
        isEmptyRules: {
            message: 'is empty'
        },
        maxValueRules: {
            limit: 125,
            message: 'is greater'
        },
        minValueRules: {
            limit: 10,
            message: 'is less'
        },
        emailRules: {
            message: 'not valid email'
        },
        numberRules: {
            message: 'not valid number'
        },
    },
    commonErrorData = {
        hasError: false,
        message: ''
    },
    writtenValue = '55'

if (isShorterThanLimit(writtenValue, inputRules.minLengthRules)) {
    errorDataHandler(commonErrorData, {...inputRules.minLengthRules, hasError})
}

if (isLongerThanLimit(writtenValue, inputRules.maxLengthRules)) {
    errorDataHandler(commonErrorData, {...inputRules.maxLengthRules, hasError})
}

if (isWrittenValueEmpty(writtenValue)) {
    errorDataHandler(commonErrorData, {...inputRules.isEmptyRules, hasError})
}

if (isGreaterThanLimit(writtenValue, inputRules.maxValueRules)) {
    errorDataHandler(commonErrorData, {...inputRules.maxValueRules, hasError})
}

if (isLessThanLimit(writtenValue, inputRules.minValueRules)) {
    errorDataHandler(commonErrorData, {...inputRules.minValueRules, hasError})
}

if (isMailInvalid(writtenValue)) {
    errorDataHandler(commonErrorData, {...inputRules.emailRules, hasError})
}

if (!isNumberValid(writtenValue)) {
    errorDataHandler(commonErrorData, {...inputRules.numberRules, hasError})
}

Node

simple-input-validators as a Node.js module

const { isShorterThanLimit,
        isLongerThanLimit,
        isGreaterThanLimit,
        isLessThanLimit,
        isWrittenValueEmpty,
        isMailInvalid,
        isNumberValid,
        errorDataHandler
} = require('simple-input-validators');

let hasError = true,
    inputRules = {
        minLengthRules: {
        limit: 1,
            message: 'is short'
        },
        maxLengthRules: {
            limit: 3,
            message: 'is long'
        },
        isEmptyRules: {
            message: 'is empty'
        },
        maxValueRules: {
            limit: 125,
            message: 'is greater'
        },
        minValueRules: {
            limit: 10,
            message: 'is less'
        },
        emailRules: {
            message: 'not valid email'
        },
        numberRules: {
            message: 'not valid number'
        },
    },
    commonErrorData = {
        hasError: false,
        message: ''
    },
    writtenValue = '55'

if (isShorterThanLimit(writtenValue, inputRules.minLengthRules)) {
    errorDataHandler(commonErrorData, {...inputRules.minLengthRules, hasError})
}

if (isLongerThanLimit(writtenValue, inputRules.maxLengthRules)) {
    errorDataHandler(commonErrorData, {...inputRules.maxLengthRules, hasError})
}

if (isWrittenValueEmpty(writtenValue)) {
    errorDataHandler(commonErrorData, {...inputRules.isEmptyRules, hasError})
}

if (isGreaterThanLimit(writtenValue, inputRules.maxValueRules)) {
    errorDataHandler(commonErrorData, {...inputRules.maxValueRules, hasError})
}

if (isLessThanLimit(writtenValue, inputRules.minValueRules)) {
    errorDataHandler(commonErrorData, {...inputRules.minValueRules, hasError})
}

if (isMailInvalid(writtenValue)) {
    errorDataHandler(commonErrorData, {...inputRules.emailRules, hasError})
}

if (!isNumberValid(writtenValue)) {
    errorDataHandler(commonErrorData, {...inputRules.numberRules, hasError})
}

Browser

Exports a global variable called simpleInputValidators. Use it like this

Connect to html file <script src="https://cdn.jsdelivr.net/npm/simple-input-validators/dist/lib/simple-input-validators.js" ></script>

<script>

    let hasError = true,
        inputRules = {
            minLengthRules: {
                limit: 1,
                message: 'is short'
            },
            maxLengthRules: {
                limit: 3,
                message: 'is long'
            },
            isEmptyRules: {
                message: 'is empty'
            },
            maxValueRules: {
                limit: 125,
                message: 'is greater'
            },
            minValueRules: {
                limit: 10,
                message: 'is less'
            },
            emailRules: {
                message: 'not valid email'
            },
            numberRules: {
                message: 'not valid number'
            },
        },
        commonErrorData = {
            hasError: false,
            message: ''
        },
        writtenValue = '55'

    if (simpleInputValidators.isShorterThanLimit(writtenValue, inputRules.minLengthRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.minLengthRules, hasError})
    }

    if (simpleInputValidators.isLongerThanLimit(writtenValue, inputRules.maxLengthRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.maxLengthRules, hasError})
    }

    if (simpleInputValidators.isWrittenValueEmpty(writtenValue)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.isEmptyRules, hasError})
    }

    if (simpleInputValidators.isGreaterThanLimit(writtenValue, inputRules.maxValueRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.maxValueRules, hasError})
    }

    if (simpleInputValidators.isLessThanLimit(writtenValue, inputRules.minValueRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.minValueRules, hasError})
    }

    if (simpleInputValidators.isMailInvalid(writtenValue)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.emailRules, hasError})
    }

    if (!simpleInputValidators.isNumberValid(writtenValue)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.numberRules, hasError})
    }

</script>

AMD

simple-input-validators as an AMD module. Use with Require.js, System.js, and so on.

  1. Download lib
  2. Connect to your module loader
requirejs(['simple-input-validators'], function(simpleInputValidators) {
    let hasError = true,
        inputRules = {
            minLengthRules: {
                limit: 1,
                message: 'is short'
            },
            maxLengthRules: {
                limit: 3,
                message: 'is long'
            },
                isEmptyRules: {
                message: 'is empty'
            },
            maxValueRules: {
                limit: 125,
                message: 'is greater'
            },
            minValueRules: {
                limit: 10,
                message: 'is less'
            },
            emailRules: {
                message: 'not valid email'
            },
            numberRules: {
                message: 'not valid number'
            },
        },
        commonErrorData = {
            hasError: false,
            message: ''
        },
        writtenValue = '55'

    if (simpleInputValidators.isShorterThanLimit(writtenValue, inputRules.minLengthRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.minLengthRules, hasError})
    }

    if (simpleInputValidators.isLongerThanLimit(writtenValue, inputRules.maxLengthRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.maxLengthRules, hasError})
    }

    if (simpleInputValidators.isWrittenValueEmpty(writtenValue)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.isEmptyRules, hasError})
    }

    if (simpleInputValidators.isGreaterThanLimit(writtenValue, inputRules.maxValueRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.maxValueRules, hasError})
    }

    if (simpleInputValidators.isLessThanLimit(writtenValue, inputRules.minValueRules)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.minValueRules, hasError})
    }

    if (simpleInputValidators.isMailInvalid(writtenValue)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.emailRules, hasError})
    }

    if (!simpleInputValidators.isNumberValid(writtenValue)) {
        simpleInputValidators.errorDataHandler(commonErrorData, {...inputRules.numberRules, hasError})
    }
});

Methods

isShorterThanLimit

function that check is write value shorted than limit

Params

  • writtenValue
    • Type: number,string
  • validatorSettings
    • Type: ValidatorSettingsProps

Returns

  • boolean

Example

isShorterThanLimit('abcd',{limit: 5})
// => true

isShorterThanLimit(['a','b','c','d'],{limit: 5})
// => true

isLongerThanLimit

function that check is write value longer than limit

Params

  • writtenValue
    • Type: number,string
  • validatorSettings
    • Type: ValidatorSettingsProps

Returns

  • boolean

Example

isLongerThanLimit('abcd',{limit: 3})
// => true

isLongerThanLimit(['a','b','c','d'],{limit: 3})
// => true

isGreaterThanLimit

function that check is write value greater than limit

Params

  • writtenValue
    • Type: number,string
  • validatorSettings
    • Type: ValidatorSettingsProps

Returns

  • boolean

Example

isGreaterThanLimit(5,{limit: 3})
// => true

// install package: https://www.npmjs.com/package/get-array-sum
import getArraySum from 'get-array-sum';

const arraySum = getArraySum([1,2,3]), //6
      shouldValidateArray = !isNaN(arraySum) //true

if (shouldValidateArray) {
  isGreaterThanLimit(arraySum,{limit: 3}) // true
}

isLessThanLimit

function that check is write value less than limit

Params

  • writtenValue
    • Type: number,string
  • validatorSettings
    • Type: ValidatorSettingsProps

Returns

  • boolean

Example

isLessThanLimit(2,{limit: 3})
// => true

// install package: https://www.npmjs.com/package/get-array-sum
import getArraySum from 'get-array-sum';

const arraySum = getArraySum([1,2,3]), //6
      shouldValidateArray = !isNaN(arraySum) //true

if (shouldValidateArray) {
  isLessThanLimit(arraySum,{limit: 10}) // true
}

isWrittenValueEmpty

function that check is write value empty

Params

  • writtenValue
    • Type: number,string

Returns

  • boolean

Example

isWrittenValueEmpty('')
// => true

isWrittenValueEmpty([])
// => true

isMailInvalid

function that check is write mail invalid

Params

  • writtenValue
    • Type: number,string,null

Returns

  • boolean

Example

isMailInvalid('ab')
// => true

isMailInvalid('[email protected]')
// => false

isNumberValid

Validate number by rules

  1. negative values
  2. dots
  3. allowableSymbols

Params

  • writtenValue
    • Type: string,number
    • Description: data
  • numberRules
    • Type: NumberValidatorRulesProps
    • Description: paramDesc

Returns

  • boolean

Example

isNumberValid(1) // => true
isNumberValid('1') // => true

isNumberValid(1.5) // => true
isNumberValid(1.5, {shouldLockFloatNumber: true} // => false

isNumberValid(-1) // => true
isNumberValid(-1, {shouldLockNegativeNumber: true} // => false

isNumberValid('12a') // => false
isNumberValid('12a', {allowableSymbols: ['a']}) // => true

isNumberValid('1ab') // => false
isNumberValid('1ab', {customRegExp: /[1-9][a-z]+/}) // => true

errorDataHandler

function update commonErrorData values to new input props

Params

  • commonErrorData

    • Description: object that have all error state for single input
  • propsToUpdate

    • Description: new props that must be update in commonErrorData

Example

const commonErrorData = {
         hasError: false,
         message: ''
      },
      validatorRules = {
         message: 'errorMessage'
      }

errorDataHandler(commonErrorData, {...validatorRules, hasError: true}) // => void

console.log(commonErrorData) // => {
         hasError: true,
         message: 'errorMessage'
}

Author

webster6667