Sorry to be putting such a Ruby Newby question to the forum, but I have
done extensive searching on the web and this seems to be a topic not
handled very well by Ruby! I am entering a dollar amount in a Shoes edit
line. I just want to test it and if it is not numeric to give an alert etc
etc. I can't find a simple test that will work - I just want to test for 0
to 9 and the decimal point - I have tried regex and trying to convert to
float etc etc, but none seem to work for me - what would be ideal is a
simple routine such as :-
if not_numeric(@amount.text) then
alert "Not numeric!"
etc etc
It's the not_numeric test that I am having trouble with - I
have copied samples from the web without success (had to try to convert
many from is_numeric to not_numeric). This should be easy, and I'm sure it
is - once you know how!
Any help much appreciated - Thanks
Roger
On Tue, Aug 17, 2010 at 7:35 AM, Roger Lovelock <rogerlovelock@hotmail.com> wrote: > if not_numeric(@amount.text) then > alert "Not numeric!" > etc etc > It's the not_numeric test that I am having trouble with - I Another way (though it's too liberal for your current use case) is to construct a Float from it - that's guaranteed to match Ruby's definition of a numeric. def numeric?(s) begin Float(s) return true rescue return false end end (or, if you prefer the one-line version, def numeric?(s) Float(s) rescue false end ) However, this will allow stuff like "-1.23e10", which is not what you want for currency, so a simple regex works better here. def numeric?(x) x =~ /^\d+(\.\d{1,2})?$/ ? true : false end (i.e. any number of digits, optionally followed by a decimal point and either one or two digits) Testing in irb irb(main):004:0> def numeric?(x); x =~ /^\d+(\.\d{1,2})?$/ ? true : false; end => nil irb(main):005:0> numeric? "1.23x" => false irb(main):006:0> numeric? "1.23" => true irb(main):007:0> numeric? "1.2" => true irb(main):008:0> numeric? "1.234" => false irb(main):009:0> numeric? "1." => false irb(main):010:0> numeric? "1" => true irb(main):011:0> numeric? ".23" # <----- note that this fails!! => false irb(main):012:0> numeric? "0.23" => true If you want to allow fractions like ".23" without the leading 0, use this regex instead x =~ /^(\d+(\.\d{1,2})?|\.\d{1,2})$/ which means "either the previous regex, or a compulsory decimal point followed by one or two digits" martin
OK - this is the one I have finally settled on with one variation - I
reversed the true and false - and called the function 'notnumeric?' - all
tested and working great!
if notnumeric?(@dues.text) then
alert "Error - Dues not numeric!"
@dues.focus
return
end
def notnumeric?(x)
x =~ /^\d+(\.\d{1,2})?$/ ? false : true
end
Thanks everyone for the great support while I worked through what was
probably a trivial matter for a 'rubyist', but was causing me some problems,
especially as I am no expert with RegEx!
Roger
--------------------------------------------------
From: "Martin DeMello" <martindemello@gmail.com>
Sent: Tuesday, August 17, 2010 3:57 PM
To: <shoes@librelist.com>
Subject: Re: [shoes] Validation
> On Tue, Aug 17, 2010 at 7:35 AM, Roger Lovelock
> <rogerlovelock@hotmail.com> wrote:
>> if not_numeric(@amount.text) then
>> alert "Not numeric!"
>> etc etc
>> It's the not_numeric test that I am having trouble with - I
>
> Another way (though it's too liberal for your current use case) is to
> construct a Float from it - that's guaranteed to match Ruby's
> definition of a numeric.
>
> def numeric?(s)
> begin
> Float(s)
> return true
> rescue
> return false
> end
> end
>
> (or, if you prefer the one-line version,
> def numeric?(s)
> Float(s) rescue false
> end
> )
>
> However, this will allow stuff like "-1.23e10", which is not what you
> want for currency, so a simple regex works better here.
>
> def numeric?(x)
> x =~ /^\d+(\.\d{1,2})?$/ ? true : false
> end
>
> (i.e. any number of digits, optionally followed by a decimal point and
> either one or two digits)
>
> Testing in irb
>
> irb(main):004:0> def numeric?(x); x =~ /^\d+(\.\d{1,2})?$/ ? true : false;
> end
> => nil
> irb(main):005:0> numeric? "1.23x"
> => false
> irb(main):006:0> numeric? "1.23"
> => true
> irb(main):007:0> numeric? "1.2"
> => true
> irb(main):008:0> numeric? "1.234"
> => false
> irb(main):009:0> numeric? "1."
> => false
> irb(main):010:0> numeric? "1"
> => true
> irb(main):011:0> numeric? ".23" # <----- note that this fails!!
> => false
> irb(main):012:0> numeric? "0.23"
> => true
>
> If you want to allow fractions like ".23" without the leading 0, use
> this regex instead
>
> x =~ /^(\d+(\.\d{1,2})?|\.\d{1,2})$/
>
> which means "either the previous regex, or a compulsory decimal point
> followed by one or two digits"
>
> martin
>
On Tue, Aug 17, 2010 at 8:36 AM, Roger Lovelock <rogerlovelock@hotmail.com> wrote: > OK - this is the one I have finally settled on with one variation - I > reversed the true and false - and called the function 'notnumeric?' - all > tested and working great! > if notnumeric?(@dues.text) then > alert "Error - Dues not numeric!" > @dues.focus > return > end > > > def notnumeric?(x) > x =~ /^\d+(\.\d{1,2})?$/ ? false : true > end This is mostly taste but I find the inversed logic a bit strange. def numeric?(x) x =~ /^\d+(\.\d{1,2})?$/ end ... unless numeric?(@dues.text) alert "Error - Dues not numeric!" @dues.focus return end or you could go crazy and monkey patch String class something like class String def dollar_amount? self =~ /^\d+(\.\d{1,2})?$/ end end ... unless @dues.text.dollar_amount? alert "Error - Dues not in dollars!" @dues.focus return end -- Jonas Elfström
The reason for the reverse logic is that the validation has multiple steps -
I'm a Ruby newby and I find it difficult to see how to mix if and unless in
the same structure. So I think my logic is the right way around - In pseudo
code ...
if description contains invalid chars then
alert
elsif description is > 30 chars then
alert
elsif dues are not numeric then
alert
else
save my update
end
but as you say a matter of taste.
Roger
--------------------------------------------------
From: "Jonas Elfström" <jonelf@gmail.com>
Sent: Tuesday, August 17, 2010 7:03 PM
To: <shoes@librelist.com>
Subject: Re: [shoes] Validation
> On Tue, Aug 17, 2010 at 8:36 AM, Roger Lovelock
> <rogerlovelock@hotmail.com> wrote:
>> OK - this is the one I have finally settled on with one variation - I
>> reversed the true and false - and called the function 'notnumeric?' - all
>> tested and working great!
>> if notnumeric?(@dues.text) then
>> alert "Error - Dues not numeric!"
>> @dues.focus
>> return
>> end
>>
>>
>> def notnumeric?(x)
>> x =~ /^\d+(\.\d{1,2})?$/ ? false : true
>> end
>
> This is mostly taste but I find the inversed logic a bit strange.
>
> def numeric?(x)
> x =~ /^\d+(\.\d{1,2})?$/
> end
>
> ...
>
> unless numeric?(@dues.text)
> alert "Error - Dues not numeric!"
> @dues.focus
> return
> end
>
>
> or you could go crazy and monkey patch String class something like
>
> class String
> def dollar_amount?
> self =~ /^\d+(\.\d{1,2})?$/
> end
> end
>
> ...
>
> unless @dues.text.dollar_amount?
> alert "Error - Dues not in dollars!"
> @dues.focus
> return
> end
>
> --
> Jonas Elfström
>
On Tue, Aug 17, 2010 at 1:07 PM, Roger Lovelock <rogerlovelock@hotmail.com> wrote: > The reason for the reverse logic is that the validation has multiple steps - > I'm a Ruby newby and I find it difficult to see how to mix if and unless in if not numeric?(@dues.text) alert elsif ... But now you certainly think that I'm way too stubborn. > the same structure. So I think my logic is the right way around - In pseudo > code ... > > if description contains invalid chars then > alert > elsif description is > 30 chars then > alert > elsif dues are not numeric then > alert > else > save my update > end > > but as you say a matter of taste. As a user I like when all my failed validations in a form is presented at once. alerts=[] alerts<<"Description has invalid characters." if not valid_chars?(@desc.text) alerts<<"Description should have a max length of 30." if @desc.text.length>30 alerts<<"Dues not numeric." if not numeric?(@dues.text) if alerts.empty? save else alert(alerts.join("\n")) end -- Jonas Elfström
OK - I accept what you are saying - as a Ruby 'Newby' you have presented to me some alternative ways of expressing things, for which I thank you! Roger -------------------------------------------------- From: "Jonas Elfström" <jonelf@gmail.com> Sent: Tuesday, August 17, 2010 10:25 PM To: <shoes@librelist.com> Subject: Re: [shoes] Validation > On Tue, Aug 17, 2010 at 1:07 PM, Roger Lovelock > <rogerlovelock@hotmail.com> wrote: >> The reason for the reverse logic is that the validation has multiple >> steps - >> I'm a Ruby newby and I find it difficult to see how to mix if and unless >> in > > if not numeric?(@dues.text) > alert > elsif ... > > But now you certainly think that I'm way too stubborn. > >> the same structure. So I think my logic is the right way around - In >> pseudo >> code ... >> >> if description contains invalid chars then >> alert >> elsif description is > 30 chars then >> alert >> elsif dues are not numeric then >> alert >> else >> save my update >> end >> >> but as you say a matter of taste. > > As a user I like when all my failed validations in a form is presented at > once. > > alerts=[] > alerts<<"Description has invalid characters." if not > valid_chars?(@desc.text) > alerts<<"Description should have a max length of 30." if > @desc.text.length>30 > alerts<<"Dues not numeric." if not numeric?(@dues.text) > if alerts.empty? > save > else > alert(alerts.join("\n")) > end > > -- > Jonas Elfström >
You said you're validating a form? Are you using Rails? If so... http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html validates_numericality_of is what you want. ;) Oh, and for that whole x =~ // ? false : true thing... you can just use x !~ // instead.
I got the impression he was doing it in shoes. Although, now that I think about it. Using ActiveRecord In Shoes would be a really fun blog post to write! On Tue, Aug 17, 2010 at 11:17 AM, Steve Klabnik <steve@steveklabnik.com> wrote: > You said you're validating a form? Are you using Rails? If so... > http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html > > validates_numericality_of is what you want. ;) > > Oh, and for that whole x =~ // ? false : true thing... you can just > use x !~ // instead. >
It'll be easier with the new ActiveModel; you can just do this: > class Person > include ActiveModel::Validations > > validates_presence_of :name > attr_accessor :name > def initialize(name) > @name = name > end > end > > Person.new.valid? #=> false > Person.new.errors #=> {:name => ["cannot be blank"]} # localizable of course > Person.new("matz").valid? #=> true (learn more here: http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3/ )
But is that just for Rails, or applicable in Shoes? -------------------------------------------------- From: "Steve Klabnik" <steve@steveklabnik.com> Sent: Wednesday, August 18, 2010 2:04 AM To: <shoes@librelist.com> Subject: Re: [shoes] Validation > It'll be easier with the new ActiveModel; you can just do this: > >> class Person >> include ActiveModel::Validations >> >> validates_presence_of :name >> attr_accessor :name >> def initialize(name) >> @name = name >> end >> end >> >> Person.new.valid? #=> false >> Person.new.errors #=> {:name => ["cannot be blank"]} # >> localizable of course >> Person.new("matz").valid? #=> true > > > (learn more here: > http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3/ > ) >
Sorry, I forget that you can use ActiveRecord in non-Rails things, too. The solution I presented is useful anywhere you have a class that involves ActiveRecord. I doubt you're using it with Shoes, but, the solution that I linked involving ActiveModel would work really nicely with Shoes. However, the new ActiveModel won't be out for another month or two, so it's not great at the moment. But then, it'd be great.
It's a bit old, but activerecord worked for this guy http://unixmonkey.net/?p=27 On Wed, 2010-08-18 at 07:17 +1000, Roger Lovelock wrote: > But is that just for Rails, or applicable in Shoes? > -------------------------------------------------- > From: "Steve Klabnik" <steve@steveklabnik.com> > Sent: Wednesday, August 18, 2010 2:04 AM > To: <shoes@librelist.com> > Subject: Re: [shoes] Validation > > > It'll be easier with the new ActiveModel; you can just do this: > > > >> class Person > >> include ActiveModel::Validations > >> > >> validates_presence_of :name > >> attr_accessor :name > >> def initialize(name) > >> @name = name > >> end > >> end > >> > >> Person.new.valid? #=> false > >> Person.new.errors #=> {:name => ["cannot be blank"]} # > >> localizable of course > >> Person.new("matz").valid? #=> true > > > > > > (learn more here: > > http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3/ > > ) > >
Thanks for that - I'll check it out! Roger -------------------------------------------------- From: "Cecil Coupe" <ccoupe@cableone.net> Sent: Wednesday, August 18, 2010 7:37 AM To: <shoes@librelist.com> Subject: Re: [shoes] Validation > It's a bit old, but activerecord worked for this guy > http://unixmonkey.net/?p=27 > > > On Wed, 2010-08-18 at 07:17 +1000, Roger Lovelock wrote: >> But is that just for Rails, or applicable in Shoes? >> -------------------------------------------------- >> From: "Steve Klabnik" <steve@steveklabnik.com> >> Sent: Wednesday, August 18, 2010 2:04 AM >> To: <shoes@librelist.com> >> Subject: Re: [shoes] Validation >> >> > It'll be easier with the new ActiveModel; you can just do this: >> > >> >> class Person >> >> include ActiveModel::Validations >> >> >> >> validates_presence_of :name >> >> attr_accessor :name >> >> def initialize(name) >> >> @name = name >> >> end >> >> end >> >> >> >> Person.new.valid? #=> false >> >> Person.new.errors #=> {:name => ["cannot be blank"]} # >> >> localizable of course >> >> Person.new("matz").valid? #=> true >> > >> > >> > (learn more here: >> > http://www.engineyard.com/blog/2009/my-five-favorite-things-about-rails-3/ >> > ) >> > > > >
On Tue, 2010-08-17 at 16:36 +1000, Roger Lovelock wrote: > OK - this is the one I have finally settled on with one variation - I > reversed the true and false - and called the function 'notnumeric?' - all > tested and working great! Then it's all good! Working is so much better than not working. > if notnumeric?(@dues.text) then > alert "Error - Dues not numeric!" > @dues.focus > return > end > > > def notnumeric?(x) > x =~ /^\d+(\.\d{1,2})?$/ ? false : true > end > > Thanks everyone for the great support while I worked through what was > probably a trivial matter for a 'rubyist', but was causing me some problems, > especially as I am no expert with RegEx! > Roger > Nobody really knows regexp and if they claim to > -------------------------------------------------- > From: "Martin DeMello" <martindemello@gmail.com> > Sent: Tuesday, August 17, 2010 3:57 PM > To: <shoes@librelist.com> > Subject: Re: [shoes] Validation > > > On Tue, Aug 17, 2010 at 7:35 AM, Roger Lovelock > > <rogerlovelock@hotmail.com> wrote: > >> if not_numeric(@amount.text) then > >> alert "Not numeric!" > >> etc etc > >> It's the not_numeric test that I am having trouble with - I > > > > Another way (though it's too liberal for your current use case) is to > > construct a Float from it - that's guaranteed to match Ruby's > > definition of a numeric. > > > > def numeric?(s) > > begin > > Float(s) > > return true > > rescue > > return false > > end > > end > > > > (or, if you prefer the one-line version, > > def numeric?(s) > > Float(s) rescue false > > end > > ) > > > > However, this will allow stuff like "-1.23e10", which is not what you > > want for currency, so a simple regex works better here. > > > > def numeric?(x) > > x =~ /^\d+(\.\d{1,2})?$/ ? true : false > > end > > > > (i.e. any number of digits, optionally followed by a decimal point and > > either one or two digits) > > > > Testing in irb > > > > irb(main):004:0> def numeric?(x); x =~ /^\d+(\.\d{1,2})?$/ ? true : false; > > end > > => nil > > irb(main):005:0> numeric? "1.23x" > > => false > > irb(main):006:0> numeric? "1.23" > > => true > > irb(main):007:0> numeric? "1.2" > > => true > > irb(main):008:0> numeric? "1.234" > > => false > > irb(main):009:0> numeric? "1." > > => false > > irb(main):010:0> numeric? "1" > > => true > > irb(main):011:0> numeric? ".23" # <----- note that this fails!! > > => false > > irb(main):012:0> numeric? "0.23" > > => true > > > > If you want to allow fractions like ".23" without the leading 0, use > > this regex instead > > > > x =~ /^(\d+(\.\d{1,2})?|\.\d{1,2})$/ > > > > which means "either the previous regex, or a compulsory decimal point > > followed by one or two digits" > > > > martin > >
Thanks - I'll give it a go! Roger -------------------------------------------------- From: "Martin DeMello" <martindemello@gmail.com> Sent: Tuesday, August 17, 2010 3:57 PM To: <shoes@librelist.com> Subject: Re: [shoes] Validation > On Tue, Aug 17, 2010 at 7:35 AM, Roger Lovelock > <rogerlovelock@hotmail.com> wrote: >> if not_numeric(@amount.text) then >> alert "Not numeric!" >> etc etc >> It's the not_numeric test that I am having trouble with - I > > Another way (though it's too liberal for your current use case) is to > construct a Float from it - that's guaranteed to match Ruby's > definition of a numeric. > > def numeric?(s) > begin > Float(s) > return true > rescue > return false > end > end > > (or, if you prefer the one-line version, > def numeric?(s) > Float(s) rescue false > end > ) > > However, this will allow stuff like "-1.23e10", which is not what you > want for currency, so a simple regex works better here. > > def numeric?(x) > x =~ /^\d+(\.\d{1,2})?$/ ? true : false > end > > (i.e. any number of digits, optionally followed by a decimal point and > either one or two digits) > > Testing in irb > > irb(main):004:0> def numeric?(x); x =~ /^\d+(\.\d{1,2})?$/ ? true : false; > end > => nil > irb(main):005:0> numeric? "1.23x" > => false > irb(main):006:0> numeric? "1.23" > => true > irb(main):007:0> numeric? "1.2" > => true > irb(main):008:0> numeric? "1.234" > => false > irb(main):009:0> numeric? "1." > => false > irb(main):010:0> numeric? "1" > => true > irb(main):011:0> numeric? ".23" # <----- note that this fails!! > => false > irb(main):012:0> numeric? "0.23" > => true > > If you want to allow fractions like ".23" without the leading 0, use > this regex instead > > x =~ /^(\d+(\.\d{1,2})?|\.\d{1,2})$/ > > which means "either the previous regex, or a compulsory decimal point > followed by one or two digits" > > martin >
There are probably no examples on the web because it's so common that nobody wrote a hint and there are so many ways to do it. here's something you can explore with to get you started. > f = '12.34' > n = '12.x5' > i = '5432' > def numstring (s) > t = s[/(\d)+\.(\d+)/] > # puts t.class > return t != nil > end > puts "#{f} => #{numstring f}" > puts "#{n} => #{numstring n}" > puts "#{1} => #{numstring i}" On Tue, 2010-08-17 at 12:05 +1000, Roger Lovelock wrote: > Sorry to be putting such a Ruby Newby question to the forum, but I > have done extensive searching on the web and this seems to be a topic > not handled very well by Ruby! I am entering a dollar amount in a > Shoes edit line. I just want to test it and if it is not numeric to > give an alert etc etc. I can't find a simple test that will work - I > just want to test for 0 to 9 and the decimal point - I have tried > regex and trying to convert to float etc etc, but none seem to work > for me - what would be ideal is a simple routine such as :- > if not_numeric(@amount.text) then > alert "Not numeric!" > etc etc > It's the not_numeric test that I am having trouble with > - I have copied samples from the web without success (had to try to > convert many from is_numeric to not_numeric). This should be easy, and > I'm sure it is - once you know how! > Any help much appreciated - Thanks > Roger
Tried the following code (I only have Shoes - not Ruby and irb) - gave true with a string such as "5.12", BUT a = "a5.12k" def numstring(s) t = s[/(\d)+\.(\d+)/] return t != nil end alert numstring(a) Gave a true result! -------------------------------------------------- From: "Cecil Coupe" <ccoupe@cableone.net> Sent: Tuesday, August 17, 2010 12:59 PM To: <shoes@librelist.com> Subject: Re: [shoes] Validation > There are probably no examples on the web because it's so common that > nobody wrote a hint and there are so many ways to do it. > here's something you can explore with to get you started. > > >> f = '12.34' >> n = '12.x5' >> i = '5432' >> def numstring (s) >> t = s[/(\d)+\.(\d+)/] >> # puts t.class >> return t != nil >> end >> puts "#{f} => #{numstring f}" >> puts "#{n} => #{numstring n}" >> puts "#{1} => #{numstring i}" > > > > On Tue, 2010-08-17 at 12:05 +1000, Roger Lovelock wrote: >> Sorry to be putting such a Ruby Newby question to the forum, but I >> have done extensive searching on the web and this seems to be a topic >> not handled very well by Ruby! I am entering a dollar amount in a >> Shoes edit line. I just want to test it and if it is not numeric to >> give an alert etc etc. I can't find a simple test that will work - I >> just want to test for 0 to 9 and the decimal point - I have tried >> regex and trying to convert to float etc etc, but none seem to work >> for me - what would be ideal is a simple routine such as :- >> if not_numeric(@amount.text) then >> alert "Not numeric!" >> etc etc >> It's the not_numeric test that I am having trouble with >> - I have copied samples from the web without success (had to try to >> convert many from is_numeric to not_numeric). This should be easy, and >> I'm sure it is - once you know how! >> Any help much appreciated - Thanks >> Roger > > >
On Tue, 2010-08-17 at 14:16 +1000, Roger Lovelock wrote: > Tried the following code (I only have Shoes - not Ruby and irb) - gave true > with a string such as "5.12", BUT > > a = "a5.12k" > def numstring(s) > t = s[/(\d)+\.(\d+)/] > return t != nil > end > alert numstring(a) > > Gave a true result! As it should. It found the numbers in the string. You could modify the function/regexp to remove all non-numerics or fail when it sees one. Bonus points for handling blanks anywhere in the string and keyboard stutter of two or more decimal points in the string. The decimal point may not be even be '.' -- I'm told some some folks use a comma to separate fractions.The right solution depends on your audience and the amount of work you want to put into it. > > -------------------------------------------------- > From: "Cecil Coupe" <ccoupe@cableone.net> > Sent: Tuesday, August 17, 2010 12:59 PM > To: <shoes@librelist.com> > Subject: Re: [shoes] Validation > > > There are probably no examples on the web because it's so common that > > nobody wrote a hint and there are so many ways to do it. > > here's something you can explore with to get you started. > > > > > >> f = '12.34' > >> n = '12.x5' > >> i = '5432' > >> def numstring (s) > >> t = s[/(\d)+\.(\d+)/] > >> # puts t.class > >> return t != nil > >> end > >> puts "#{f} => #{numstring f}" > >> puts "#{n} => #{numstring n}" > >> puts "#{1} => #{numstring i}" > > > > > > > > On Tue, 2010-08-17 at 12:05 +1000, Roger Lovelock wrote: > >> Sorry to be putting such a Ruby Newby question to the forum, but I > >> have done extensive searching on the web and this seems to be a topic > >> not handled very well by Ruby! I am entering a dollar amount in a > >> Shoes edit line. I just want to test it and if it is not numeric to > >> give an alert etc etc. I can't find a simple test that will work - I > >> just want to test for 0 to 9 and the decimal point - I have tried > >> regex and trying to convert to float etc etc, but none seem to work > >> for me - what would be ideal is a simple routine such as :- > >> if not_numeric(@amount.text) then > >> alert "Not numeric!" > >> etc etc > >> It's the not_numeric test that I am having trouble with > >> - I have copied samples from the web without success (had to try to > >> convert many from is_numeric to not_numeric). This should be easy, and > >> I'm sure it is - once you know how! > >> Any help much appreciated - Thanks > >> Roger > > > > > >
Thanks for all the suggestions - I'll try them out! Roger -------------------------------------------------- From: "Cecil Coupe" <ccoupe@cableone.net> Sent: Tuesday, August 17, 2010 12:59 PM To: <shoes@librelist.com> Subject: Re: [shoes] Validation > There are probably no examples on the web because it's so common that > nobody wrote a hint and there are so many ways to do it. > here's something you can explore with to get you started. > > >> f = '12.34' >> n = '12.x5' >> i = '5432' >> def numstring (s) >> t = s[/(\d)+\.(\d+)/] >> # puts t.class >> return t != nil >> end >> puts "#{f} => #{numstring f}" >> puts "#{n} => #{numstring n}" >> puts "#{1} => #{numstring i}" > > > > On Tue, 2010-08-17 at 12:05 +1000, Roger Lovelock wrote: >> Sorry to be putting such a Ruby Newby question to the forum, but I >> have done extensive searching on the web and this seems to be a topic >> not handled very well by Ruby! I am entering a dollar amount in a >> Shoes edit line. I just want to test it and if it is not numeric to >> give an alert etc etc. I can't find a simple test that will work - I >> just want to test for 0 to 9 and the decimal point - I have tried >> regex and trying to convert to float etc etc, but none seem to work >> for me - what would be ideal is a simple routine such as :- >> if not_numeric(@amount.text) then >> alert "Not numeric!" >> etc etc >> It's the not_numeric test that I am having trouble with >> - I have copied samples from the web without success (had to try to >> convert many from is_numeric to not_numeric). This should be easy, and >> I'm sure it is - once you know how! >> Any help much appreciated - Thanks >> Roger > > >
You could try something like: a=5.12 a.class==Float #returns true Try it out in irb. I know you're probably looking for something a little more forgiving (numbers that aren't floats would be valid too) but that's probably the first iteration I'd go with until I started testing more cases... On 16 Aug 2010, at 10:05 PM, "Roger Lovelock" <rogerlovelock@hotmail.com> wrote: > Sorry to be putting such a Ruby Newby question to the forum, but I have done extensive searching on the web and this seems to be a topic not handled very well by Ruby! I am entering a dollar amount in a Shoes edit line. I just want to test it and if it is not numeric to give an alert etc etc. I can't find a simple test that will work - I just want to test for 0 to 9 and the decimal point - I have tried regex and trying to convert to float etc etc, but none seem to work for me - what would be ideal is a simple routine such as :- > if not_numeric(@amount.text) then > alert "Not numeric!" > etc etc > It's the not_numeric test that I am having trouble with - I have copied samples from the web without success (had to try to convert many from is_numeric to not_numeric). This should be easy, and I'm sure it is - once you know how! > Any help much appreciated - Thanks > Roger
Problem with this is - "5.12" returns false - and I am dealing with a text
string within an edit line.
From: Scott Werner
Sent: Tuesday, August 17, 2010 12:24 PM
To: shoes@librelist.com
Subject: Re: [shoes] Validation
You could try something like:
a=5.12
a.class==Float #returns true
Try it out in irb.
I know you're probably looking for something a little more forgiving
(numbers that aren't floats would be valid too) but that's probably the
first iteration I'd go with until I started testing more cases...
On 16 Aug 2010, at 10:05 PM, "Roger Lovelock" <rogerlovelock@hotmail.com> wrote:
Sorry to be putting such a Ruby Newby question to the forum, but I have
done extensive searching on the web and this seems to be a topic not
handled very well by Ruby! I am entering a dollar amount in a Shoes edit
line. I just want to test it and if it is not numeric to give an alert etc
etc. I can't find a simple test that will work - I just want to test for 0
to 9 and the decimal point - I have tried regex and trying to convert to
float etc etc, but none seem to work for me - what would be ideal is a
simple routine such as :-
if not_numeric(@amount.text) then
alert "Not numeric!"
etc etc
It's the not_numeric test that I am having trouble with -
I have copied samples from the web without success (had to try to convert
many from is_numeric to not_numeric). This should be easy, and I'm sure it
is - once you know how!
Any help much appreciated - Thanks
Roger
You could use a regex and to_f in combination if you are comfortable
with regexes, or alternatively you could do something like this:
Float(text_input) rescue alert("not a number")
On Mon, Aug 16, 2010 at 10:24 PM, Scott Werner <stwerner@vt.edu> wrote:
> You could try something like:
> a=5.12
> a.class==Float #returns true
> Try it out in irb.
> I know you're probably looking for something a little more forgiving
> (numbers that aren't floats would be valid too) but that's probably the
> first iteration I'd go with until I started testing more cases...
>
>
>
>
> On 16 Aug 2010, at 10:05 PM, "Roger Lovelock" <rogerlovelock@hotmail.com>
> wrote:
>
> Sorry to be putting such a Ruby Newby question to the forum, but I have done
> extensive searching on the web and this seems to be a topic not handled very
> well by Ruby! I am entering a dollar amount in a Shoes edit line. I just
> want to test it and if it is not numeric to give an alert etc etc. I can't
> find a simple test that will work - I just want to test for 0 to 9 and the
> decimal point - I have tried regex and trying to convert to float etc etc,
> but none seem to work for me - what would be ideal is a simple routine such
> as :-
> if not_numeric(@amount.text) then
> alert "Not numeric!"
> etc etc
> It's the not_numeric test that I am having trouble with - I
> have copied samples from the web without success (had to try to convert many
> from is_numeric to not_numeric). This should be easy, and I'm sure it is -
> once you know how!
> Any help much appreciated - Thanks
> Roger
This one seemed to work best for me! Thanks Roger -------------------------------------------------- From: "Evan Farrar" <evanfarrar@gmail.com> Sent: Tuesday, August 17, 2010 12:35 PM To: <shoes@librelist.com> Subject: Re: [shoes] Validation > You could use a regex and to_f in combination if you are comfortable > with regexes, or alternatively you could do something like this: > > Float(text_input) rescue alert("not a number") > > > On Mon, Aug 16, 2010 at 10:24 PM, Scott Werner <stwerner@vt.edu> wrote: >> You could try something like: >> a=5.12 >> a.class==Float #returns true >> Try it out in irb. >> I know you're probably looking for something a little more forgiving >> (numbers that aren't floats would be valid too) but that's probably the >> first iteration I'd go with until I started testing more cases... >> >> >> >> >> On 16 Aug 2010, at 10:05 PM, "Roger Lovelock" <rogerlovelock@hotmail.com> >> wrote: >> >> Sorry to be putting such a Ruby Newby question to the forum, but I have >> done >> extensive searching on the web and this seems to be a topic not handled >> very >> well by Ruby! I am entering a dollar amount in a Shoes edit line. I just >> want to test it and if it is not numeric to give an alert etc etc. I >> can't >> find a simple test that will work - I just want to test for 0 to 9 and >> the >> decimal point - I have tried regex and trying to convert to float etc >> etc, >> but none seem to work for me - what would be ideal is a simple routine >> such >> as :- >> if not_numeric(@amount.text) then >> alert "Not numeric!" >> etc etc >> It's the not_numeric test that I am having trouble with - I >> have copied samples from the web without success (had to try to convert >> many >> from is_numeric to not_numeric). This should be easy, and I'm sure it >> is - >> once you know how! >> Any help much appreciated - Thanks >> Roger >