Skip to content

Instantly share code, notes, and snippets.

@davidvanvickle
Created August 8, 2012 18:48
Show Gist options
  • Save davidvanvickle/3297485 to your computer and use it in GitHub Desktop.
Save davidvanvickle/3297485 to your computer and use it in GitHub Desktop.
js - only numbers and commas
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>only numbers and commas</title>
</head>
<body>
<form name="Main" action="#" method="get" accept-charset="utf-8">
<label for="inputtext">inputtext</label><input type="text" name="inputtext" value="" id="inputtext">
<p><input type="button" value="Continue &rarr;" onClick="doit();"></p>
</form>
<script type="text/javascript" charset="utf-8">
function doit () {
var f_dval = {n:"inputtext",va:"",vb:"",c:"",cc:0,a:[],ab:[],bd:""};
if (document.Main && document.Main[f_dval.n] && document.Main[f_dval.n].value!='') {
f_dval.va = document.Main[f_dval.n].value;
for (var i=0; i<f_dval.va.length; i++) {
f_dval.c = f_dval.va.charAt(i);
f_dval.cc = f_dval.va.charCodeAt(i);
if (f_dval.cc >= 48 && f_dval.cc <= 57) { // numbers
f_dval.vb += ''+f_dval.c;
} else {
switch (f_dval.c) {
case '$': // dupe this line to add chars to include
case ',':
f_dval.vb += f_dval.c; break;
}
}
}
document.Main[f_dval.n].value = f_dval.vb;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment