Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Experts,

I have a doubt in HTML5 asp.net

HTML
<form action="#" method="post" id="commentform" onsubmit="return false">
       <p class="comment-form-author">Name *<input id="author" name="author" type="text" value="" size="30" aria-required="true" required="required" /></p>
       <p class="comment-form-email">Email<input id="email" name="email" type="text" value="" size="30" /></p>

       <p class="comment-form-rating"><label for="rating">Rating</label>
       <select name="rating" id="rating" required="required">
           <option value="">Rate…</option>
           <option value="5">Perfect</option>
           <option value="4">Good</option>
           <option value="3">Average</option>
           <option value="2">Not that bad</option>
           <option value="1">Very Poor</option>
       </select>
       </p>


       <p class="form-submit">
       <input name="submit" type="submit" id="submit" value="Submit Review" />

       </p>
   </form>



On the above code i have set a required HTML5 validation on "author" input also the "select"


JavaScript
$(document).ready(function () {

    $("#submit").click(function () {

        var _rating = document.getElementById('rating').options[document.getElementById('rating').selectedIndex].value;
        var _author = document.getElementById('author').value;
        var _email = document.getElementById('email').value;
        var _comment = document.getElementById('comment').value;


        var data = [
           {
               "_author": _author, "_email": _email, "_rating": _rating, "_comment": _comment
           }];


        $.ajax(
             {
                 type: "POST",
                 url: "Item_desc.aspx/Save_Comment",
                 dataType: "json",
                 data: JSON.stringify({ Cust_Comments: data }),
                 contentType: "application/json; charset=utf-8",
                 success: function (json) {

                    

                 }

             });




         });

});



And i have used Ajax JSON for sending datas to the code behind in asp.net.....

But when i click the button then the validation will catch but it also fire the the click event and send the values to the code behind....

I need to stop fire the click event when the validation catches...

Thanks

Dileep
Posted

XML
Try this:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>


    <title></title>

    <script type='text/javascript'>


var btnClick =function () {


        var _rating = document.getElementById('rating').options[document.getElementById('rating').selectedIndex].value;
        alert(_rating);
        var _author = document.getElementById('author').value;
        var _email = document.getElementById('email').value;

        if(_author!="" && _rating!="Rate" && _rating!="")
       {
            alert("fdgg");
            var data = [
               {
                   "_author": _author, "_email": _email, "_rating": _rating
               }];


            $.ajax(
                 {
                     type: "POST",
                     url: "Item_desc.aspx/Save_Comment",
                     dataType: "json",
                     data: JSON.stringify({ Cust_Comments: data }),
                     contentType: "application/json; charset=utf-8",
                     success: function (json) {

                        alert("dgsjhgdfdjhg");

                     }

                 });


 }else
 {
 return false;

 }

    return true;

};



</script>

</head>
<body>

 <form action="#" method="post" id="commentform" onsubmit="return false">
        <p class="comment-form-author">Name *<input id="author" name="author" type="text" value="" size="30" aria-required="true" required="required" />


        </p>
        <p class="comment-form-email">Email<input id="email" name="email" type="text" value="" size="30" /></p>

        <p class="comment-form-rating"><label for="rating">Rating</label>
        <select name="rating" id="rating" required="required">
            <option value="">Rate&hellip;</option>
            <option value="5">Perfect</option>
            <option value="4">Good</option>
            <option value="3">Average</option>
            <option value="2">Not that bad</option>
            <option value="1">Very Poor</option>
        </select>
        </p>


        <p class="form-submit">
        <input name="submit" type="submit" id="submit" value="Submit Review" onclick="btnClick();" />

        </p>
    </form>

</body>
</html>
 
Share this answer
 
Try this
$('#submit').click(function(){
if($("form")[0].checkValidity()) {
//your form execution code
}else console.log("invalid form");
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900