# Validation of security code with leading zero

**URL:** <https://www.ruby-forum.com/t/validation-of-security-code-with-leading-zero/110117>\
**Category:** Rails\
**Created:** [July 23, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/validation-of-security-code-with-leading-zero/110117 "2007-07-23T14:01:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [July 23, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/validation-of-security-code-with-leading-zero/110117/1 "2007-07-23T14:01:14Z")

</div>

Hi All,

I am trying to validate the security codes on payment cards but I am  
having problems with leading zeros.

I initially started off with

validates\_length\_of :security\_code, :is =\> 3

however this produced some unexpected results. My research suggested  
that this was down to the fact that validates length of actually  
validates the amount of bytes not the length of the string/number.

so then I changed my validation to

def validate  
unless self.security\_code.to\_s.size == 3  
errors.add(“security\_code”, “should be 3 digits”)  
end  
end

however converting a number with a leading zero such as 056 to a  
string drops the leading zero and becomes 56 which does not pass the  
validation. (I think this is my best chance if I can keep the leading  
zero)

finally i have tried

validates\_format\_of :security\_code, :with =\> /\A[0-9][0-9]  
[0-9]\Z/, :message =\> ‘must be 3 digits’

but I am no regex expert and this does not appear to work either even  
tough it seems like a rather simple regex. I have also tried other  
variations on that regex such as /\d\d\d/ with no success.

Does any one know how I can keep the leading zero or validate the  
length of this number in any other way. Surely this must be a common  
problem but I am failing to find any solutions on the net.

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [July 23, 2007, 2:18pm UTC](https://www.ruby-forum.com/t/validation-of-security-code-with-leading-zero/110117/2 "2007-07-23T14:18:03Z")

</div>

Hi –

On Mon, 23 Jul 2007, dodgyboz wrote:

> however this produced some unexpected results. My research suggested  
> that this was down to the fact that validates length of actually  
> validates the amount of bytes not the length of the string/number.

Try making security code a string (i.e., string type in the database).  
Then you should be able to do:

validates\_format\_of :security\_code, :with =\> /\A\d{3}\z/

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![dodgyboz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dodgyboz](https://www.ruby-forum.com/u/dodgyboz)\
**Post date:** [July 23, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/validation-of-security-code-with-leading-zero/110117/3 "2007-07-23T14:36:56Z")

</div>

Thanks removed\_email\_address@domain.invalid,

changing the column type in the db would appear to have been the over  
site in this situation.

Once again thank you for the speedy responce.
