javascript - Prevent form from submitting when certain fields are empty -


i appreciate on one. tried use code other questions, unfortunately doesn't work.

i trying prevent form submitting , show alert ("select dates continue")if 2 specific fields empty. in case "checkin" & "checkout"

this code first input field:

<input type="text" id="checkin" readonly="true" name="checkin" class="form-control search-input" placeholder="check-in date" data-provide="datepicker-inline" data-date-orientation="bottom" data-date-autoclose="true" data-date-start-date="now" data-date-format="<?php echo $javascriptlocal ?>" data-date-language="<?php echo $language ?>"required> <div class="input-group-addon search-icon-right" onclick="$('#checkin')[0].focus()" onfocus="$('#checkin')[0].focus()"> 

code second field:

  <input type="text" readonly="true" id="checkout" name="checkout" class="form-control search-input" placeholder="check-out date" data-provide="datepicker-inline" data-date-orientation="bottom" data-date-autoclose="true" data-date-start-date="now" data-date-format="<?php echo $javascriptlocal ?>" data-date-language="<?php echo $language ?>"/>  <div class="input-group-addon search-icon-right" onclick="$('#checkout')[0].focus()" onfocus="$('#checkout')[0].focus()"> 

i tried achieve javascript:

<script type="text/javascript"> function empty() {     var x;     x = document.getelementbyid("checkout").value;     if (x == "") {         alert("select dates continue");         return false;     }; } </script> 

however, not work. please me?

edit: javascript seems interfere code: suggestions?

<script>$(document).ready(function(){$(".gotop").on("click",function(){$("html, body").animate({scrolltop:$("body").offset().top},1000)});$("#child").on("change",function(){var a=$(this).val();if(a<=0){$(".child-age-1-container").fadeout("slow");$(".child-age-2-container").fadeout("slow");$(".child-age-3-container").fadeout("slow");$(".child-age-4-container").fadeout("slow");$("#child-age-1").val(0);$("#child-age-2").val(0);$("#child-age-3").val(0);$("#child-age-4").val(0)}else{if(a==1){$(".child-age-1-container").fadein("slow");$(".child-age-2-container").fadeout("slow");$(".child-age-3-container").fadeout("slow");$(".child-age-4-container").fadeout("slow");$("#child-age-2").val(0);$("#child-age-3").val(0);$("#child-age-4").val(0)}else{if(a==2){$(".child-age-1-container").fadein("slow");$(".child-age-2-container").fadein("slow");$(".child-age-3-container").fadeout("slow");$(".child-age-4-container").fadeout("slow");$("#child-age-3").val(0);$("#child-age-4").val(0)}else{if(a==3){$(".child-age-1-container").fadein("slow");$(".child-age-2-container").fadein("slow");$(".child-age-3-container").fadein("slow");$(".child-age-4-container").fadeout("slow");$("#child-age-4").val(0)}else{if(a>=4){$(".child-age-1-container").fadein("slow");$(".child-age-2-container").fadein("slow");$(".child-age-3-container").fadein("slow");$(".child-age-4-container").fadein("slow")}}}}}});$("#checkin").datepicker().on("changedate",function(){var a=$("#checkin").datepicker("getdate");a.setdate(a.getdate()+2);$("#checkout").datepicker("setdate",a);$("#checkout")[0].focus()});$(".btn-hotel-search").on("click",function(a){a.preventdefault();var j=$("#search-form");var g=$("#child-age-1").val();var f=$("#child-age-2").val();var e=$("#child-age-3").val();var d=$("#child-age-4").val();var b;var c=$("#child").val();var h="";for(b=1;b<=c;b++){h+=$("#child-age-"+b).val()+","}h=h.substr(0,h.length-1);j.append($('<input type="hidden" name="childages">').val(h));j.get(0).submit()})});</script> 

use html5 required attribute on input elements, form wont submitted if fields empty. learn more html5 validation please visit http://www.the-art-of-web.com/html/html5-form-validation/


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -