Validating a textbox in javascript

In order to create a set of rules we use a Regular Expression (Regex).A Regular Expression is a way of matching an input to a set of rules.Here is a tutorial for building your own Javascript validation that you can use on any input on your website or web application.I’ve intentionally kept it as easy, simple and lightweight as possible.However, we can stop it from ever getting this far by validating on the client side too.So the first thing we need to do is to create a button to allow our form to be submitted.To add a validation prompt, add the following line of code to your form, just under your input field.This will clean up the prompt message once the user corrects their mistake.

Whether it’s filling in a contact form or entering profile details into a new application, handling input errors can be the difference between a good or a bad user experience.

Two examples are provided: email address validation and number validation. That is, the form will not be submitted until and unless the user provides valid data for both items.

The use of the fake button and the distinction between the fake button and real button is explained in the dropdown-nag entry.

For this tutorial I’m just going to include all of the HTML and Javascript in one file to keep things simple.

So to start off, create yourself a little HTML file, like this.

Leave a Reply