470,647 Members | 1,184 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

Special Characters Processing

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
Jul 20 '05 #1
3 1530
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?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #2

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
Jul 20 '05 #3
Michael Hill wrote on 05 mrt 2004 in comp.lang.javascript:
alert(/'"/.test(key))

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


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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Roy W. Andersen | last post: by
3 posts views Thread by Barry Olly | last post: by
2 posts views Thread by withers | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.