# Check if space exists on the value and if yes prompt for error

**URL:** <https://www.ruby-forum.com/t/check-if-space-exists-on-the-value-and-if-yes-prompt-for-error/128692>\
**Category:** Rails\
**Created:** [February 10, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/check-if-space-exists-on-the-value-and-if-yes-prompt-for-error/128692 "2008-02-10T17:53:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 10, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/check-if-space-exists-on-the-value-and-if-yes-prompt-for-error/128692/1 "2008-02-10T17:53:47Z")

</div>

Hi

How I can check on models controller if item has space on it and if  
yes then return error ?

ie. on form you have textbox myitem and if users fills in there like:  
xxx yyyy (ie. space on middle or end of value)  
then this should return an error saying that no space is allowed on  
that field ?

Thanks

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 10, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/check-if-space-exists-on-the-value-and-if-yes-prompt-for-error/128692/2 "2008-02-10T19:13:58Z")

</div>

On 10 Feb 2008, at 16:52, KTU wrote:

> 

validates\_format\_of ?

Fred

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 10, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/check-if-space-exists-on-the-value-and-if-yes-prompt-for-error/128692/3 "2008-02-10T20:29:22Z")

</div>

Jep I got it

validates\_format\_of :intname, :with =\> /^[a-zA-Z0-9]\*?$/, :message =\>  
‘Internal  
name can only contain letters & numbers and nospaces’

On Feb 10, 8:13 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/check-if-space-exists-on-the-value-and-if-yes-prompt-for-error/128692/4 "2008-02-11T18:54:16Z")

</div>

Is the problem that the regex validation is working correctly but you  
do not know that in the controller?

You should be able to:

@model = Model.new(params[:model])  
unless @model.valid?  
… send your formatting message here …  
end
