Special Characters Processing

M

Michael Hill

How do I detect the special characters " ' or & when the user pushed
them.

I have this code:
<input type='text' onKeyUp='ck_char(this,event);'>

function ck_char(fld, e)
{
var key = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
key = String.fromCharCode(whichCode); // Get key value from key code
alert(key);
}

Since each one of these special keys also involve pressing the shift
key, the only value I get passed back is the last key pressed, which in
the case of & would always be 7.

What I'd like to do is detect these special characters when they are
entered and go ahead and replace them with an appropriate equivalent.

Mike
 
E

Evertjan.

Michael Hill wrote on 05 mrt 2004 in comp.lang.javascript:
How do I detect the special characters " ' or & when the user pushed
them.

I have this code:
<input type='text' onKeyUp='ck_char(this,event);'>

function ck_char(fld, e)
{
var key = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
key = String.fromCharCode(whichCode); // Get key value from key
alert(key);
}

where do you use the "fld" for ?

========================

alert(/'"/.test(key))

will give "true" if either ' or " is found.


Since each one of these special keys also involve pressing the shift
key, the only value I get passed back is the last key pressed, which
in the case of & would always be 7.

What I'd like to do is detect these special characters when they are
entered and go ahead and replace them with an appropriate equivalent.

wouldn't we all?
 
M

Michael Hill

where do you use the "fld" for ?

Another part of the script that I didn't show.
========================

alert(/'"/.test(key))

will give "true" if either ' or " is found.

This is giving me "false" for everything I push.

Mike
 
E

Evertjan.

Michael Hill wrote on 05 mrt 2004 in comp.lang.javascript:
This is giving me "false" for everything I push.

Ah yes, the key is a number,
so you will have to convert to a character
and I forgot the []

<script>
key=39 // '
key= String.fromCharCode(key)
alert(/['"]/.test(key))

key=34 // "
key= String.fromCharCode(key)
alert(/['"]/.test(key))
</script>

tested IE6
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top