0

I have this in a separated js file

$(function () {

    var form = $("#formNovoArtigo").validate({
        rules: {
            '[id*=txtCadenciaMensal]': {
                required: true,
                digits: true
            }
        },
        messages: {
            "[id*=txtCadenciaMensal]": "Insira a Cadência Mensal"
        },
    });    
});

however i even tried to see what's going on with a try catch but it won't trigger so I'm not sure what am i doing wrong. I added rule to a textbox to accept only numbers but nothing happens. no message not nothing

Edit.

Here's scripts references

bundles.Add(new ScriptBundle("~/Bundles/jquery").Include(
            "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/Bundles/adminlte").Include(
            "~/AdminLTE/dist/js/adminlte.js",
            "~/AdminLTE/plugins/bootstrap/js/bootstrap.bundle.js"));

        bundles.Add(new ScriptBundle("~/Bundles/jqueryui").Include(
            "~/Scripts/jquery-ui-{version}.js"));

        bundles.Add(new ScriptBundle("~/Bundles/jqueryval").Include(
            "~/Scripts/jquery.unobtrusive*",
            "~/Scripts/jquery.validate*"));

        bundles.Add(new ScriptBundle("~/Bundles/js").Include(
        "~/Scripts/MasterPage/menu-selection.js",
        "~/Scripts/Pages/DataTables.js",
        "~/Scripts/Pages/Validation.js"));
5
  • 1
    what libraries are you using? Commented Dec 31, 2018 at 18:32
  • I'm using jquery validation that comes with jquery from nuget package on VS2013, there's no error on console about plugins. I edited my post shwing all scripts i'm using Commented Dec 31, 2018 at 18:35
  • 1
    u should use name of input fields instead of id. e.g: <input name="salary" /> then your rule look like this salary: { ... }. Commented Dec 31, 2018 at 19:14
  • 1
    for testing purpose use jQuery.validators.setDefaults as shown in this link. this will display your configuration errors. Commented Dec 31, 2018 at 19:17
  • Well now it worked... Seriously I don't know what was wrong. Because I'm using ASP net webforms I installed xVal.WebForms and just made a model and when I submitted the jquery error message on client side appeared... Commented Dec 31, 2018 at 20:43

1 Answer 1

1

Ah I found the error, i forgot to write input before the ID... Works 100% in ASP.NET 4.5

$(function () {

    $('form').validate({
        debug:true
    });
        $('input[id*=txtCadenciaMensal]').rules('add', {
            required: true,
            messages: {
                required: 'Some custom message for the username required field'
            }
        });
});
Sign up to request clarification or add additional context in comments.

1 Comment

(y), this would be helpful to those who want to add dynamic rules.

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.