Alister West

home is where your code is ...

jQuery form validation plugin usage.

http://bassistance.de/jquery-plugins/jquery-plugin-validation/ Doesn't play nicely with "Sexy-Combo":http://code.google.com/p/sexy-combo/ (select dropdown)

// extend jquery-validation to make sure Date is a future date.
// ie<9 doesn't parse hyphens                                                  
$.validator.addMethod("FutureDate",
    function (value, element) { return Date.parse(value.replace("-","/")) > new Date().getTime(); },
    "Date must be in the future."
);
$.extend(jQuery.validator.messages, { required: "Required." }); */
$.validator.addMethod( "passwordCheck",
    function( value, element ) { return this.optional(element) || value.length >= 8 && /\d/.test(value) && /[a-z]/i.test(value); }, 
    "Password should be at least 8 characters and contain letters and numbers"
);

jQuery.validator.addMethod( "dbusername",
    function(value, element) { return this.optional(element) || (value.length <= 16 && !/\s/.test(value) ); },
    "Username must be less than 17 chars and contain no spaces."
);


$('#myForm').validate({
    // prevent server submit
    debug: true,

    rules: {
        subject: "required",
        category:  { required: true, minlength: 1 },
        requestor: { required: true, email: true },
        cc:        { required: false, email: true },
        department: "required",
        date_expected: { required: true, dateISO: true, FutureDate: true },
        content: "required",
        year: { digits: true, min: 1900, max: new Date().getFullYear() }
    },

    messages: { 
        date_expected: { required: "You must enter a valid date", dateISO: "You must enter a valid date" },
        requestor: { required : "An Email address is required", email: "You must enter a valid email address" },
        cc:        { required : "An Email address is required", email: "You must enter a valid email address" }
    },

    errorPlacement: function(error, element) {
        if (element.attr("name") == "date_expected") {
            error.insertAfter( $('#date-error') );
        }
        else if (element.attr("name") == "content") {
            error.insertBefore( element );
        }
        else { error.insertAfter(element); }
    },

    invalidHandler: function(form, validator) {
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1
                ? 'You have an error on the page. Please review and make the appropriate changes'
                : 'You have ' + errors + ' errors on the page. Please review and make the appropriate changes';
            var $errorsbox = $('#myForm > div.errors');
            $errorsbox.html(message);
            $errorsbox.show();
        } else {
            $errorsbox.hide();
        }
    },

    submitHandler: function(form) {
        // do something here pre-submit
        // using a selector like $('#..') causes issues in an iframe.
        // (!form.username.value) { form.username.value = form.email.value; }
        form.submit();
    }
});
By Alister West