0

I'm struggling create validation for inputs in jQuery. I have a btn to create a row. When I click this btn, you will see a pop up page which include 2 text box and 1 dropdown. I want to check if these tow text box is not empty then its going to submit. This is my creating btn which open pop up:

<div class="col-lg-12 panel">
    <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#editModal">
        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> @Resource.Add_new_item
    </a>
</div>

And this is what in this pop up:

<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            @using (Html.BeginForm("Edit", "ApplicationDetailList", new {page = Model.PageNumber,filter.ListType, filter.PackageName }, FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="addModalLabel">@Resource.Add_new_item </h5>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>@Resource.Application_category_tittle *</label>
                            <input name="id" id="id" class="form-control" style="direction: ltr;"/>
                            @*@Html.ValidationMessageFor(m => m[0].Id);*@
                        </div>
                        <div class="form-group">
                            <label>@Resource.List_type *</label>
                            @Html.DropDownList("listType", ApplicationDetailListType.Solr.ToSelectListItems(false, filter.ListType), new { @class = "form-control"})                           
                            @*<input name="tags" class="form-control" style="direction: ltr;"/>*@
                        </div>
                        <div class="form-group">
                            <label>@Resource.Package_name *</label>
                            <input name="packageName" class="form-control" style="direction: ltr;"id="packageName" />
                            @*@Html.ValidationMessageFor(m=>m[0].PackageName);*@
                        </div>
                        <label id="validation">

                        </label>
                        <div class="form-group">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-success btn-edit-save">@Resource.Save</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">@Resource.Cancel</button>
                    </div>
                </div>
            }
        </div>
    </div>

And this this is my section script which I made function:

var edit = $("#editModal");
           // var packageName = $("#packageName").val();
            //var id = $("#id").val();
            //packageName.length > 0 && id.length > 0 ?
                 $(".btn-edit-save").click(function() {
                    $("form", edit).submit();
                });

the problem is here:

 $(".btn-edit-save").click(function() {
                    $("form", edit).submit();
                });

I don't want to submit if inputs are empty and just show message they are empty. Many thanks

1
  • can you make a fiddle please Commented May 14, 2019 at 9:16

4 Answers 4

0

If you don't want to submit if inputs are empty, you can use Ajax.Beginform instead of Html.Beginform. You can define a javascript function and trigger it before submit with Ajax.BeginForm's OnBegin attribute.

But you should also add a reference to the jquery.unobtrusive-ajax.min.js.

<html>
<head>
    <script src="/Scripts/jquery-[yourVersion].min.js" type="text/javascript"></script>
    <script src="/Scripts/jquer.unobtrusive-ajax.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function YourValidationFunc() {
            //add your validation code
            return true;
        }
    </script>
</head>
<body>
    <div>
        @using (Ajax.BeginForm("Edit", "ApplicationDetailList", new AjaxOptions
            {
                HttpMethod = "POST",
                OnBegin = "return YourValidationFunc();"
            }, new { page = Model.PageNumber,filter.ListType, filter.PackageName }))
        {
            @Html.AntiForgeryToken()
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addModalLabel">@Resource.Add_new_item </h5>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>@Resource.Application_category_tittle *</label>
                        <input name="id" id="id" class="form-control" style="direction: ltr;"/>
                        @*@Html.ValidationMessageFor(m => m[0].Id);*@
                    </div>
                    <div class="form-group">
                        <label>@Resource.List_type *</label>
                        @Html.DropDownList("listType", ApplicationDetailListType.Solr.ToSelectListItems(false, filter.ListType), new { @class = "form-control"})                           
                        @*<input name="tags" class="form-control" style="direction: ltr;"/>*@
                    </div>
                    <div class="form-group">
                        <label>@Resource.Package_name *</label>
                        <input name="packageName" class="form-control" style="direction: ltr;"id="packageName" />
                        @*@Html.ValidationMessageFor(m=>m[0].PackageName);*@
                    </div>
                    <label id="validation">

                    </label>
                    <div class="form-group">
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="submit" name="Submit" class="btn btn-success btn-edit-save" value="@Resource.Save" />
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">@Resource.Cancel</button>
                </div>
            </div>
        }
    </div>
</body>
</html>

In the example above, OnBegin will be triggered via:

<input type="submit" name="Submit" class="btn btn-success btn-edit-save" value="@Resource.Save" />
Sign up to request clarification or add additional context in comments.

Comments

0

You can add 'required' attribute to input elements like as below

  <input type="text" required name="anyName" />

if explicit you want to check then check by selector. $("#Selectorname").val() //will give you value if its there

Comments

0

$.submit method in Jquery submits the form.

Before you call submit method you need to validate

$(".btn-edit-save").click(function() {
             if(validate(fname)==true)
               {
                 $("form", edit).submit();
                }
               else alert("empty fname");
            });

Comments

0

You can append a label error belong text box and border error fields as

$(".btn-edit-save").click(function() {
     var title = $('#id').val();
     var packageName = $('#packageName').val();
     var isValid = true;
     if(title == ""){
        $('#id').after("<label class='error'>Title is required</label>");
        $('#id').css('border', '1px solid red');
        isValid = false;
     }
     if(packageName == ""){
        $('#packageName').after("<label class='error'>PackageName is required</label>");
        $('#packageName').css('border', '1px solid red');
        isValid = false;
     }
     if(isValid){
        //submit form
        //$("form", edit).submit();
     }
});

$(".btn-edit-save").click(function() {
     var title = $('#id').val();
     var packageName = $('#packageName').val();
     var isValid = true;
     if(title == ""){
        $('#id').after("<label class='error'>Title is required</label>");
        $('#id').css('border', '1px solid red');
        isValid = false;
     }
     if(packageName == ""){
        $('#packageName').after("<label class='error'>PackageName is required</label>");
        $('#packageName').css('border', '1px solid red');
        isValid = false;
     }
     if(isValid){
        //submit form
        //$("form", edit).submit();
     }
});
.error{
color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="addModalLabel">@Resource.Add_new_item </h5>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>@Resource.Application_category_tittle *</label>
                            <input name="id" id="id" class="form-control" style="direction: ltr;"/>
                            
                        </div>
                      
                        <div class="form-group">
                            <label>@Resource.Package_name *</label>
                            <input name="packageName" class="form-control" style="direction: ltr;"id="packageName" />
                            
                        </div>
                        <label id="validation">

                        </label>
                        <div class="form-group">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-success btn-edit-save">@Resource.Save</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">@Resource.Cancel</button>
                    </div>
                </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.