-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.html
83 lines (83 loc) · 2.7 KB
/
converter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!doctype html>
<html>
<head>
<style>
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 30pt;
margin: 2em;
}
input[type=text] {
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
font-size: 30pt;
padding: 5px 10px;
text-align: right;
}
.currency-container {
margin: 10px;
}
.currency-symbol {
display: inline-block;
text-align: center;
width: 30px;
}
#message {
color: #f88;
font-size: 20px;
}
</style>
</head>
<body>
<div class="currency-container">
<span class="currency-symbol">$</span>
<input type="text" size="6" placeholder="Dollars" id="dollars" />
</div>
<div class="currency-container">
<span class="currency-symbol">₸</span>
<input type="text" size="6" placeholder="Tokens" id="tokens" />
</div>
<div id="message"></div>
<script type="text/javascript">
var dollarsInput = document.getElementById('dollars');
var tokensInput = document.getElementById('tokens');
var message = document.getElementById('message');
function clear() {
dollarsInput.value = '';
tokensInput.value = '';
message.innerHTML = '';
}
// Converter dollars to tokens.
dollarsInput.addEventListener('keyup', function(ev) {
if (dollarsInput.value.match(/^\s*$/)) { clear(); return; }
var dollars = Number(dollarsInput.value.replace(/,/g,''));
if (isNaN(dollars)) {
tokensInput.value = '';
message.innerHTML = 'Invalid dollar amount "' + dollarsInput.value + '".';
return;
}
message.innerHTML = '';
tokensInput.value = commify(Math.floor(dollars * 20));
});
dollarsInput.addEventListener('focus', function() { this.select(); });
dollarsInput.focus();
// Converter tokens to dollars.
tokensInput.addEventListener('keyup', function(ev) {
if (tokensInput.value.match(/^\s*$/)) { clear(); return; }
var tokens = Number(tokensInput.value.replace(/,/g,''));
if (isNaN(tokens)) {
dollarsInput.value = '';
message.innerHTML = 'Invalid tokens amount "' + tokensInput.value + '".';
return;
}
message.innerHTML = '';
dollarsInput.value = (tokens / 20).toFixed(2);
});
tokensInput.addEventListener('focus', function() { this.select(); });
function commify(x) {
return (x).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').split('').reverse().join('').replace(/^,/,'');
}
</script>
</body>
</html>