Okay, here's a start...
You'll see that i'm using a div as a console since i'm trying to debug in IE
as well...
The keycode seems to be the correct ascii code for the key pressed, at least
in FF2 and IE7, I haven't tried Mac/Opera yet.
How do we actually check it is a correct numeric key?
What I mean by this is try pressing a few keys such as Shift-1 , which
string.fromCharCode() returns "1" for.
You can also paste text data into the field - since the paste never fires
the onkeypress it's perfectly valid.
> <html>
> <head>
> <script src="prototype.js" language="javascript"></script>
> <script>
> function test_keydown(e)
> {
> if (document.all) { e = window.event; }
> var key;
> if (e.keyCode) key = e.keyCode;
> if (e.which) key = e.which;
> if (key != undefined) {
> debug(Event.element(e).id + ' -> ' + key + ' -> ' + String.fromCharCode
> (key));
> }
> return true;
> }
> function debug(text)
> {
> new Insertion.Top('debuggingoutput',text + '<br/>');
> }
> function Begin()
> {
> debug("onload called");
> $A(document.getElementsByTagName("INPUT")).each(function (input) { if (
> input.type == 'text' && input.getAttribute("validationtype") != null) {
> debug('observer added to ' + input.id); Event.observe(input, 'keydown',
> test_keydown, true); } });
> }
> </script>
> </head>
> <body onload='Begin();'>
> <strong>Numeric Only:</strong> <input type='text'
> id='NumericOnlyInputControl_1' size='40'
> validationtype='numericonly'></input>
> <strong>Anything:</strong> <input type='text'
> id='AnyTextInputControl_1' size='40'></input>
> <br/><br/>
> <strong>Debugging</strong>
> <div id='debuggingoutput' style='width:600px; height:400px; border:solid
> 1px #000000; padding-right:25px; overflow: auto;'>
> </div>
> </body>
> </html>
On 3/9/07, Gareth Evans <[EMAIL PROTECTED]> wrote:
>
> I have a feeling instantiating a regex on every key press to ensure the
> key pressed is numeric may be a tad overkill?
>
>
> On 3/9/07, David Dashifen Kees <[EMAIL PROTECTED]> wrote:
> >
> >
> > Sounds to me like you need a regular expression that can accurately
> > describe the data that's valid and then check that the value of the
> > field matches the regular expression on a key press.
> >
> > -- Dash --
> >
> > agrath wrote:
> > > I have a requirement to validate input to a field as numeric only-
> > >
> > > A number of approaches could be used here, but I need to create 3
> > > different validators:
> > >
> > > 1) Currency
> > > 2) Numbers, including decimals and , i guess
> > > 3) Integers only
> > >
> > > I'm thinking an observer on the keydown event of the field I want to
> > > validate, the question is how do we validate the data being entered is
> > > correct and close all the loopholes.
> > >
> > > In a previous scenario such as this, I experienced major issues with
> > > users holding the shift key and typing number keys (to get symbols
> > > entered) as well as copy-and-paste being able to paste text data, so
> > > ended up needing to recheck and clear on-blur as well...
> > > The result was a massive script that was total bloatware for the
> > > operation.
> > >
> > > I was wondering how some of you might approach the problem.
> > >
> > > Prototype 1.5.0 is being used in the project.
> > >
> > > The way I see it, I can either force validation by only allowing
> > > numeric digits to be entered, and deny non numeric digits, or go with
> > > a remember-the-milk type input validation, with an icon on the end of
> > > the field that indicates if the data is parsed correctly and prevent
> > > the form from being submitted unless all validation rules are met.
> > > Whilst I like the second option (validation icon) I think it will
> > > prove to be more trouble than it's worth.
> > >
> > > What do you guys think?
> > >
> > >
> > > >
> > >
> > >
> >
> > > >
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---