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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 <dashifen-NT0ononE2K1Wk0Htik3J/w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
...here''s a ''numbersonly'' js function that I like..... this would take care of the numbers only part... and you could easily adapt to meet your needs: cheers, Mark ...in the element you want to validate, simply add: onkeypress="return numbersonly(event)" put this JS at the top of the page: <script type="text/javascript"> // verifies only a number was typed into the form element function numbersonly(e){ var unicode=e.charCode? e.charCode : e.keyCode // if (unicode!=8||unicode!=9) if (unicode<8||unicode>9) { //if the key isn''t the backspace key or tab key (which we should allow) if (unicode<48||unicode>57) //if not a number return false //disable key press } } </script> On 3/8/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <dashifen-NT0ononE2K1Wk0Htik3J/w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <dashifen-NT0ononE2K1Wk0Htik3J/w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Found a few probs - home/end/arrow keys don''t work and you can hold shift and type symbols. I used it as a base though... I am just going to complete some testing on my handler and then i''ll put it up in case someone can see some obvious optimisations. Gareth On 3/9/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > ...here''s a ''numbersonly'' js function that I like..... this would take > care of the numbers only part... and you could easily adapt to meet your > needs: > cheers, > Mark > > ...in the element you want to validate, simply add: > onkeypress="return numbersonly(event)" > > put this JS at the top of the page: > <script type="text/javascript"> > // verifies only a number was typed into the form element > function numbersonly(e){ > var unicode=e.charCode? e.charCode : e.keyCode > // if (unicode!=8||unicode!=9) > if (unicode<8||unicode>9) > { > //if the key isn''t the backspace key or tab key (which we > should allow) > if (unicode<48||unicode>57) //if not a number > return false //disable key press > } > } > </script> > > > > On 3/8/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <dashifen-NT0ononE2K1Wk0Htik3J/w@public.gmane.org > 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
cool, that''s why I sent it... it''s a good base to expand it. looking forward to seeing the results On 3/8/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Found a few probs - home/end/arrow keys don''t work and you can hold shift > and type symbols. > I used it as a base though... > I am just going to complete some testing on my handler and then i''ll put > it up in case someone can see some obvious optimisations. > > Gareth > > > On 3/9/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > ...here''s a ''numbersonly'' js function that I like..... this would take > > care of the numbers only part... and you could easily adapt to meet your > > needs: > > cheers, > > Mark > > > > ...in the element you want to validate, simply add: > > onkeypress="return numbersonly(event)" > > > > put this JS at the top of the page: > > <script type="text/javascript"> > > // verifies only a number was typed into the form element > > function numbersonly(e){ > > var unicode= e.charCode? e.charCode : e.keyCode > > // if (unicode!=8||unicode!=9) > > if (unicode<8||unicode>9) > > { > > //if the key isn''t the backspace key or tab key (which we > > should allow) > > if (unicode<48||unicode>57) //if not a number > > return false //disable key press > > } > > } > > </script> > > > > > > > > On 3/8/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <dashifen-NT0ononE2K1Wk0Htik3J/w@public.gmane.org > 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Accidentally pasted it in a new thread entitled "final code" On 3/9/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > cool, that''s why I sent it... it''s a good base to expand it. > looking forward to seeing the results > > On 3/8/07, Gareth Evans < agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Found a few probs - home/end/arrow keys don''t work and you can hold > > shift and type symbols. > > I used it as a base though... > > I am just going to complete some testing on my handler and then i''ll put > > it up in case someone can see some obvious optimisations. > > > > Gareth > > > > > > On 3/9/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > ...here''s a ''numbersonly'' js function that I like..... this would take > > > care of the numbers only part... and you could easily adapt to meet your > > > needs: > > > cheers, > > > Mark > > > > > > ...in the element you want to validate, simply add: > > > onkeypress="return numbersonly(event)" > > > > > > put this JS at the top of the page: > > > <script type="text/javascript"> > > > // verifies only a number was typed into the form element > > > function numbersonly(e){ > > > var unicode= e.charCode? e.charCode : e.keyCode > > > // if (unicode!=8||unicode!=9) > > > if (unicode<8||unicode>9) > > > { > > > //if the key isn''t the backspace key or tab key (which we > > > should allow) > > > if (unicode<48||unicode>57) //if not a number > > > return false //disable key press > > > } > > > } > > > </script> > > > > > > > > > > > > On 3/8/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 <dashifen-NT0ononE2K1Wk0Htik3J/w@public.gmane.org > 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 9, 10:00 am, "agrath" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.Use keyup, not keydown. Use regular expressions, they are by far the simplest and most robust way to validate such input. There is everything you need to know here: <URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP > If you need help on the specifics, post to a group that specialises in javascript: news:comp.lang.javascript <URL: http://groups.google.com.au/group/comp.lang.javascript >> 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.Use a regular expression onkeyup and onsubmit.> 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 withThat is very user-unfriendly and doesn''t work anyway. There are more ways of entering input that you can prevent, so don''t try. Also, users may type an incorrect character, then press delete, then start typing again. If you deleted the character, they''ll delete another one... just warn users and get them to fix it themselves. You have to do the validation again at the server anyway.> 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.That is better, along with a message to say what is wrong (preferably in the page, not an alert).> Whilst I like the second option (validation icon) I think it will > prove to be more trouble than it''s worth.It doesn''t have to be. Remember that client-side validation is only a convenience, it isn''t at all reliable and all your validation has to occur again on the server anyway. You can organise a validation object, like: var isValid = (function(){ var integerTest = /^\d+$/; var decimalTest = /^\d+\.?\d*$/; var currencyTest = /^\d+\.\d\d$/; return { integer: function(n){ return integerTest.test(n); }, decimal: function(n){ return decimalTest.test(n); }, currency: function(n){ return currencyTest.test(n); } }; })(); alert( isValid.integer(''123'') // true +''\n''+ isValid.integer(''a123'') // false +''\n''+ isValid.decimal(''1212.2232'') // true +''\n''+ isValid.currency(''12.23'') // true +''\n''+ isValid.currency(''1212.23'') // true +''\n''+ isValid.currency(''1212.232'') // false ); Or if you feel brave, extend the String object''s prototype: String.prototype.isInteger = function(){ return /^\d+$/.test(this); } alert( ''123''.isInteger() ); -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 9, 10:53 am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a feeling instantiating a regex on every key press to ensure the key > pressed is numeric may be a tad overkill?You only have to instantiate it once, then use it as often as you like. -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 9, 11:12 am, "Mark Holton" <holto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ...here''s a ''numbersonly'' js function that I like..... this would take care > of the numbers only part... and you could easily adapt to meet your needs: > cheers, > Mark > > ...in the element you want to validate, simply add: > onkeypress="return numbersonly(event)" > > put this JS at the top of the page: > <script type="text/javascript"> > // verifies only a number was typed into the form element > function numbersonly(e){ > var unicode=e.charCode? e.charCode : e.keyCodeA strategy based on charCodes will fail at least some of the time and will be much longer, more verbose and less reliable than a regular expression. Consider how you are going to test that only one decimal point is entered, or only two decimal places for currency. <URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP > -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Your code was very concise and an appreciated analysis of the problem. I''ve coded a keyCode based keydown validator that handles the 3 cases (numeric, decimal, currency) which is what i''ll probably use- It''s definitely going to be server side validated, more of a helper in the UI. It''s in the final code thread, but I will take your comments onboard all the same. Thanks Gareth On 3/9/07, RobG <rgqld-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> > > > > On Mar 9, 11:12 am, "Mark Holton" <holto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ...here''s a ''numbersonly'' js function that I like..... this would take > care > > of the numbers only part... and you could easily adapt to meet your > needs: > > cheers, > > Mark > > > > ...in the element you want to validate, simply add: > > onkeypress="return numbersonly(event)" > > > > put this JS at the top of the page: > > <script type="text/javascript"> > > // verifies only a number was typed into the form element > > function numbersonly(e){ > > var unicode=e.charCode? e.charCode : e.keyCode > > A strategy based on charCodes will fail at least some of the time and > will be much longer, more verbose and less reliable than a regular > expression. Consider how you are going to test that only one decimal > point is entered, or only two decimal places for currency. > > <URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP > > > > -- > Rob > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
... and that is doubly true if you need to consider number format as it changes by browser language settings. If you plan on internationalizing, that''s a whole ''nother kettle of fish. TAG On Mar 8, 2007, at 10:57 PM, RobG wrote:> > Consider how you are going to test that only one decimal > point is entered, or only two decimal places for currency. > > <URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP > > > > -- > Rob--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---