Is it possible to use the required property in IE?

I have a code like this:

<input required> 

In good browsers, I can use jquery as follows to add an error class to the error:

 $('input[required]').addClass('error'); 

However, it does not work in IE. I will resort to it otherwise, but it is not so neat ...

HTML:

 <input data-required=true> 

JavaScript:

 if( $('input').data('required') ) $('input').addClass('error'); 

Any ideas?

Thanks,
Will

EDIT:

Yes, I use HTML5.

IE really interprets this:

 <input required> 

as:

 <input required=""> 

So, you can check that like this:

 if( $('input').prop() == undefined ) 

But then again, this is a slightly messy way of doing things, especially considering that this is only IE. This code works great in all other browsers.

I basically ask if there is a validation method that is cross browser and neat. Maybe I'm a little perfectionist !;)

+6
source share
2 answers

using the data- attribute data- safe and your page will be valid if you use them. But you need qoutation for your value. I use data attributes all the time. We support IE7.

 <input data-required="true"> 

Or even you can leave the value in such cases:

 <input data-required> 

If you often use data attributes, it’s good to know that you can use the dataset property in the Chrome (and Firebug) developer tools to access element data attributes through the console. It is not yet safe to use the dataset API in your code. it is not supported in IE8

in console:

 $$('input')[0].dataset 

and get the values ​​and properties of data attributes!

+4
source

Try using required="required" , this can make older browsers better.

IMPORTANT NOTE: It is still very important to write <input required>

+2
source

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


All Articles