Firefox: Filter Extended Ascii from Form

B

bgbauer70

My appologies if this ends up being a duplicate post. For some reason the
first post never showed up.



I've tried about 300 iterrations of this same ability, and none of them seem
to work in Firefox. Take the following code for example. It WILL stop me
from entering zero into the first text box, but it wont stop me from
entering extended ascii characters (which is the final goal).

Two items of note:
1) Typing ALT+0156 inputs "o". And strangely enough the statusbar text gets
set to "ALT0moz2 ALT0moz2 ALT0moz2". Only 3 ALT sequences show up when I
actually type 4 characters
2)Im working on a laptop that has no true numpad, so the '0' may be a result
of me having to hold down a special function key in order to enable a numpad
overlay.). So the check for altKey is correctly working, but attempting to
cancel the ALT event fails.

Does anyone know what is wrong with this, or have a working example that
stops ALT keypresses / Extended chars in FIREFOX ?


<script type="text/javascript">
if(document.addEventListener){
document.addEventListener("keypress", HandleEnterKey, true);
}
else{
document.attachEvent("onkeypress", HandleEnterKey);
}


// Handle the enter key for a section of a form, binding it to the provided
submit buton
function HandleEnterKey(event) {
var nav = window.Event ? true : false;
if (nav) {
return NetscapeEventHandler_KeyDown(event);
} else {
return MicrosoftEventHandler_KeyDown();
}
}

function NetscapeEventHandler_KeyDown(e) {
if (e.which == 48) {
window.status = window.status + e.which + "moz1 ";
e.returnValue = false;
e.cancel = true;
e.preventDefault();
return false;
} else if (e.altKey) {
window.status = window.status + "ALT" + e.which + "moz2 ";
e.returnValue = false;
e.cancel = true;
e.stopPropagation();
e.preventDefault();
return false;
}
return true;
}
</script>
</head>
<body>
<form action="" id="theForm">
<input type="text" id="i1" name="i1" />
</form>
 
R

RobG

bgbauer70 wrote:
[...]
// Handle the enter key for a section of a form, binding it to the provided
submit buton
function HandleEnterKey(event) {
var nav = window.Event ? true : false;
if (nav) {
return NetscapeEventHandler_KeyDown(event);
} else {
return MicrosoftEventHandler_KeyDown();
}
}

I don't have a solution, but the following is possibly a better way to
determine appropriate the event model:

function HandleEnterKey( e ) {
if ( e ) {
return NetscapeEventHandler_KeyDown( e );
} else if ( window.event ) {
return MicrosoftEventHandler_KeyDown();
}
}

If you are trying to restrict the characters that can be entered into a
text input, you are probably better off to use a regular expression to
test its value rather than trying to intercept keystrokes.

For example, I can enter a zero into the text input by copying and
pasting - ctrl+v or Edit->Paste - without using the zero key.

[...]
 
B

bgbauer70

Hi Rob,

Is there a way to regex test based on ascii #? The problem is I just want
to filter out high ascii ( >127 ).


RobG said:
bgbauer70 wrote:
[...]
// Handle the enter key for a section of a form, binding it to the
provided
submit buton
function HandleEnterKey(event) {
var nav = window.Event ? true : false;
if (nav) {
return NetscapeEventHandler_KeyDown(event);
} else {
return MicrosoftEventHandler_KeyDown();
}
}

I don't have a solution, but the following is possibly a better way to
determine appropriate the event model:

function HandleEnterKey( e ) {
if ( e ) {
return NetscapeEventHandler_KeyDown( e );
} else if ( window.event ) {
return MicrosoftEventHandler_KeyDown();
}
}

If you are trying to restrict the characters that can be entered into a
text input, you are probably better off to use a regular expression to
test its value rather than trying to intercept keystrokes.

For example, I can enter a zero into the text input by copying and
pasting - ctrl+v or Edit->Paste - without using the zero key.

[...]
 
M

Mick White

bgbauer70 said:
Is there a way to regex test based on ascii #? The problem is I just want
to filter out high ascii ( >127 ).

A string method:
String.charCodeAt(index)


Returns the decimal Unicode value, but for most characters it's the same
as AASCI.
Mick
 
R

RobG

bgbauer70 said:
Hi Rob,

Is there a way to regex test based on ascii #? The problem is I just want
to filter out high ascii ( >127 ).

There is no such thing as "high ascii" - it stops at 127, there are no
ASCII characters beyond it. As Mick suggests, you could use the Unicode
value to discriminate or you could make a regular expression that tests
for the 95 displayable ASCII characters. I think the former is simpler.

There is a relevant thread here in c.i.a.html:

<URL:http://groups.google.co.uk/group/co...ascii+character&rnum=1&hl=en#9d5f6ded0144e4c8>

An an excellent piece on HTML character encoding here:

<URL:http://ppewww.ph.gla.ac.uk/~flavell/charset/checklist>


[...]
 
B

bgbauer70

RobG said:
There is no such thing as "high ascii" - it stops at 127, there are no
ASCII characters beyond it.

This is just not true. The extended ASCII character set has 255.
 
B

Baconbutty

To filter out, you could perhaps filter out from >126 as 127 is unused.

The reg exp woud be:-

var r=/[\x7F-\xFF]*/g;
s=s.replace(r,"");
 
M

Mick White

Baconbutty said:
To filter out, you could perhaps filter out from >126 as 127 is unused.

The reg exp woud be:-

var r=/[\x7F-\xFF]*/g;
s=s.replace(r,"");
Good, but this replaces control characters too (\x0 - \x1f )
Mick
 
R

RobG

bgbauer70 said:
This is just not true. The extended ASCII character set has 255.

There are a vast array of extended ASCII sets, IBM alone developed over
three hundred of them[1], but none of them are standards. The following
reference seems a pretty good history:

<URL: http://en.wikipedia.org/wiki/ASCII >

ASCII consists of 128 characters mapped to a decimal range of 0 to 127
inclusive. Because it's been around for over 40 years, it's been
bastardised many times but:

"...most widely-used form uses the ANSI X3.4-1986 definition, also
standardized as ECMA-6, ISO/IEC 646:1991 International Reference
Version, ITU-T Recommendation T.50 (09/92), and Request for Comments
RFC 20."

In other words, the ASCII 7 bit character set has been standardised by a
number of official international standards-setting organisations[2].

I'm happy to be proven wrong, but you'll need to find a reference to an
official standard for 'the extended ASCII character set' to do it.


1.
<URL:
http://www-03.ibm.com/servers/eserver/iseries/software/globalization/codepages.html
2.
Standard ECMA-6:
<URL:
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-006.pdf >
 
B

bgbauer70

Thanks! Thats exactly what I was wondering if you could do.


Mick White said:
Baconbutty said:
To filter out, you could perhaps filter out from >126 as 127 is unused.

The reg exp woud be:-

var r=/[\x7F-\xFF]*/g;
s=s.replace(r,"");
Good, but this replaces control characters too (\x0 - \x1f )
Mick
 
M

Mick White

Mick said:
Baconbutty said:
To filter out, you could perhaps filter out from >126 as 127 is unused.

The reg exp woud be:-

var r=/[\x7F-\xFF]*/g;
s=s.replace(r,"");
Good, but this replaces control characters too (\x0 - \x1f )
Mick

Oops, ignore me.
Mick
 
B

bgbauer70

Strike that... I had to switch back to using the hex regex. The unicode
char set isnt layed out how I thought.


bgbauer70 said:
Just thought I'd post an update. I got keypress filtering working
finally.

For onblur handling (to cover paste ops) I ended up using the following.

if(/[\u0080-\uFFFF]+/i.test(targ.value)) {
window.status = "FAILED!"
} else {
window.status = "PASSED!"
}

This way higher unicode chars were included. I also opted against
replace, because if for example a person has 1 non-us english character in
their address, and I strip that character out, they may not visually see
that its missing, and it could lead to errors in the data. I am instead
going to alert() them to the error, and focus/select or highlight the
offending field.

Thanks for the help guys! I didnt realise you could regex based on char
code. Just what I needed in this situation. Very helpful!




Mick White said:
Baconbutty said:
To filter out, you could perhaps filter out from >126 as 127 is unused.

The reg exp woud be:-

var r=/[\x7F-\xFF]*/g;
s=s.replace(r,"");
Good, but this replaces control characters too (\x0 - \x1f )
Mick
 

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,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top