Skip to content

Instantly share code, notes, and snippets.

@sgraessle
Last active December 31, 2015 18:49
Show Gist options
  • Save sgraessle/8029700 to your computer and use it in GitHub Desktop.
Save sgraessle/8029700 to your computer and use it in GitHub Desktop.
Here's the relevant code for our text-to-mobile form. We're using a jQuery plugin for the country-code drop down: http://jackocnr.com/intl-tel-input.html, then stripping extraneous punctuation and leading zeroes from the result.
<html>
<head>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="../js/intlTelInput.min.js"></script>
<link rel="stylesheet" href="../css/intlTelInput.css">
</head>
<style>
.f16 .flag {background-image: url('../images/canvas/flags16.png');}
</style>
<body>
<div id="text-to-mobile">
<div id="t2m-form-div" class="t2m" style="display: none;">
<span class="t2m-prompt">Enter your phone number</span>
<form id="t2m-form" method="post">
<input type="tel" id="mobile-number" name="mobile_number" placeholder="e.g. +1 702 123 4567">
<input type="submit" class="button">
</form>
</div>
<div id="text-prompt" class="t2m t2m-prompt button">
<p>Send the game to your phone by text message!</p>
</div>
</div>
</body>
<script>
(function($) {
var t2m_url = 'http://appcc.gamepass.com/apps/dragons/';
$('#mobile-number').intlTelInput();
function HideTextToMobileForm() {
$('#t2m-form-div').hide();
$('#text-to-mobile').on('click', OnTextToMobileView);
$('#text-prompt').show();
}
function ShowTextToMobileForm() {
$('#t2m-form-div').show();
$('#text-to-mobile').off('click', OnTextToMobileView);
$('#text-prompt').hide();
}
function OnTextToMobileView(e) {
ShowTextToMobileForm();
$.post(t2m_url + 'view');
}
function OnTextToMobileSubmit(e) {
e.preventDefault();
var data = {};
for (var i = 0, ii = this.length; i < ii; ++i) {
var input = this[i];
if (input.name) {
n = input.value;
a = n.split(' ');
if (a.length > 1) {
// strip away leading zeroes in body
a[1] = a[1].replace(/^0/, '');
n = a.join('');
}
data[input.name] = '+' + n.replace(/\D/g, '');
}
}
if (data['mobile_number'].length > 6) {
$.ajax({
type: 'post',
url: t2m_url + 'send',
contentType: 'application/json; charset=UTF-8',
data: JSON.stringify(data),
success: function(result) {
console.log("send result: " + JSON.stringify(result));
HideTextToMobileForm();
},
error: function(result) {
console.log("send error: " + JSON.stringify(result));
HideTextToMobileForm();
}
});
} else {
HideTextToMobileForm();
}
}
$('#text-to-mobile').on('click', OnTextToMobileView);
$('#t2m-form').on('submit', OnTextToMobileSubmit);
})(jQuery);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment