πŸ“œ ⬆️ ⬇️

jVForms.js - cross-browser form checking polyfill


What is it?



  1. Automatic verification of HTML forms of type text | textarea.
  2. Verification of forms occurs on the attributes `required` and` pattern` and is designed primarily for browsers that do not support these attributes.
  3. Built-in set of templates for quick use.
  4. The ability to set the form submission handler to any HTML element by clicking both in the form and outside it.


So why?


')
Unfortunately, not all browsers are able to support such standard attributes as required and pattern , and unfortunately, not all and not always update browsers to the latest versions.

Internet ExplorerChromeOperaSafariFirefoxAndroidiOS
required10.05.0+9.6+x4.0+2.3+3.0+
pattern10.05.0+9.6+x4.0+2.3+3.0+


Well yes! So what to do?


You can use the competent jVForms.js polyfile , which does not take up much space, does not need settings (only sometimes), and does all the dirty work of checking forms for you.

But as?


Attach jVForms.min.js at the end of the document before the closing body tag and initialize it.

<script src="jVForms.min.js"></script> <script> jVForms.initialize(); </script> 


So what is next?


Add the attribute `required =β€œ required ”` in the form field, thus making the element required.

 <input type="text" name="field_1" value="" required="required"/> 


HTML required-attribute

Add the attribute `pattern =" RegExp "`, where `RegExp` is your regular expression for form validation.
 <input type="text" name="field_1" value="" required="required" pattern="^\d+$"/> 


HTML pattern-attribute

What is a regular expression?


If you are having difficulty adding a regular expression, you can use a pre-made set of templates by simply declaring them in a class.
 <input type="text" name="field_1" value="" class=""/> 


List of templates:



Templates can be declared any number, but only the first will work:
 <input type="text" name="field_1" value="" class="vf-numInt vf-wordUSLower vf-dataDMYus otherClass"/> 


When declaring the pattern attribute and template class, the template class will not work:
 <input type="text" name="field_1" value="" required="required" pattern="^\d+$" class="vf-numInt"/> 


If the `required =β€œ required ”` attribute is omitted and the template class or the `pattern` attribute is added, then this field is optional;
but if it is still filled, it will be checked:
 <input type="text" name="field_1" value="" class="vf-numInt"/> 


I do not fit submit!


To install a handler on any other element within the form, you need to add the vf-submit class to it:
 <form> <input type="text" required="required" name="name" class="vf-numInt"/> <input type="text" name="phone"/> <textarea name="area"></textarea> <span class="vf-submit"></span> </form> 


To install a handler on any other element outside the form, you must add the vf-submit class to it, and also associate this element with the form.
For this, the form must specify the id attribute or the name attribute and assign the value of this attribute as a class to an element to the handler:
 <form id="formId"> <input type="text" required="required" name="name" class="vf-numInt"/> <input type="text" name="phone"/> <textarea name="area"></textarea> </form> <span class="vf-submit formId" ></span> 


I want only errors.


jVForms is able to change the type of notification when the form is validated, for this there are three values:



The default value is β€œAll”. To change this value to β€œOff”, for example, you need to pass the following to the initialization function:
 <script> jVForms.initialize({ notice: 'Off' }); </script> 


And what is the result?


As a result, we received a small polyfil, which is simple as soap. Nowadays, in modern browsers, automatic field highlighting is built in, so when using jVForms.js , no conflicts or visual rejections occur.

Git: jVForms.js

Thanks for attention!

The demonstration is here!

Source: https://habr.com/ru/post/181782/


All Articles