How to validate presence of phone number or mobile phone

In my model of user.rb

there are phone number and mobile number.

i want to validate if phone number and mobile number are exist or
not(not neccessary to do both).

how to do that

Hi
As an example

validates_format_of :phone_number,:allow_nil => true,:with => /regex
here/

Sijo

i don’t understand… could you please explain more

also , i thought i use the wrong word.

There are two field like this

Mobile Phone… Phone …

validate if mobile phone Or phone is filled , not neccessary to field
both, but filled at least one.

I found another problem when i use this code

validates_presence_of :mobile_number,:if => :phone_number_blank_check
validates_presence_of :phone_number,:if => :mobile_number_blank_check

def phone_number_blank_check
self.phone_number.blank?
end

def mobile_number_blank_check
self.mobile_number.blank?
end

when both field leave blank, it happened two message told that
mobile_number can’t be blank and phone_number can’t be blank

i want it to appear only one message tell that
phone number or mobile number can’t be blank, how could i do that.

Hi

What I understood is you have to check presence of phone number and 

mobile number But not at the same time Right? If so please try the
following Assuming you have the fields phone_number and mobile_number

validates_presence_of :mobile_number,:if => :phone_number_blank_check
validates_presence_of :phone_number,:if => :mobile_number_blank_check

def phone_number_blank_check
self.phone_number.blank?
end

def mobile_number_blank_check
self.mobile_number.blank?
end

Sijo

Write your own validate routine instead of using the helpers.

On Sep 29, 11:32 am, “Thriving K.” [email protected]

Mukund wrote:

Write your own validate routine instead of using the helpers.

On Sep 29, 11:32�am, “Thriving K.” [email protected]

Is there any guideline for that… please.

Thank you

Hi

i want it to appear only one message tell that
phone number or mobile number can’t be blank, how could i do that.

You can do like

def validate
validate_phone_and_mobile([mobile_number,phone_number])
end

def validate_phone_and_mobile(numbers)

            errors.add_to_base("phone number and/or mobile number 

can’t be blank") if (numbers.first.blank? or numbers.second.blank?) or
(numbers.first.blank? and numbers.second.blank?)

end

Sijo

I have something like this in one of my apps for email and cell:

validate :presence_of_email_or_cell

def presence_of_email_or_cell
errors.add(“Either an email address or cell phone number is
required”) if email.blank? and cell_number.blank?
end

On Sep 29, 9:34 am, “John T.” [email protected]

Thriving K. wrote:

Mukund wrote:

Write your own validate routine instead of using the helpers.

On Sep 29, 11:32�am, “Thriving K.” [email protected]

Is there any guideline for that… please.

www.google.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs