Validating Integers

Hi all,
Is there a way to do validation such as if i enter a string i should
get an alert saying it is a string enter number, I can use
validates_numericality_of but its ajax form, so i can’t get the message
there.

So i am trying to validate by form field saying that

if @project.project_number!= ~ /^[0-9]+$/
render :update do |page|
page.alert(" should be a number")
end

I also tried using

if !@project.project_number.integer?
render :update do |page|
page.alert(" should be a number")
end

But it didn’t worked. Is there a way to do this??

Naga harish Kanegolla wrote:

       page.alert(" should be a number")

Try

if not @project.project_number =~ /^[0-9]+$/

end


Michael W.

Still its not working, Its giving an alert for both integers and
strings, Anything else??

I am getting only the number which i entered in the form,

Naga harish Kanegolla wrote:

Still its not working, Its giving an alert for both integers and
strings, Anything else??

You may have other “stuff” in that variable. If you do something like:

logger.debug("@project.project_number: !#{@project.project_number}!")

do you see:

!123123124!

or stuff like:

!123123123
!

or other non-numbers between the "!"s?


Michael W.

I tried outputting like this puts @project.project_number.class, it gave
me the Fixnum so i tried using

if not @project.project_number.to_s =~ /^[0-9]+$/

end

now its taking both string and integer both into the db.
Is there anything else??

Naga harish Kanegolla wrote:

I am getting only the number which i entered in the form,

What’s your code look like right now?

Is @project.project_number coming back as a String or Fixnum or
something else? You can output @project.project_number.class to see what
class it is.

You could try something like this:

if not @project.project_number.to_s =~ /^[0-9]+$/

end

If it’s not coming back as a String.


Michael W.

What happens when you enter in letters into the form field? Is it then
coming back as a String class or is it always getting converted to a
Fixnum?

When i enter in letters it is always getting converted into Fixnum, but
i want if it is character or string give an alert and if it is a number
accept that, so wat can i do now?? Is there a way to do??

Naga harish Kanegolla wrote:

I tried outputting like this puts @project.project_number.class, it gave
me the Fixnum so i tried using

if not @project.project_number.to_s =~ /^[0-9]+$/

end

now its taking both string and integer both into the db.
Is there anything else??

If it’s a Fixnum it is an integer and you don’t need to do a pattern
match to check it. How big can the project_number get? A Fixnum is an
integer that can fix in the native machine’s word.

What happens when you enter in letters into the form field? Is it then
coming back as a String class or is it always getting converted to a
Fixnum?


Michael W.

Naga harish Kanegolla wrote:

What happens when you enter in letters into the form field? Is it then
coming back as a String class or is it always getting converted to a
Fixnum?

When i enter in letters it is always getting converted into Fixnum, but
i want if it is character or string give an alert and if it is a number
accept that, so wat can i do now?? Is there a way to do??

Without knowing all the JavaScript, AJAX and server side code that’s
involved I couldn’t tell you.

Your best bet may be to do the check in JavaScript on the client side
and put up an alert at that point before you submit the XMLHttpRequest
(the AJAX call) to the server.


Michael W.