Hey all, I am trying to mod some code that does a validation check on an e-mail address field on a form. I have been looking all over the script for Javascript that was doing it and couldn't find it, then I came across this

function is_email($email){
	$rBool=false;

if(preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $email)){
	$rBool=true;
}
return $rBool;
 

I must admit I can't decipher this. Is this an e-mail validation function? And if so can some one give me a run down on what it is doing?

What i would LIKE to do is configure the validation to only allow submissions from a specific domain...
i.e. something@specificdomain.com

Thanx for the help!!

    Yes, it is an email validation script, but a crude one at that.

    It basically does the following:
    [\w.-]+
    Look for any word character ("\w") any dot (".") or any hyphen ("-") that is of length 1 or more (no limit to maximum length).
    @
    Next there must be an "@" symbol

    \w+
    Now there must be at least one character as the domain, at least one!
    [\w.-]*?
    Any other domain valid characters (words, dots and hyphens) for any length (0 - infinity) but this can be optional!! That's what the ? means, optional.

    .\w{1,4}
    Next is a dot, followed by a word that is 1 to 4 characters long (the TLD).

    Honestly, you should NOT use that regex, and should use a little more sophisticated one like:

    [-a-z0-9!#$%&\'*+/=?^_`{|}~]+([\.[-a-z0-9!#$%&\'*+/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){2,63}

    That will validate all proper email addresses with any valid address. Whereas the one you posted wouldn't take an email address with an underscore in it 🙁

    Oh, and to validate at only a specific domain, just use this:

    [-a-z0-9!#$%&\'*+/=?^_`{|}~]+([\.[-a-z0-9!#$%&\'*+/=?^_`{|}~]+)*@specificdomain\.com

      Thank you bpat, that is exactly the info I was looking for!

        bpat1434 wrote:

        [\w.-]?
        Any other domain valid characters (words, dots and hyphens) for any length (0 - infinity) but this can be optional!! That's what the ? means, optional.

        Not quite; when ? immediately follows a quantifier (, +, {n,m} or ?), then ? is the ungreedy modifer: instead of "as many consecutive characters as possible", the quantifier instead matches "as few consecutive characters as possible". So [\w] will prefer to match five characters if it can instead of two, but [\w]? will prefer to match two instead of five. As long as it means the regexp as a whole can succeed, of course.

        Why ? was reused for this I don't know. Perhaps Larry Wall didn't want to require yet another character to be "special".

          Ah, thanks for clearin that up Weedpacket. I am no expert at regex...

          But really the initial regex was very lax and not very good because not all true email addresses would validate.

            True; it would have quite a few false negatives. It's got a few false positives as well: ".@1.1" for example.

            I'd actually prefer taking the whole process in stages: use strpos to check for the presence of @, for example, and basically build up to RFC2822 requirements (now where did I leave that Ultimate Email Regexp...?)

              lol... isn't that what I posted... found in the [man]eregi/man manual by bobocop in the user-notes?

                Well, I read you as observing that it would miss some valid addresses; I was noting that it would allow some invalid ones....

                Weedpacket wrote:

                (now where did I leave that Ultimate Email Regexp...?)

                Ah, thought so.... OOps: too long for a post. Okay, trimming out routing information (thus only implementing the "localpart@domain-spec" part) and option inline comments/whitespace it becomes (ignoring line breaks):

                /([^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff]+(?![^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff])|"[^\\\x80-\xff\r\n"]*
                (\\[^\x80-\xff]\x20[^\\\x80-\xff\r\n"]*)*")
                (\.
                ([^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff]+(?![^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff])|"[^\\\x80-\xff\r\n"]*
                (\\[^\x80-\xff]\x20[^\\\x80-\xff\r\n"]*)*"))*@
                ([^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff]+(?![^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff])|\[
                ([^\\\x80-\xff\r\n\[\]]|\\[^\x80-\xff])*\])
                (\.
                ([^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff]+(?![^(\x20)<>@,;:".\\\[\]\x00-\x19\x80-\xff])|\[
                ([^\\\x80-\xff\r\n\[\]]|\\[^\x80-\xff])*\]))*/

                NOTE: I haven't tested this.

                  wow.... wow... wow.... what's the deal with the hex characters? can't you just use regular ones? like a-z 0-9 or what not? Or are the spans you're using characters from ~ to )?

                    Write a Reply...