0

I have a function that simply validates forms (for old browsers). The function works just fine except that I have to pass the parameters every time I call this function, where in fact I already specified the default parameters in 'config'.

So by logic, If I called the function as: validateMe(); it should run as validateMe({requiredClass: '.required', verifiedClass: 'invalid'});

but unfortunately calling the function without parameters doesn't work correctly ( in my case the form triggers the submission event) (it doesn't reach return false).

so what is missing in the code to run the function with the default settings??

function validateMe(vform, settings) {

    var vform,      //form name or id
        config = {
            'requiredClass': '.required', 
            'verifiedClass': 'invalid'
        };
    if (settings) {
        $.extend(config, settings);
    }

    $(vform).on('submit', function(){

        var inputs =  $(this).find(config.requiredClass), 
            required = [];
        for (i=0; i < inputs.length; i++) {

            if (inputs[i] != null) {
                if ($(inputs[i]).val().replace(/^\s+|\s+$/g, '') == '') {
                    required.push($(inputs[i]).index());
                }
            }
        }

        if (required.length > 0) {
            $(this).find('input').removeClass(config.verifiedClass);
            for(n=0;n<required.length;n++) {
                $(inputs[n]).addClass(config.verifiedClass);
            }
            return false;   
        }

    });
}

Any help?

Thanks.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
KQI
  • 322
  • 1
  • 16

1 Answers1

0
function validateMe(vform, settings) {
this.vform = vform || 'default',
this.setting = 'whatever',
this.private = ''
}
var newInstance = new validateMe();

now you have an instance of it, so you can define it as you go.

kangoroo
  • 359
  • 2
  • 10
  • no it doesn't, it's in the function definition? this is basic decorator pattern, maybe you are thinking of $(this) – kangoroo Sep 04 '13 at 20:44
  • So? What is next? The alert in jsfiddle returns back 'default'.. What does this mean? – KQI Sep 04 '13 at 21:31
  • well, i hate to tell you but, that's not how you use it. u say var newInstance = new validateMe(); then try it, and you will get undefined. – kangoroo Sep 05 '13 at 09:18
  • the real problem is this guy above want to use a function like an object without knowing it ;) – kangoroo Sep 05 '13 at 09:20