How to extend with own rules

Home Forums Jquery Validation for Contact Form 7 How to extend with own rules

This topic contains 0 replies, has 1 voice, and was last updated by  Stanislaw Nalepa 2 years ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #3888

    Stanislaw Nalepa

    Hello Dinesh.

    I have tested an extension to jQuery Validation, in JSFiddler it works as expected. So now I wonder how I could integrate this function in your plugin “jquery validation for contact form 7”. the code below begins with two fields which were in the example already, it is the third field that runs the validation I need in my form.

    /*
    $(document).ready(function () {

    $(‘#myform’).validate({
    rules: {
    field1: {
    required: true,
    integer: true
    },
    field2: {
    required: true,
    integer: true
    },
    field3: {
    required: true,
    myPersonnummerkoll: true
    }
    },
    submitHandler: function (form) { // for demo
    alert(‘valid form’);
    return false;
    }
    });

    jQuery.validator.addMethod(‘integer’, function (value, element, param) {
    return (value != 0) && (value == parseInt(value, 10));
    }, ‘Please enter a non zero integer value!’);
    $.validator.addMethod(“myPersonnummerkoll”, function(value, element) {
    var pnr = value;
    // Do formatting and sanity control
    pnr = pnr.replace(/[^0-9]/g, ”); // only keep digits
    if (12 == pnr.length) // year format 1985 → 85
    pnr = pnr.substr(2);
    if (10 != pnr.length) // check length
    return false;
    if (pnr.substr(2,2) > 12) // check month
    return false;
    if (pnr.substr(4,2) > 31 || pnr.substr(4,2) == 0) // check date
    return false;
    var parts = pnr.split(”).map(function(i){
    return Number(i);
    });
    // Then run the mod 10 algorithm to produce check digit
    var control = parts.pop();
    var inc = 0, multiplicator = 2, product;
    for (var i in parts) {
    product = parts[i] * multiplicator;
    if (product > 9)
    inc += product – 9;
    else
    inc += product;
    multiplicator = multiplicator == 1 ? 2 : 1;
    }
    var control_ = 10 – (inc – Math.floor(inc/10)*10);
    if (10 == control_)
    control_ = 0;
    if (control == control_)
    return true;
    }, “Personnumret är ofullständigt eller i fel form!”);
    });*/

Viewing 1 post (of 1 total)

The topic ‘How to extend with own rules’ is closed to new replies.