Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7916611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:52:19+00:00 2026-06-03T14:52:19+00:00

I am trying to validate if the phone number is a digit or not

  • 0

I am trying to validate if the phone number is a digit or not :-

This is my user.rg

 number_regex = /\d[0-9]\)*\z/


 validates_format_of :phone, :with =>  number_regex, :message => "Only positive number without spaces are allowed"

This is my view.html.haml

%li
   %strong=f.label :phone, "Phone Number"
   =f.text_field :phone, :placeholder => "Your phone number"

This is controller

def edit_profile
        @user = current_user
        request.method.inspect
        if request.method == "POST"
            if @user.update_attributes(params[:user])
                sign_in(@user, :bypass => true)
                flash[:success] = "You have updated your profile successfully"
                redirect_to dashboard_index_path
            else
                flash[:error] = "Profile could not be updated"
                render :action => "edit_profile"
            end
        end  
    end

When I enter the number in the text field for the first time it validates prperly, but if I enter the correct format and then try to enter the wrong format it skips validations and I get a flash message that profile has been successfully updated, however the wrong value (with letters) is not saved.

What could be the problem here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:52:21+00:00Added an answer on June 3, 2026 at 2:52 pm

    I use this, :with => “no problem”.

    validates :phone,:presence => true,
                     :numericality => true,
                     :length => { :minimum => 10, :maximum => 15 }
    

    If you want a message,(not a MASSAGE), try this,

     validates :phone,   :presence => {:message => 'hello world, bad operation!'},
                         :numericality => true,
                         :length => { :minimum => 10, :maximum => 15 }
    

    Also check this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate a phone number without success. When I submit the form
I am trying to validate a phone number which is contained over two fields.
I am using jquery and i am trying to validate a cell phone number
I am trying to validate a phone number. The website is US based but
I'm trying to validate a phone number against a RegEx. I was provided with
I am trying to validate a (US) phone number with no extra characters in
I'm trying to validate my form. In my form is a phone number field.
I am trying to validate a phone number and require it to have 10
Hi I am trying to validate a phone number for a cookie via java-script.
I am trying to validate a string as a phone number (digits and certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.