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

text-input-validator

v0.1.3

Published

Progressive text input control validator

Downloads

12

Readme

text-input-validator

Progressive text input control validator.

Most validate-while-typing validators are annoying, they will flash red warning at you when you type first letter. Well of course, I havn't finished typing yet!

This validator is not the same. It can use different checking rules on typing and finished.

Usage

See file examples/index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../TextInputValidator.js"></script>
</head>

<body>
<p>Email: <input type="text" id="foo"> <span id="error-foo"></span></p>
<p><button onclick="forceSetError()">force set error</button></p>

<script>
const validator = new TextInputValidator({
  element: document.getElementById('foo'),

  input: /(?!.*[-_.+@]{2,})(?!^[-_.+@])^[-_.+a-z0-9]+(@([-.a-z0-9]+)?)?$/i,

  blur(email) {
    return /(?!.*[-_.+@]{2,})(?!^[-_.+@])^[-_.+a-z0-9]+@[-.a-z0-9]+\.[a-z]+$/i.test(email) && isRegistered(email).then(reged => !reged || 'this email has been taken')
  },

  onValidityChange(valid) {
    document.getElementById('error-foo').textContent = valid === true || valid === null ? '' : valid || 'invalid'
  }
})

function forceSetError() {
  validator.setValidity('force error')
}

function isRegistered(email) {
  // emulate a remote API call
  return new Promise(resolve => {
    setTimeout(() => {
      const registeredEmail = [
        '[email protected]',
        '[email protected]'
      ]

      resolve(registeredEmail.includes(email))
    }, 300)
  })
}
</script>
</body>
</html>

APIs

new TextInputValidate({ element, input, blur, onValidityChange })

element: the input element

input: RegExp or function. Optional. Rule for checking on input. It can return immediately or return a promise that resolves with value:

true: valid
false: invalid
null | undefined: initial state
other types: your custom state, e.g. invalid message, password strength, etc.

blur: Rule for checking on blur. Optional. Similar to input. Will also check the standard HTML5 validating attributes (such as "required" and "pattern") via HTMLInputElement.checkValidity()

onValidityChange(valid): callback function. called when validity changes.

valid: the result given by input and blur.

textInputValidate.check()

Validate manually. Returns promise.

textInputValidate.setRules({ input, blur })

Set new rules for input and blur

textInputValidate.setValidity(valid)

Set validity of the input control.

valid: same as input and blur option of constructor.

If validity is not equal to current state, onValidityChange callback will be called.

Returns a promise resolved with value valid

textInputValidate.on()

Turn on validator

textInputValidate.off()

Turn off validator

Build

npm run build

License

MIT