0

I am trying to use client side validation in my application but its not working. Only server side validation is working. The validation works after the form is posted to the server. I have the following scripts in my head section of the layout

<script src="~/js/jquery-3.6.0.min" type="text/javascript"></script>
<script src="~/js/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="~/js/jquery.validate.js" type="text/javascript"></script>

I have used browser developer tools and the scripts are shown in the head section of the page. when I click the submit button in the form the form is being submitted and only afterwards the errors show.

The form is as follows:

<form asp-action="Create">  

    <div class="form-group">
        <label asp-for="@Model.Title"></label>
        <textarea asp-for="@Model.Title" rows="3" class="form-control"></textarea>
        <span asp-validation-for="@Model.Title" class="text-danger"></span>
    </div>

    <div class="text-center">
        <button class="btn btn-primary form-btn-margin-top" type="submit">Create</button>
    </div>
           
</form>

The model class is

public class Material
{
    public int Id { get; set; }

    [Required]
    public string Title { get; set; }
}
8
  • you're loading the scripts without an issue, but not sure if you are invoking it(Hard to tell from your code). This link has all the documentation and shows you how to invoke methods and use it jqueryvalidation.org Commented Jul 15, 2021 at 15:45
  • But in asp.net core you don't need to write any additional code for client side validation to work. just the data annotations in the model class which are the same for server side validation Commented Jul 15, 2021 at 15:49
  • Could you add the form and the model that you are validating? Commented Jul 15, 2021 at 15:53
  • I have edited the question and added the model and form. also please note that only server side validation is already working Commented Jul 15, 2021 at 16:06
  • 1
    This really looks wrong, perhaps <script src="~/js/jquery-3.6.0.min" type="text/javascript"></script> did you mean <script src="~/js/jquery-3.6.0.min.js" type="text/javascript"></script> Commented Jul 15, 2021 at 16:10

1 Answer 1

1

Update to place the .js on the end of the jQuery path. Also remove the attribute for type="text/javascript" all modern browsers have that as a default and it will generate a warning on validation for example here: https://validator.w3.org/

<script src="~/js/jquery-3.6.0.min.js"></script>
<script src="~/js/jquery.validate.unobtrusive.min.js"></script>
<script src="~/js/jquery.validate.js"></script>

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

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.