PawelDecowski / jquery-creditcardvalidator

jQuery credit card validation and detection plugin
jquerycreditcardvalidator.com
MIT License
1.06k stars 275 forks source link

Code breaks in normalize method if number is <undefined> #102

Closed starlino closed 3 years ago

starlino commented 7 years ago

Temporary workaround:

    validate = (function(_this) {
      return function() {
        var number = 0; //_FIX_
        try{//_FIX_
            number = normalize($(_this).val());
        }catch(e){};//_FIX_
        return validate_number(number);
      };
PawelDecowski commented 7 years ago

Why would number be undefined? If you provide a valid input field it will always have a value.

starlino commented 7 years ago

This actually happens in CS-Cart that uses your library. I think during some animation/ switching the filed becomes briefly unavailable. It also might have to do with timeouts and de-synchronized calls. Anyways in real world this can happen unfortunately since DOM is not set in stone. I think it would be useful to do a sanity check for undefined since it will trigger an error and interrupt execution of the rest of the script. Just good coding practice , not a requirement. Thanks.

PawelDecowski commented 7 years ago

Ah, OK. That makes sense. What do you think the behaviour should be in that case?

Also, do you have an example of a page where it happens?

starlino commented 7 years ago

I think all you have to do is check that $(_this) still exists. You can use the code I provide above (since it is verified to work), or just check that $(_this).size() > 0 , or better yet 'undefined' != typeof $(_this).val()

PawelDecowski commented 7 years ago

Thanks. I’ll look into this.