javascript - onsubmit validateForm funxtion is not working -
here validation used onsubmit event. how not working. want check textbox filed empty or not.
<!doctype html> <html> <head> <title>temple3</title> <link rel="stylesheet" type="text/css" href="temple3css.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </head> <body> <div id="popup-content" class='container'> <div id="container1"> <form id="form" class="form" onsubmit="return validateform()"> <h1>feedbak form</h1> <div class="content"> <div class="intro"></div> <div id="section0" > <div class="field roption"> <input type="radio" name="view" value="public" checked="checked"> <span>public</span> <input type="radio" name="view" value="private" ><span>private</span> </div> <div class="field category"> <label for>category:</label> <select class="dropdowncate" autofocus> <option value="bug">bug</option> <option value="1">1</option> <option value="2">2</option> </select> </div> <div class="field message"> <label for="comments">comments:</label> <textarea id="comments" name="comments" placeholder='your feedback here!' autofocus></textarea> </div> <div class="field"> <label for="email">email</label> <input type="email" id="email" name="email" placeholder="example@stevens.edu(optional)" autofocus> </div> <div class="field"><input type="submit" id="submit-feedback-button" autofocus></div> </div> </div> </form> </div> </div> <div id="popup-overlay-bg"> </div> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script> function validateform(){ var texbox=document.getelementbyid("comments").value; if(texbox == "") { document.getelementbyid("comments").focus(); // document.getelementbyid("comments").style.border = "1px solid #ff0000"; return false; } } $(document).ready(function(){ $("#feedback-button").click(function(){ openfeedbackform(); }); $("#popup-overlay-bg").click(function(){ closefeedbackform(); }); $("#submit-feedback-button").click(function(){ // closefeedbackform(); }); $(window).resize(function(){ updatefeedbackform(); }); $("#submit-feedback-button").click(function(){ var category=$(".dropdowncate").val(); var roption=$('input:radio[name=view]:checked').val(); var message=$("#comments").val(); var email=$("#email").val(); $.ajax({ type: "post", url: "feedback.php", data: "category="+category+ "&roption="+roption+ "&message="+message+ "&email="+email, success:function(result) { if(result=="true"){ $("#form").hide(); $("#container1").html("<h3>thank feedback asap</h3> "); } else{ $("#form").hide(); $("#container1").html("<h3> database error </h3>"); } } }); return false; }); }); function openfeedbackform(){ $("#popup-content").find('input:text').val(''); $("#popup-content").fadein(); $("#popup-overlay-bg").fadein(); updatefeedbackform(); } function updatefeedbackform(){ $popup=$("#popup-content"); var top = "50px"; var left = ($(window).width() - $popup.outerwidth()) / 2; $popup.css({ 'top' : top, 'left' : left }); } function closefeedbackform(){ $("#popup-content").fadeout(); $("#popup-overlay-bg").fadeout(); } </script> </body> </html>
earlier used same form validation. don't why not working here. tried debug function has not been call.
the problem doing ajax submit in button click event, not in form submit.
the click handler triggered before submit event handler, validation not have effect.
the solution use form submit event handler both validation , ajax call given below, , there no need have inline event handler onsubmit="return validateform()"
$("#form").submit(function () { if (validateform() === false) { return false; } var category = $(".dropdowncate").val(); var roption = $('input:radio[name=view]:checked').val(); var message = $("#comments").val(); var email = $("#email").val(); $.ajax({ type: "post", url: "feedback.php", data: "category=" + category + "&roption=" + roption + "&message=" + message + "&email=" + email, success: function (result) { if (result == "true") { $("#form").hide(); $("#container1").html("<h3>thank feedback asap</h3> "); } else { $("#form").hide(); $("#container1").html("<h3> database error </h3>"); } } }); return false; });
Comments
Post a Comment