0

I have a form I am trying to validate. This is the html

<h2>Personal Details</h2>
            <p>Please enter in some information about you.</p>
            <div id="form-row">
                <label>Name</label>
                <input type="text" name="name" value="" id="name" required/>
            </div>
            <div id="form-row">
                <label>Telephone</label>
                <input type="text" name="telephone" value="" id="telephone" required/>
            </div>
            <div id="form-row">
                <label>Email</label>
                <input type="email" name="email" value="" id="email" required/>
    </div>

I need to use some form of validation so I opted to use jQuery validation plugin called .validate. This is my script

$(document).ready(function($) {
    //When the form is submitted do this...
    $("#stripe-payment-form").submit(function(event) {

        //Validation
        // just for the demos, avoids form submit
        $.validator.setDefaults({
            debug: true,
            success: "valid"
        });

        $( "#stripe-payment-form" ).validate({
            rules: {
                telephone: {
                    required: true,
                    digits: true
                    }
            }
        });

When the page loads the first time I get no errors and validation is working. However If input 'aaa' into the telephone input I get this error and no validation occurs:

TypeError: $.validator is undefined

Does anyone know why this is happening? I was following along the documentation on

http://jqueryvalidation.org/digits-method

8
  • 1
    For starters, don't put your setDefaults code in the submit function, as it will get run every time the form is submitted. Leave it in the doc.ready Commented Dec 18, 2014 at 12:52
  • IDs must be unique on document context, IIRC, this is not the first time we have to say that to you... Commented Dec 18, 2014 at 12:57
  • My id's are unique...name, telephone, email? If your talking about the divs I can see that but I don't see how they would affect the issue im having at present Commented Dec 18, 2014 at 13:11
  • '<script src="jqueryvalidation.org/files/dist/…> <script src="jqueryvalidation.org/files/dist/…>' have you included these files Commented Dec 18, 2014 at 13:12
  • Maybe you have included validator codes before jquery core? Commented Dec 18, 2014 at 14:11

2 Answers 2

2

You need to include the jquery.validate.js file after jquery, like so:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.min.js"></script>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>

Also, your javascript code should look more like this:

$(document).ready(function() {
    $( "#stripe-payment-form" ).validate({
        rules: {
            telephone: {
                required: true,
                digits: true
                }
        }
    });
});

No need to wrap it in the form submit event. It will do that by itself. If there are other things you want to do on form submit, use the submitHandler option.

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

Comments

0

you can also do your code by developing a function and calling it according to your requirement.

var only_Number;
$(function(){
    $('.onlynumber').keypress(function(){
        return only_Number();
    });

    only_Number=function(evt){
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }
});

Also have a look on codepen: http://codepen.io/ahmadasjad/pen/bNMmya

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.