Skip to content

Instantly share code, notes, and snippets.

@jessehanley
Created September 29, 2016 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessehanley/b2db6636229fd52ee418543b9cdca707 to your computer and use it in GitHub Desktop.
Save jessehanley/b2db6636229fd52ee418543b9cdca707 to your computer and use it in GitHub Desktop.
YouTube Demo - Form Personalisation
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/1.0.1/js/md5.min.js"></script>
<script>
function validateEmail(email) {
var re = /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
return re.test(email);
}
$('#user_email').on('change keyup keydown', function() {
var email = $(this).val(),
valid = validateEmail(email);
$(this).toggleClass('error', !valid);
if (valid) {
var hash = md5($.trim(email.toLowerCase())),
url = 'http://www.gravatar.com/avatar/'+hash+'?s=100&d=mm&r=g';
localStorage.setItem('md5', hash);
$('#image').css('background-image', 'url('+url+')');
$.get('http://www.gravatar.com/'+hash+'.json',
function(s) {
console.log(s);
});
}
});
$('#user_username').on('change keyup keydown', function() {
var name = $(this).val();
localStorage.setItem('name', name);
$( ".card-title" ).text( "Nice to meet you, "+name+"!");
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment