MAC Safari compatibility problem

S

Simon Wigzell

I use onkeypress() within text fields in my forms to chcek certain
characters e.g. allow numbers for a phone number or credit card field. The
code looks at the key pressed and if it is not numberic it posts an alert
and returns false. On everything I have looked at except safari on the mac,
returning false to onkeypress() is enough that the newly typed character
will not appear in the text field. On mac safari it does. Is there no way to
make this concept work on a safari mac? And yes I know there are ways around
it if someone is determined to enter other than numbers in a numeric field
such as copy/paste or just making a copy of the form and removing the
character checking but that isn't the point, its just a convenience in case
the user does type a bad character by mistake, its a nice feature to have.
Thanks.
 
R

RobG

Simon said:
I use onkeypress() within text fields in my forms to chcek certain
characters e.g. allow numbers for a phone number or credit card field. The
code looks at the key pressed and if it is not numberic it posts an alert
and returns false. On everything I have looked at except safari on the mac,
returning false to onkeypress() is enough that the newly typed character
will not appear in the text field. On mac safari it does. Is there no way to
make this concept work on a safari mac?

Without seeing your code, it's impossible to say how to fix what you have.
But looking at keycodes is flawed, and more work than looking at the
content of the input.

If you want to validate the text field, use onkeyup and look at the text
actually in the field. It is much simpler to validate whether it contains
all digits (or digits and spaces) than to detect keycodes using the two
popular models.

And yes I know there are ways around
it if someone is determined to enter other than numbers in a numeric field
such as copy/paste or just making a copy of the form and removing the
character checking but that isn't the point, its just a convenience in case
the user does type a bad character by mistake, its a nice feature to have.

I don't think it's convenience, I hate such things, especially when an
alert is used. You want to save the user work, yet you interrupt them,
stop further keypresses, required them to click the alert then delete what
they've typed before they even see what they did wrong.

Put a message in the page if you think the text is wrong and let the user
fix it, don't auto-delete what they type. The following example has been
tested in Safari 1.0.3 and Firefox 1.5.0.1 on Mac.


Enter numbers and spaces<input type="text"
onkeyup="checkDigits(this.value,'errMsg');">

<div id="errMsg"></div>

<script type="text/javascript">
function checkDigits(txt, id)
{
var errMsg = '';
if (/[^\d ]/.test(txt)){
errMsg = 'Only digits and spaces please';
}
document.getElementById(id).innerHTML = errMsg;
}

</script>
 
S

Simon Wigzell

RobG said:
Simon said:
I use onkeypress() within text fields in my forms to chcek certain
characters e.g. allow numbers for a phone number or credit card field.
The code looks at the key pressed and if it is not numberic it posts an
alert and returns false. On everything I have looked at except safari on
the mac, returning false to onkeypress() is enough that the newly typed
character will not appear in the text field. On mac safari it does. Is
there no way to make this concept work on a safari mac?

Without seeing your code, it's impossible to say how to fix what you have.
But looking at keycodes is flawed, and more work than looking at the
content of the input.

If you want to validate the text field, use onkeyup and look at the text
actually in the field. It is much simpler to validate whether it contains
all digits (or digits and spaces) than to detect keycodes using the two
popular models.

And yes I know there are ways around it if someone is
determined to enter other than numbers in a numeric field such as
copy/paste or just making a copy of the form and removing the character
checking but that isn't the point, its just a convenience in case the
user does type a bad character by mistake, its a nice feature to have.

I don't think it's convenience, I hate such things, especially when an
alert is used. You want to save the user work, yet you interrupt them,
stop further keypresses, required them to click the alert then delete what
they've typed before they even see what they did wrong.

Put a message in the page if you think the text is wrong and let the user
fix it, don't auto-delete what they type. The following example has been
tested in Safari 1.0.3 and Firefox 1.5.0.1 on Mac.


Enter numbers and spaces<input type="text"
onkeyup="checkDigits(this.value,'errMsg');">

<div id="errMsg"></div>

<script type="text/javascript">
function checkDigits(txt, id)
{
var errMsg = '';
if (/[^\d ]/.test(txt)){
errMsg = 'Only digits and spaces please';
}
document.getElementById(id).innerHTML = errMsg;
}

</script>
Thanks Rob, I will try your approach.
 

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,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top