Skip to content

Instantly share code, notes, and snippets.

@davidvanvickle
Created June 19, 2012 18:42
Show Gist options
  • Save davidvanvickle/2955818 to your computer and use it in GitHub Desktop.
Save davidvanvickle/2955818 to your computer and use it in GitHub Desktop.
JS - only letters and numbers
<!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 letters and numbers</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_fval = {n:"inputtext",va:"",vb:"",c:"",cc:0,a:[],ab:[],bd:""};
if (document.Main && document.Main[f_fval.n] && document.Main[f_fval.n].value!='') {
f_fval.va = document.Main[f_fval.n].value;
for (var i=0; i<f_fval.va.length; i++) {
f_fval.c = f_fval.va.charAt(i);
f_fval.cc = f_fval.va.charCodeAt(i);
if (f_fval.cc < 10 || (f_fval.cc >= 48 && f_fval.cc <= 57) ||
(f_fval.cc >= 65 && f_fval.cc <= 90) ||
(f_fval.cc >= 97 && f_fval.cc <= 122)) {
f_fval.vb += ''+f_fval.c;
} else {
switch (f_fval.c) { // "No commas, periods, quotes, asterisk, parenthesis"
case ' ': // dupe this line to add chars to include
f_fval.vb += f_fval.c; break;
case "&": f_fval.vb += 'and'; break;
default: f_fval.vb += ' '; f_fval.bd += f_fval.c;
}
}
}
f_fval.a = f_fval.vb.split(' ');
for (var i=0; i<f_fval.a.length; i++) {
if (f_fval.a[i]!='') f_fval.ab.push(f_fval.a[i]);
}
f_fval.vb = f_fval.ab.join(' ');
document.Main[f_fval.n].value = f_fval.vb;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment