# Validation help

**URL:** <https://www.ruby-forum.com/t/validation-help/169815>\
**Category:** Rails\
**Created:** [June 9, 2009, 7:02am UTC](https://www.ruby-forum.com/t/validation-help/169815 "2009-06-09T07:02:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 9, 2009, 7:02am UTC](https://www.ruby-forum.com/t/validation-help/169815/1 "2009-06-09T07:02:45Z")

</div>

i use this code to validate the phone field.it should have only numbers.  
but it also accepts alphapets with numbers.it should not accept other  
than numkbers  
below is my code i use.  
errors.add(:phone\_code, “\*please enter valid phone number”) unless  
self.phone\_code =~/[0-9]/

is there any fault in my regular expression …  
any helps pls…

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 9, 2009, 11:53am UTC](https://www.ruby-forum.com/t/validation-help/169815/2 "2009-06-09T11:53:51Z")

</div>

2009/6/9 Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> i use this code to validate the phone field.it should have only numbers.  
> but it also accepts alphapets with numbers.it should not accept other  
> than numkbers  
> below is my code i use.  
> errors.add(:phone\_code, “\*please enter valid phone number”) unless  
> self.phone\_code =~/[0-9]/

That will match if there is at least one number in the string. I think  
you need something like  
/\A[0-9]_\z/  
or  
/\A\d_\z/

or possibly even better you could use something like  
validates\_numericality\_of :phone\_code, :only\_integer =\> true,  
:greater\_than =\> 0  
Though I think this would allow a leading + character.

Colin

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 9, 2009, 12:00pm UTC](https://www.ruby-forum.com/t/validation-help/169815/3 "2009-06-09T12:00:19Z")

</div>

On Jun 9, 10:53 am, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That will match if there is at least one number in the string. I think  
> you need something like  
> /\A[0-9]_\z/  
> or  
> /\A\d_\z/

if you are going to use a regexp you might as well use  
validates\_format\_of rather than doing it yourself

Fred

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [June 9, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/validation-help/169815/4 "2009-06-09T17:04:50Z")

</div>

A Simple Example using RegExp:

Insert your data number here:

I don’t know if this can help you, i hope.  
See more: [Regular Expressions In Javascript | evolt.org](http://evolt.org/regexp_in_javascript).

On Tue, Jun 9, 2009 at 12:02 AM, Newb N.  
\<removed\_email\_address@domain.invalid
