SomethingOn
SomethingOn

Reputation: 10891

jQuery Validate property 'max' isn't working on my input field

I have an input that I want to set the max value to 255. I'm not submitting the form, but rather calling a function which submits the data via ajax.

Some of the other validate checks are working, like required and digits, but max isn't working. I'm using jQuery 1.4.4 and validate 1.7.

HTML

<input type="text" maxlength="3" style=" width: 190px" value="" name="Repeats" id="formfield_Repeats">

JavaScript

$(document).ready(function () {
   $("#my_form").validate({
      rules: {
         formfield_Repeats: {
            digits: true,
            required: true,
            max: 255
         }
      }
   });
});

When I go to save the app I call the valid() method on the form.

Is it a version mismatch with jQuery and the validate plugin? I could upgrade both, but the project is HUGE and I don't necessarily want to have to retest the entire app.

Upvotes: 0

Views: 2849

Answers (2)

Patricia
Patricia

Reputation: 7802

jquery validate uses the NAMES of inputs, not the id's so you should have:

$(document).ready(function () {
   $("#my_form").validate({
      rules: {
         Repeats: {
            digits: true,
            required: true,
            max: 255
         }
      }
   });
});

EDIT: hmm, ok, let's try adding the rule directly to the input field:

$("#my_form").validate();

$('#formfield_Repeats').rules('add', {
    required: true,
    digits: true,
    max: 255
});

EDIT: have a look at this js fiddle: it shows this working exactly as expected with validate 1.7 and jquery 1.4.4 http://jsfiddle.net/Et9vM/

Upvotes: 5

Dinash
Dinash

Reputation: 3047

hi it should maxlength instead of max... Just go through this Link for maxlength validation jquery

Hop this helps

Upvotes: 0

Related Questions