Created
March 14, 2014 15:33
-
-
Save BuddyLReno/9550118 to your computer and use it in GitHub Desktop.
Lock a Button with a Code Check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Requires jquery | |
function regCodeComponent() { | |
'use strict'; | |
var regController = {}; | |
var valid_codes = ['ramseynyc', 'sethsblog', 'garynyc']; | |
regController.init = function (button) { | |
regController.$button = $j(button); | |
regController.$regContainer = regController.$button.parents('.registration'); | |
regController.$promocode = regController.$regContainer.find('input.promocode'); | |
regController.$messagePanel = regController.$regContainer.find('.messagePanel'); | |
regController.$regContainer.on('click', regController.$button.selector, regController.click); | |
}; | |
regController.click = function(a) { | |
a.preventDefault(); | |
regController.show_error(false, ''); | |
var user_code = regController.$promocode.val().trim().toLowerCase(); | |
if (regController.code_exists(user_code)) { | |
if (regController.is_valid_code(user_code)) { | |
window.location.href = regController.$button.attr('href') + '/couponXXcode/' + user_code; | |
} else { | |
regController.show_error(true, 'Invalid code.'); | |
} | |
} else { | |
regController.show_error(true, 'Code is required.'); | |
} | |
}; | |
regController.show_error = function(status, error_msg) { | |
regController.$messagePanel.toggleClass('error', status).html(error_msg); | |
} | |
regController.code_exists = function(user_code) { | |
var result = false; | |
if (!!user_code) { | |
result = true; | |
} | |
return result; | |
}; | |
regController.is_valid_code = function(user_code) { | |
var result = false; | |
var i = 0; | |
for (i; i < valid_codes.length; i++) { | |
if (valid_codes[i].toLowerCase() === user_code) { | |
result = true; | |
} | |
} | |
return result; | |
}; | |
return { | |
init: regController.init | |
}; | |
} | |
(function (startRegController) { | |
if (window.jQuery) { /* jQuery already loaded. Proceed. Otherwise wait until window.load */ | |
startRegController(); | |
} else if (window.addEventListener) { /*DOM method for binding an event */ | |
window.addEventListener('DOMContentLoaded', startRegController, false); | |
} else if (window.attachEvent) { /* IE exclusive method for binding an event */ | |
window.attachEvent('onload', startRegController); | |
} else if (document.getElementById) { /* support for older modern browsers */ | |
window.onload = startRegController; | |
} | |
}(function () { | |
var headerRegCode = new regCodeComponent(); | |
var bodyRegCode = new regCodeComponent(); | |
headerRegCode.init('.sectionHeader .event_summary .registration a.button'); | |
bodyRegCode.init('.sectionPrimary .event_summary .registration a.button'); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment