Grabbing ASCII values in a text box

K

Kermit Piper

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();
}
 
Z

Zif

Kermit said:
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 = 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.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top