Validating html page


While HTML5 form validation is typically about missing or invalid text inputs, there are other form element types that also require attention. Suppose you have a form on your website that at the bottom asks people to "accept the Terms and Conditions" or something similar.Basically you don't want the form to be submitted unless this is checked.Using vanilla Java Script we can prevent form submission as follows: All this does is confirm before submitting the form that the checkbox is checked.If not, an alert is displayed and focus is moved to the checkbox.Not the prettiest solution, but functional in all browsers with Java Script enabled.



The advantage for the user is that it's obvious whick element is causing the problem and there's no alert window that needs to be clicked away.At time of writing Safari does not enforce input fields.If you're using Safari or another unsupporting browsers all the examples will just display the Java Script alert box.As you would hope it is possible to customise the messages that are displayed by the browser with your own text, but this can only be done via Java Script.

You need to check the The block of Java Script below the form is assigning our custom error message to the checkbox when the page loads.

We know that the checkbox is unchecked by default so we need to tell the browser what message to display.