Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control HTML5 validation balloon scroll?

I am working on a form with Twitter Bootstrap that uses the fixed on top nav menu (scrolls with page, but stays fixed on the top of it) and http://reactiveraven.github.com/jqBootstrapValidation/ for simple form validation styling.

However, the problem I am running into, is that when a validation balloon pops up saying to "Please fill out this field", the browser is scrolled up to where the input box is at the very top of the page. Under normal conditions, this is fine, however due to my menu setup it is hidden. Is there any way I can take over this scrolling and add an offset to it to prevent this from occuring?

like image 731
Cody Mays Avatar asked Jan 21 '13 04:01

Cody Mays


People also ask

How do I bypass HTML5 validation?

To ignore HTML validation, you can remove the attribute on button click using JavaScript. Uer removeAttribute() to remove an attribute from each of the matched elements.

How do I force a HTML5 form validation without submission?

on('click', function(event) { var isvalidate = $("#formID")[0]. checkValidity(); if (isvalidate) { event. preventDefault(); // HERE YOU CAN PUT YOUR AJAX CALL } }); }); Code described above will allow You to use basic HTML5 validation (with type and pattern matching) WITHOUT submitting form.

What are all validation controls in HTML5?

HTML5 Form Featuresautofocus, required, placeholder, autocomplete, pattern, minlength, readonly, list. meter, progress, datalist.

Is HTML5 validation accessible?

Short Answer. Yes the standard HTML5 validation is accessible and will pass WCAG2. 1 AA, but you can do a lot better with some JavaScript.


2 Answers

I'm working with the exact same setup and I've found that there's no easy way to do this with the form element. My hack was to watch for an invalid input/select/textarea and then have something fire from that. This isn't the best fix, especially with a lot of invalid inputs, but it helped me out with what I needed. Hopefully someone else will come by and be able to clean it up a bit.

function scrollToInvalid() {
  // Height of your nav bar plus a bottom margin
  var navHeight = 60;
  // Offset of the first input element minus your nav height
  var invalid_el = $('input:invalid').first().offset().top - navHeight;

  // If the invalid element is already within the window view, return true. If you return false, the validation will stop.
  if ( invalid_el > (window.pageYOffset - navHeight) && invalid_el < (window.pageYOffset + window.innerHeight - navHeight) ) {
    return true;
  } else {
    // If the first invalid input is not within the current view, scroll to it.
    $('html, body').scrollTop(invalid_el);
  }
}

$('input').on('invalid', scrollToInvalid);
like image 149
Devin McInnis Avatar answered Nov 03 '22 01:11

Devin McInnis


This can be done with simple css on the html element

html {
  scroll-padding-top: 100px;
}

Use scroll-padding-bottom: 100px; similarly if you have sticky footer

Works on all modern browsers except off course IE See here for more info https://css-tricks.com/almanac/properties/s/scroll-padding/

like image 26
Peter Kerr Avatar answered Nov 03 '22 02:11

Peter Kerr