0

Very basic and stupid question this might sound but i wanted to know if it is possible, if yes, then how.

These days, all new web browsers use HTML5 integrations, when i define

<input type="number" name="numericInput" />

<input type="email" name="email">

chrome / FF etc. will validate it for the input given, which would otherwise require some javascript or jQuery form validation methods.

What i want to know is, that, is there any method by which i can use form validation JS/jQuery only if these html5 features are not enabled in the browser for validation?
Thanks for any help!! :)

  • 1
    Are you looking for that? http://stackoverflow.com/questions/6731303/how-to-detect-html-5-compatibility-in-browser – Schminitz Oct 24 '13 at 13:27
  • @Schminitz : it's for browser compatibility, i wanted to know speciically for form validation, found it [**here**](http://stackoverflow.com/questions/8550642/check-if-a-browser-has-built-in-html5-form-validation).....thanks for the help! – user2915896 Oct 24 '13 at 13:53

1 Answers1

2
var hasBrowserValidation = (typeof document.createElement('input').checkValidity == 'function');

if(hasBrowserValidation){
    //validate form
} 
wmbtrmb
  • 307
  • 1
  • 17
  • can you please explain this? particularly, why are we creating `input` element here (i understand the checkValidity part, but rest is just flying of!!! :( ) – user2915896 Oct 24 '13 at 13:36
  • 2
    if type of input's method checkValidity is function - it means that browser has validation function inside. If browser doesn't have such method checkValidity will be undefined. You can use $('input')[0] instead of document.createElement('input'). Any input is ok. But if you serching all inputs by jQuery selector it's not so fast as create new element in document (that's why I use .createElement method) – wmbtrmb Oct 24 '13 at 13:40
  • so if i have a form, containing all `HTML5 input types`, will this `var hasBrowserValidation = (typeof document.createElement('input').checkValidity == 'function');` check for all of them or do i need to run in some loop? – user2915896 Oct 24 '13 at 13:54