I have custom jQuery validation function, but I'm not able to make it working as I want: If element is empty, I don't want to validate it, but if it's not empty, I want to check, if value is correct.
My custom function looks like this:
$.validator.addMethod("phoneCZ", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
if(!phone_number.match(/^((\+420)|(\+421))??[0-9]{3}?[0-9]{3}?[0-9]{3}$/)){
return (phone_number.length < 1);
}
else {
return (phone_number.length >= 9);
}
}, "Neplatné telefonní číslo");
Maybe just some describe: Allowed formats are: 123456789 +420123456789 +421123456789
If numbers is not in correct format, I return true, if it's length is 0, else return false. I format is matched, I check, if length is at least 9 characters.
if (phone_number.length === 0) return true;at the top