Validating form fields with javascript

You can check out a demo below: See the Pen Instant Form Validation by Site Point (@Site Point) on Code Pen.

Once that scripting is implemented, we’re actually only a couple of skips and hops away from a complete polyfill.

The final thing to note is that each of the three fields has a corresponding v-model to connect them to values we will work with in the Java Script. The check Form logic (which is run on submit remember) checks for name and age only as movie is optional. Don’t forget that on a successful submission it’s going to POST to a temporary URL.

If they are empty we check each and set a specific error for each. See the Pen form validation 1 by Raymond Camden (@cfjedimaster) on Code Pen.

If you already have this enhancement (which works in all modern browsers back to IE7), and given that you have James is a freelance web developer based in the UK, specialising in Java Script application development and building accessible websites.

With more than a decade's professional experience, he is a published author, a frequent blogger and speaker, and an outspoken advocate of standards-based development.

We could just choose to ignore those browsers, but I think that would be a shame, especially when the problem is so simple to workaround.

It just means a bit more convoluted code – we have to get the collections of So there we have it – a simple and non-intrusive enhancement for instant form validation, providing accessible and visual cues to help users complete forms.

validating form fields with javascript-68validating form fields with javascript-87

Instead, these outlines are only shown after you’ve interacted with the field, which is basically (though not precisely) analogous to the instant, as it could easily be triggered on and off many types in rapid succession while typing, creating a flashing effect which would be annoying or impossibly distracting for some users.

Form validation is natively supported by the browser, but sometimes different browsers will handle things in a manner which makes relying on it a bit tricky. Let’s look at the HTML first: Let’s cover it from the top.

Even when validation is supported perfectly, there may be times when custom validations are needed and a more manual, Vue-based solution may be more appropriate. The form tag has an ID that we’ll be using for the Vue component.

Such a script is beyond the scope of this article, but if you wanted to develop it further, all of the basic blocks are there – testing whether a field should be validated, validating a field against a pattern and/or required, and binding trigger events.

I have to confess, I’m not sure it’s really worth it!

Leave a Reply