0

Hopefully someone can help, I have been going crazy trying to figure this out. I am working on an MVC 5 application. I created a form and table in my viewmodel. Below are the 2 text box inputs I am working with. Jquery Validation is putting a validation rule on markup input box for step=.10 even though I don't specifically set the rule and this rule is not working. The text box gives an error for many inputs including (1.1, 1.2, 1.3, however input 1.6 passes.) The error is "Please specify a multiple of 0.1" The other wierd thing is, this works fine if a run from localhost debugging, however when I publish to hosted server, it does not work at all. The soldbox step of 10 works fine, there is no validation on it.

Any ideas??

Ian

@Html.TextBox("markup" + @item.ID, (item.markup == 0 ? new decimal(1.0) : item.markup), new { data_id = item.ID, style = "width:75px", @class = "markupbox form-control", @type = "number", @min = 0, @step = .10 })

@Html.TextBox("sold" + @item.ID, new decimal(0), new { data_id = item.ID, style = "width:75px", @class = "soldbox form-control", @type = "number", @min = 0, @max = 100, @step = 10 })

//JAVA SNIPPET
$('#livecartitems').validate({
        errorClass: "text-danger"
    });


        $('.soldbox').each(function () {
        $(this).rules('add', {
            min: 0,
            max: 100,
            messages: {
                min: "must be >= 0",
                max: "must be <= 100"
            }
        })
        $(this).rules('remove', 'step');
    });
});

//EDITTED JAVASCRIPT - Still does not work

 $('#livecartitems').validate({
        errorClass: "text-danger"
    });
    $('.markupbox').rules('remove', 'step');

I just tried a very simple example... Same behavior. 1.5 doesnt work, but 1.6 passes. Very strange. I am next going to try to reload the JS files or point them to google hosted JS scripts.

<form id="testform">
  @Html.TextBox("markup", new decimal(1.0), new { @style = "width:75px", @class = "markupbox form-control", @type = "number", @min = 0, @step = .10 })
</form>

<script src="@Url.Content("~/Scripts/jquery.validate.js")"    type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script type="text/javascript">
$('#testform').validate({
    errorClass: "text-danger"
});
</script>
ian486
  • 13
  • 7

1 Answers1

0

I believe the jQuery Validate plugin will automatically apply rules based on your markup and input type. You're trying to remove the rule with $(this).rules('remove', 'step');, however you're applying it to the wrong input, as it's wrapped in $('.soldbox').each(function () { ... }.

$('.markupbox').rules('remove', 'step'); will fix that (add it outside of the scope).

As for not working in production, there's not enough detail. Make sure jQuery and the Validate plugin are loading properly and check console for errors.

Edit: After a bit of testing in JSFiddle, I am able to replicate your step error (< 1.5 not valid). Take a look at this question as to why this is happening.

I was able to remove the step check, though, using the .rules('remove', 'step') method, though. You can also pass through the rule step: false when initialising the plugin.

Your best bet is to create a custom validator (for example, float-step and retrieve the values there if you do in fact need to validate the step size.

Here's a fiddle.

Community
  • 1
  • 1
scottgearyau
  • 162
  • 4
  • Hi Thanks for responding. I actually had that earlier as well and it didnt work. I just updated my javascript. Still does not work. I am going to test this on an individual page and also try to see if I can reload the JS scripts. – ian486 Jun 30 '16 at 02:29
  • Thank You. I am glad you were able to confirm what I was seeing. Was so frustrating! Hope they fix that issue. I was still not able to get the remove rule to work, but was able to add the step: false rule and that worked to remove the validation. I didnt need the validation as I had a regex validation already to prevent more then 2 decimal places. Thanks! – ian486 Jun 30 '16 at 04:32