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

Grabbing single ASCII values pasted into a text area

P: n/a
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[i] = 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').inne rHTML =
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

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


P: n/a
Kermit Piper wrote:
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[i] = 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
Mar 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.