Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    67
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Adding custom fields to JQuery Form Validate

    Hello:

    I am having trouble getting adding custom rules to JQuery form validation to work properly. I want to use the default rules to make input required, make sure it is a number, and to make sure the number is 15 or greater. That was working fine.

    But, I am now trying to add a custom rule to it that will allow only numbers that are divisible by 5 into the field. And I am not having any luck getting it to work. I also would like to be able to have a custom message for each error.

    Code:
    <script scr="jquery.js" />
    <script scr="jquery.form.validate.js" />
    
    <script>
    
    $(document).ready(function(){
        $.validator.addMethod("add-copies-input",
                function(value, element) {
                    if ("add-copies-input" % 5 != 0);
                },
    
                "Must be multiples of 5"
        );
    
        $("#my-form").validate({
            rules: {
                "quantity": {
                    required: true,
                    number: true,
                    min: 15,
                    "add-copies-input": true
                }
            },
            messages: {
    
                "quantity": {
                    required: "Required Field"
                }
            }
    
        });
    
    })
    
    </script>
    
    
    <form id="my-form">
    ...
    
    <input type="text" name="quantity" class="add-copies-input" />
    
    ...
    </form>
    What am I missing?

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,683
    Thanks
    25
    Thanked 656 Times in 655 Posts
    Don't know if this will fix things but
    Code:
    <script scr="jquery.js" />
    <script scr="jquery.form.validate.js" />
    is wrong it's src

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    67
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi there:

    Good catch, but that is not the problem.

    It still functions as it did before.

    Maybe the JQuery Validate is not suppose to function like this .. ??

    I thought it was set-up to work this way but I'm missing something ...


  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,683
    Thanks
    25
    Thanked 656 Times in 655 Posts
    Have played with this and looked over the internet. The second parameter must be true or false and it must be returned. This looks like it works. Play with it and let me know:
    Code:
        $.validator.addMethod("add-copies-input", 
                function(value, element) {
                    return ((value % 5) == 0);
                },
                "Must be multiples of 5"
        );


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •