Spookster
05-29-2003, 06:37 AM
Regular expressions drive me nuts and I have been working on this one for awhile now for an application I am working on and almost have what I want but can't quite get it.
//Check if email address is not entered or not in proper format
if(($_POST['email'] == "") || (!eregi('^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(\.[a-zA-Z]{2,6})+$',$_POST['email']))){
$error_msgs[$error_index] = "Please enter a valid email address";
$error_index++;
}
The RegExp here:
^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(\.[a-zA-Z]{2,6})+$
right now follows these rules that I have defined:
1. username portion of email must start with one or more numbers or letters and can be followed by zero or more patterns consisting of a period followed by one or more numbers or letters
As indicated here: [a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*
2. The @ symbol will follow the username.
As indicated here: @
3. The domain portion must contain one or more numbers or letters.
As indicated here: [a-zA-Z0-9]+
4. The domain extension must contain one ore more patterns of a period followed by 2-6 letters.
As indicated here: (\.[a-zA-Z]{2,6})+
Now what I want to add into this is the use of hyphens and underscores into the username following this rule:
Username cannot start or end with a hyphen or underscore. A hyphen or underscore cannot follow nor precede a period.
I also want to be able to use hyphens in the domain portion but the domain can neither start nor end with the hyphen.
Any help would be appreciated. :)
//Check if email address is not entered or not in proper format
if(($_POST['email'] == "") || (!eregi('^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(\.[a-zA-Z]{2,6})+$',$_POST['email']))){
$error_msgs[$error_index] = "Please enter a valid email address";
$error_index++;
}
The RegExp here:
^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(\.[a-zA-Z]{2,6})+$
right now follows these rules that I have defined:
1. username portion of email must start with one or more numbers or letters and can be followed by zero or more patterns consisting of a period followed by one or more numbers or letters
As indicated here: [a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*
2. The @ symbol will follow the username.
As indicated here: @
3. The domain portion must contain one or more numbers or letters.
As indicated here: [a-zA-Z0-9]+
4. The domain extension must contain one ore more patterns of a period followed by 2-6 letters.
As indicated here: (\.[a-zA-Z]{2,6})+
Now what I want to add into this is the use of hyphens and underscores into the username following this rule:
Username cannot start or end with a hyphen or underscore. A hyphen or underscore cannot follow nor precede a period.
I also want to be able to use hyphens in the domain portion but the domain can neither start nor end with the hyphen.
Any help would be appreciated. :)