Created
November 16, 2011 19:32
-
-
Save joshsmith/1371080 to your computer and use it in GitHub Desktop.
jQuery email validation and Kissmetrics
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
function isEmail(email) { | |
var regex = /^([a-zA-Z0-9_\.\-\+])+\@@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; | |
return regex.test(email); | |
} | |
$("#validate-form").submit(function (event, dontCheck) { | |
$email = $('#email'); | |
$form = $('#validate-form'); | |
var email = $email.val(); | |
if (!isEmail(email)) { | |
$email.addClass('has-error'); | |
return false; | |
} | |
if (dontCheck === true) return; | |
$.ajax({ | |
url: "/validate", | |
data: $(this).serialize(), | |
success: function (data) { | |
if (data.valid) { | |
console.log('data is valid...triggering submit'); | |
$("#validate-form").trigger("submit", true); | |
} else { | |
$("#info").text(data.message); | |
} | |
} | |
}); | |
event.preventDefault(); | |
}); | |
$.ajax = function (settings) { | |
if (!settings) return; | |
console.log('starting setTimeout'); | |
setTimeout(function () { | |
var resp = { | |
"valid": true, | |
"message": "" | |
}; | |
var email = $email.val(); | |
_kmq.push(['identify', email]); | |
_kmq.push(['record', 'Signed Up for Beta']); | |
if (settings.success) settings.success(resp); | |
}, 5000); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment