Coder Social home page Coder Social logo

vanillajsformvalidation's Introduction

Vanilla JS form validation

This project is a raw vanilla JS code sample for form validation. No external dependencies needed.

You can run it locally with your own web server or with python in the terminal:

$ python3 -m http.server 8080

To add a field that needs to be validated:

  • First add it to the HTML with the oninput and onblur event
  • Then add the configuration object in the inputs array in form.js

A configuration object looks like this:

fieldKey: {
    item: field dom element,
    value: function to fetch field value,
    isValid: boolean function to check if field is valid,
    sanitizedValue: () => function fetching field value and cleaning it before use,
    mandatory: boolean,
    pristine: boolean to tell if the field has been touched or not. Should be set to true by default,
    errorMessage: Error message to display if isValid returns false
}

TODO: On script init, append onblur & onchange event to the targeted input elements (so they don't need to be added manually in the HTML).

TODO: Support multiple error messages for a same field (eg. email too short, email format incorrect).

TODO: Allow error messages to be displayed next to the field instead of being all together inside a same element.

TODO: Add a class to inputs in error state.

vanillajsformvalidation's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.