PDA

View Full Version : regular expression -validator (email)


eiji
March 12th, 2006, 09:31 AM
hi there

im trying to validate an entry for emails..
could someone help me on regular expressions??

i know its incorrect cause it accepts "email@amilcom"
^(\w|_|.)*(\w|_|.)@\w.+\w+\w$

or does anyone have a regular expression for ruby on validating email addresses.. cause i have used the previous ones i have used in asp but it is not working.

could someone help me out..

thanks. :D

eiji
March 12th, 2006, 09:37 AM
incase somebody else comes to stumble at the same situation

here is a regular expression for it

/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/

taken from
http://regexlib.com/REDetails.aspx?regexp_id=295

thanks.

bluetechnx
March 16th, 2006, 12:27 AM
*snip* from the Rails documentation.

validates_format_of(*attr_names)

Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression provided.

class Person < ActiveRecord::Base
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create
end

A regular expression must be provided or else an exception will be raised.

Configuration options:

* message - A custom error message (default is: "is invalid")
* with - The regular expression used to validate the format with (note: must be supplied!)
* on Specifies when this validation is active (default is :save, other options :create, :update)
* if - Specifies a method, proc or string to call to determine if the validation should


My point being is that such a regular expression can be found in alot of places, and are in common use. I even have one I made in a personal library for PHP coding, ahhh my love hate relationship with preg_match hehe.

I much prefer:
if email ~= /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i blah

hehe