-1

validation is not working here, please guide me. I tried to follow this but didn;t work How to validate input fields with a dot in name using the jquery validation plugin? thanks

JSFiddle link: https://jsfiddle.net/arshad7abdul/8mu8wL84/4/

 <form id="appForm" asp-controller="some" asp-action="AnonymousApp" method="post">

<div class="panel-body pt0 p10">
            <section class="grid grid--direction-row">
                <!-- First Name -->
                <div class="grid__col-4">
                    <label class="control-label">First Name <i class="fa fa-asterisk"></i></label>
                    <input asp-for="TaxPreparer.FirstName" class="form-control" maxlength="50"/>
                </div>

                <!-- Middle Name -->
                <div class="grid__col-4">
                    <label class="control-label">Middle Name</label>
                    <input asp-for="TaxPreparer.MiddleName" class="form-control charlimit" maxlength="50"/>
                </div>

                <!-- Last Name -->
                <div class="grid__col-4">
                    <label class="control-label">Last Name <i class="fa fa-asterisk"></i></label>
                    <input asp-for="TaxPreparer.LastName" class="form-control charlimit" maxlength="50"/>
                </div>
            </section>

 </div>
 <input class="btn btn-success btn-large" style="float: right; "id="submitApp" type="submit" value="Submit" />
    </form>

Here is the Javascript

$(document).ready(function() {  
var checkformValidation = function () {
            $("#appForm").validate({
            rules: {
                "TaxPreparer.FirstName": "required"
            }
            });
        }

   $('#submitApp').click(function(e) {
            e.preventDefault();
            checkformValidation();
            //checkValidation();
            //$("#appForm").validate();
            if ($("#appForm").valid()) {
                alert("success");
                return;
            } else {
                alert("wrong");
            }
            //$('#submitAppModal').modal('show');
        });
      });
2
  • Since you tagged this question JavaScript, which is client-side code, show us the actual rendered JavaScript & HTML markup instead the ASP. Commented Feb 15, 2018 at 22:29
  • Also FYI, putting .validate() inside of the form's submit click handler makes no sense. The .validate() method is only used for initializing the plugin on your form on page load. Capturing the click is then automatic. Commented Feb 15, 2018 at 22:47

1 Answer 1

0

instead of asp-for try name attribute on input

A 'name' attribute is required for all input elements needing validation, and the plugin will not work without this.

if you want asp-for for some other purpose you can keep it but you got to have a name on input for validation using the plugin mentioned

https://jsfiddle.net/206ku1yL/1/

  $(document).ready(function() {  
  var checkformValidation = function () {
                $("#appForm").validate({
                rules: {
                    "TaxPreparer.FirstName": "required"
                }
                });
            }

       $('#submitApp').click(function(e) {
                e.preventDefault();
                checkformValidation();
                //checkValidation();
                //$("#appForm").validate();
                if ($("#appForm").valid()) {
                    alert("success");
                    return;
                } else {
                    alert("wrong");
                }
                //$('#submitAppModal').modal('show');
            });
          });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>
<form id="appForm" asp-controller="some" asp-action="AnonymousApp" method="post">
   
    <div class="panel-body pt0 p10">
                <section class="grid grid--direction-row">
                    <!-- First Name -->
                    <div class="grid__col-4">
                        <label class="control-label">First Name <i class="fa fa-asterisk"></i></label>
                        <input name="TaxPreparer.FirstName" class="form-control" maxlength="50"/>
                    </div>

                    <!-- Middle Name -->
                    <div class="grid__col-4">
                        <label class="control-label">Middle Name</label>
                        <input name="TaxPreparer.MiddleName" class="form-control charlimit" maxlength="50"/>
                    </div>

                    <!-- Last Name -->
                    <div class="grid__col-4">
                        <label class="control-label">Last Name <i class="fa fa-asterisk"></i></label>
                        <input name="TaxPreparer.LastName" class="form-control charlimit" maxlength="50"/>
                    </div>
                </section>
   
   </div>
   <input class="btn btn-success btn-large" style="float: right; "id="submitApp" type="submit" value="Submit" />
        </form>

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Punith, Also, may I please know the reason for why it doesn't work with asp-for tag helper?
@KimboSlice in the documentation jqueryvalidation.org/reference it mention that you have to address to name of the input for validation
but isn't asp-for is the same like name and id? asp-for = "Taxpreparer.Name" gives html => name= "Taxpreparer.Name" id="Taxpreparer_Name"
@KimboSlice, then why did you put the ASP markup in your jsFiddle instead of the actual rendered HTML markup that a browser would use?
@Sparky there was something else preventing the validation, it does work with both asp-for and name, The asp-for tag is implemented by someone else working on it. thank you.

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.