Grabbing single ASCII values pasted into a text area

K

Kermit Piper

Hello,

OK, almost there. Here's what I have so far, which handles characters
as they're typed in. Could someone please show me how I would loop
through all the values that are entered if a block of text was pasted
in, and then grab the ascii value(s) that are > 128?:

<script type="text/javascript">
function toASCII(s)
{
var r = [];
for (var i=0, j=s.length; j--; i++){
r = s.charCodeAt(i);
if (r > 128)
{
alert('"' + String.fromCharCode(r) + '"' + " Is not an acceptable
character.");
document.form1.textarea1.value="";
document.form1.textarea1.focus();
return false;
}
else
{
return true;
}
}
//return r.join(' ');
}
</script>

<BODY>
<form name="form1">
<textarea name="textarea1" onBlur="toASCII(this.value);"></textarea>

<!--<textarea name="textarea1"
onBlur="onBlur="document.getElementById('xx').innerHTML =
toASCII(this.value);"></textarea>-->
<br>
<div>ASCII character codes
<!--<div id="xx"></div>-->
</div>
<input type=reset value="clear" onClick="document.form1.reset()">
</form>
</BODY>

Thanks in advance,
KP
 
T

Thomas 'PointedEars' Lahn

Kermit said:
OK, almost there. Here's what I have so far, which handles characters
as they're typed in. Could someone please show me how I would loop
through all the values that are entered if a block of text was pasted
in, and then grab the ascii value(s) that are > 128?:

There are no "ASCII values" beyond code point 127, ASCII is a 7-bit code.
And most certainly you mean > 127.
<script type="text/javascript">
function toASCII(s)

See above.
{
var r = [];
for (var i=0, j=s.length; j--; i++){

The value of `i' will be always what s.length-j evaluates to, so one of both
variables is redundant.
r = s.charCodeAt(i);


OK, you are creating a new array element with the value of the code of the
character at position `i'.
if (r > 128)

But `r' refers to an Array object. It cannot work this way.
{
alert('"' + String.fromCharCode(r) + '"' + " Is not an acceptable

It would be more simple/easier/better if you just used s.charAt(i) here.
character.");

Use multiples of two spaces to indent your code, not the Horizontal Tab
character. Avoid code that exceeds the 80-columns margin, especially when
posting it; 72 or 76 are good numbers. IIRC there is a documentation
standard that even calls for word wrap after column #67.
document.form1.textarea1.value="";
document.form1.textarea1.focus();

See below.
return false;
}
else
{
return true;
}
}
//return r.join(' ');
}
</script>

<BODY>
<form name="form1">

The `action' attribute is missing.
<textarea name="textarea1" onBlur="toASCII(this.value);"></textarea>

Do not validate `onblur'. Use a button to validate instead. That can also
be a submit button in which case you would use the `onsubmit' handler of
the `form' element instead of the `onclick' handler of the button.

Pass `this' instead of `this.value', and you can use the `form' property
(or the passed reference itself, if called from an event handler for the
`form' element), coding standards compliant and avoiding any dependency
on the `form' element's name.

Search the archives for "form validation".
[...]
<input type=reset value="clear" onClick="document.form1.reset()">

The `onclick' code here is nonsense, since it is already a reset
button (input[type=reset]). Remove the entire attribute.


PointedEars
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top