Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a browser has built-in HTML5 form validation?

Tags:

How to check if a browser has built-in HTML5 form validation ability? By doing so, we don't need to apply jQuery form validation functions on browsers who can validate form by themselves.

[Solved] Based on ThinkingStiff's answer, here is a way:

if (typeof document.createElement("input").checkValidity == "function") {     alert("Your browser has built-in form validation!"); } 
like image 874
Ian Y. Avatar asked Dec 18 '11 08:12

Ian Y.


People also ask

Does HTML5 have form validation?

Using HTML5, we can create a form with built in validation (i.e. no javascript required). Earlier, we were using JAVASCRIPT to control form validation.

How do I check if a HTML form is valid?

Using the email type, we can check the validity of the form field with a javascript function called… checkValidity() . This function returns a true|false value. checkValidity() will look at the input type as well as if the required attribute was set and any pattern="" tag .

What is HTML5 validation?

With HTML5, form validation is a built-in feature. There are various validation attributes that come with HTML5. When form input is valid, the :valid CSS pseudoclass is applied to the element. If it's invalid, then the :invalid CSS pseudoclass is applied to the element.

Which HTML5 attribute is used for data validation?

The simplest HTML validation feature is the required attribute.


2 Answers

Simply check if the checkValidity() function exists:

Demo: http://jsfiddle.net/ThinkingStiff/cmSJw/

function hasFormValidation() {      return (typeof document.createElement( 'input' ).checkValidity == 'function');  }; 

Call it like this:

if( hasFormValidation() ) {     //HTML5 Form Validation supported }; 
like image 66
ThinkingStiff Avatar answered Oct 18 '22 19:10

ThinkingStiff


You can do this using Modernizr javascript http://www.modernizr.com/ . You can take help from this post html5 form validation modernizr safari . You can also take advantage of modernizr load

The basic syntax for Modernizr.load() is to pass it an object with the following properties:

  • test: The Modernizr property you want to detect.
  • yep: The location of the script you want to load if the test succeeds. Use an array for multiple scripts.
  • nope: The location of the script you want to load if the test fails. Use an array for multiple scripts.
  • complete: A function to be run as soon as the external script has been loaded (optional).

Both yep and nope are optional, as long as you supply one of them.

To load and execute the script in check_required.js, add the following block after modernizr.adc.js has been attached to the page (the code is in required_load.html):

<script>   Modernizr.load({     test: Modernizr.input.required,     nope: 'js/check_required.js',     complete: function() {       init();     }   }); </script> 

Source : http://www.adobe.com/devnet/dreamweaver/articles/using-modernizr.html

like image 36
Syed Bashar Avatar answered Oct 18 '22 20:10

Syed Bashar