# Numericality validation strips chars from original entry

**URL:** <https://www.ruby-forum.com/t/numericality-validation-strips-chars-from-original-entry/194974>\
**Category:** Rails\
**Created:** [September 14, 2010, 5:07pm UTC](https://www.ruby-forum.com/t/numericality-validation-strips-chars-from-original-entry/194974 "2010-09-14T17:07:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jdc](https://www.ruby-forum.com/u/jdc)\
**Post date:** [September 14, 2010, 5:07pm UTC](https://www.ruby-forum.com/t/numericality-validation-strips-chars-from-original-entry/194974/1 "2010-09-14T17:07:53Z")

</div>

I’ve noticed that if I use the numericality validator and the user  
enters, for example “1234ABCD” it’ll come back with a “not a number”  
error, but it then fills the input box with “1234”, discarding the non-  
numeric input the user originally put in.

I’ve seen a similar question about this in this group, but the poster  
was looking for a way to completely blank the number upon return. I  
want something different - I’d like the original value to be  
displayed, which seems more logical to me.

What worries me about having it auto-strip the non-numeric stuff is,  
if a user has a couple of validation problems in the form, they may  
fix other ones, not noticing the numeric, which will now happily post  
because it’s been turned into a number automagically by Rails - and  
maybe not the number that was intended.

I’m afraid it may be impossible since I think it’s actually the input  
parser doing this and not the validator, but does anyone know how to  
make the original value come back instead of the parsed?

Thanks in advance.

jc

---

<div class="post-metadata">

**Author:** ![jdc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jdc](https://www.ruby-forum.com/u/jdc)\
**Post date:** [September 15, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/numericality-validation-strips-chars-from-original-entry/194974/2 "2010-09-15T14:46:12Z")

</div>

Just off the top of my head and never done it but… could you reuse  
the params value to repopulate the field if the field has errors? I  
think it should be possible. Something like:

unless @your\_object.save  
@your\_object.your\_attribute = params[:your\_object][:your\_attribute]  
if @your\_object.errors.on @your\_object.your\_attribute  
render…  
end

The syntax might be off but you should get it right easily if that’s  
the case.

---

<div class="post-metadata">

**Author:** ![jdc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jdc](https://www.ruby-forum.com/u/jdc)\
**Post date:** [September 19, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/numericality-validation-strips-chars-from-original-entry/194974/3 "2010-09-19T19:30:56Z")

</div>

I’ll give that a try. I was hoping for something less “manual” but I  
can live with this.

---

<div class="post-metadata">

**Author:** ![jdc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/919ad9/32.png) [@jdc](https://www.ruby-forum.com/u/jdc)\
**Post date:** [September 20, 2010, 12:48am UTC](https://www.ruby-forum.com/t/numericality-validation-strips-chars-from-original-entry/194974/4 "2010-09-20T00:48:48Z")

</div>

I’m sorry but I don’t know of a shorter way of doing this.

However, going back to the original problem I think that it might be  
better to do this in the model itself from a design point of view. I  
think the model should be able to handle this and leave the controller  
“unaware” of the problem. In other words, the model should be able to  
manage itself and take care of the problem. The controller shouldn’t  
need to be involved.
