jquery - Trigger a redirect to a page on form submission after three incorrect attempts in Javascript -
i have page coupon field, page should allow 3 incorrect attempts after has redirect page. tried setting counters couldn't make work. please on below code.
function validatecoupon() { var couponkey = ["abcdef1", "abcdef2", "abcdef3", "abcdef4", "abcdef5", "abcdef6", "abcdef7"]; var keyinput = $('#coupon').val().trim().touppercase(); if (couponkey.indexof(keyinput) > -1) { return true; } else { alert("invalid key"); return false; } }
after 3 failed attempts, want redirect page.
it's possible kept counter inside of function? might've reset each time called function. keep counter outside scope of function.
try out.
let timesattemped = 0; function validatecoupon() { var couponkey = ["abcdef1", "abcdef2", "abcdef3", "abcdef4", "abcdef5", "abcdef6", "abcdef7"]; var keyinput = $('#coupon').val().trim().touppercase(); if (couponkey.indexof(keyinput) > -1) { return true; } else if (timesattempted < 3) { alert("invalid key"); timesattempted++ return false; } else if (timesattempted === 3) { window.location.href = "http://www.lolrofl.com" // redirect logic here } }
Comments
Post a Comment