By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

Grabbing ASCII values in a text box

P: n/a
Hello,

I have a function that lets me convert one character and throw an alert
with the corresponding ASCII value, but what I am having trouble with
is applying it to a text box. What I'm trying to do is when characters
are entered the function will loop through the entered values and throw
an alert for each of the corresponding ASCII values for each of the
characters entered. I am trying to alert the user for every character
that is out of the range of ASCII equivalent 1 - 128. Test code below.

Thanks in advance,
KP

function asciiValue(c)
{
// restrict input to a single character
c = c.charAt(0);

// loop through all possible ASCII values
var i;
for (i = 0; i < 256; ++ i)
{
// convert i into a 2-digit hex string
var h = i.toString (16);
if (h.length == 1)
h = "0" + h;

// insert a % character into the string
h = "%" + h;

// determine the character represented by the escape code
h = unescape(h);

// if the characters match, we've found the ASCII value
if (h == c)
break;
}
return i;
}

function update()
{
var inp = document.f.inp.value;
if (inp != "")
{
var result = asciiValue(inp);
document.f.outp.value = result != null ? result : "???";
document.f.inp.value = "";
}
else
alert ("You must enter a character first.");
document.f.inp.focus();
}

Mar 10 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Zif
Kermit Piper wrote:
Hello,

I have a function that lets me convert one character and throw an alert
with the corresponding ASCII value, but what I am having trouble with
is applying it to a text box. What I'm trying to do is when characters
'text box'? Do you mean a textarea or text input? Not that it really
matters I guess.

are entered the function will loop through the entered values and throw
an alert for each of the corresponding ASCII values for each of the
characters entered. I am trying to alert the user for every character
that is out of the range of ASCII equivalent 1 - 128. Test code below.


If your range is 128 (which is the valid range for ASCII) why do you
loop to 256? Anyway, see if you can adapt this to suit (wrapped for
posting):

<script type="text/javascript">

function toASCII(s)
{
var r = [];
for (var i=0, j=s.length; j--; i++){
r[i] = s.charCodeAt(i);
}
return r.join(' ');
}

</script>

<textarea
onkeyup="document.getElementById('xx').innerHTML =
toASCII(this.value);"></textarea>
<div>ASCII character codes
<div id="xx"></div>
</div>
It only considers the actual characters entered into the text area.

[...]


--
Zif
Mar 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.