# Regex help

**URL:** <https://www.ruby-forum.com/t/regex-help/95710>\
**Category:** Rails\
**Created:** [June 10, 2006, 2:08am UTC](https://www.ruby-forum.com/t/regex-help/95710 "2006-06-10T02:08:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_PSMueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@Nicholas\_PSMueller](https://www.ruby-forum.com/u/Nicholas_PSMueller)\
**Post date:** [June 10, 2006, 2:08am UTC](https://www.ruby-forum.com/t/regex-help/95710/1 "2006-06-10T02:08:47Z")

</div>

I am trying to write a model validates\_format\_of that will accept a  
field containing only certain characters. I want the field to only  
allow the following characters:

a-z, A-Z, 0-9 and the ‘\_’, ‘@’, ‘.’ and ‘-’ characters.

I have hunted around a bit and came up with this:

validates\_format\_of :password, :with =\> /^([a-zA-Z0-9.-\_@])+  
$/, :message =\> “illegal.”

Is that correct? I tested it a bunch, but I want to ask the  
community to be sure.

Thanks,

Nicholas P. Mueller

---

<div class="post-metadata">

**Author:** ![Nicholas\_PSMueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@Nicholas\_PSMueller](https://www.ruby-forum.com/u/Nicholas_PSMueller)\
**Post date:** [June 10, 2006, 2:18am UTC](https://www.ruby-forum.com/t/regex-help/95710/2 "2006-06-10T02:18:19Z")

</div>

I’m hardly an expert, but it looks good to me. I like to test my regex  
using  
this website: [JRX: real-time JavaScript RegExp evaluator - cuneytyilmaz.com](http://www.cuneytyilmaz.com/prog/jrx/) Just paste in your  
regex without the leading and trailing / marks, and then you can test  
input  
using the bottom box. It’s fun =D

## HTH, Ryan

View this message in context:  
[http://www.nabble.com/regex-help-t1764703.html#a4803438](http://www.nabble.com/regex-help-t1764703.html#a4803438)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Nicholas\_PSMueller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@Nicholas\_PSMueller](https://www.ruby-forum.com/u/Nicholas_PSMueller)\
**Post date:** [June 10, 2006, 2:36am UTC](https://www.ruby-forum.com/t/regex-help/95710/3 "2006-06-10T02:36:52Z")

</div>

On Jun 9, 2006, at 5:04 PM, Nicholas P. Mueller wrote:

> Is that correct? I tested it a bunch, but I want to ask the  
> community to be sure.
> 
> Thanks,
> 
> Nicholas P. Mueller
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

```
I think that looks good for what you want. You can shorten it a bit

```

if you like though 😉

/^([\w.-\_@])+$/

-Ezra
