Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delimit verbose email addresses in text fields

Status
Not open for further replies.

cmayo

MIS
Apr 23, 2001
159
0
0
US
I have an email app which needs to let users enter multiple email addresses into a text field, then separate out the addresses using Javascript for validation, and also separate the addresses using PHP when the form is submitted.

The script needs to support addresses in the form of

John Doe <jdoe@example.com>; Smith, Jane <jsmith@example.com>; johnd@example.com

The issue is that there's really no dependable delimiter I can use between addresses. Separating them with a semicolon will work a lot of the time, but there's no guarantee that a semicolon won't be used in the name portion. Using commas won't work since names often appear as "Smith, Jane" and the code will interpret the comma embedded in the name as a delimiter between addresses.

I guess I can improve the odds by only allowing the address portion in the text fields:

jdoe@example.com, jsmith@example.com, johnd@example.com

but under certain conditions, email addresses can contain pretty much any character, i.e.:

"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com

so there still doesn't seem to be an absolutely certain way to delimit multiple addresses in text fields.

Is there another/better way to let users enter multiple addresses into an HTML form and still keep the addresses separate?
 
Hi

I would start with something like this :
PHP:
<?php

$str=[i][green]'John Doe <jdoe@example.com>, Smith, Jane <jsmith@example.com>, johnd@example.com'[/green][/i];

preg_match_all([i][green]'/(.*?)[\s<]*(\S+@\S+?)(?:[>\s;,]+|$)/'[/green][/i],$str,$match);

print_r($match);
Code:
Array
(
    [0] => Array
        (
            [0] => John Doe <jdoe@example.com>,
            [1] => Smith, Jane <jsmith@example.com>,
            [2] => johnd@example.com
        )

    [1] => Array
        (
            [0] => John Doe
            [1] => Smith, Jane
            [2] =>
        )

    [2] => Array
        (
            [0] => jdoe@example.com
            [1] => jsmith@example.com
            [2] => johnd@example.com
        )

)

Feherke.
feherke.github.io
 
Is there another/better way to let users enter multiple addresses into an HTML form and still keep the addresses separate?

sure.

how about
Code:
<div>
<input type="text" name="emailAddressName[]" /> &nbsp;
<input type="text" name="emailAddress[]" />
</div>

allow multiple lines of the above and then each name and address pair will be in the same key of the relevant superglobal.
you then need only validate the emailAddress portion. do not do any javascript concatenation or similar first. leave it to the browser to encode and handle.

within the local part of the domain, semicolons are not permitted unless they are contained within quotation marks. likewise ASCII 32, 34, 40, 41, 44, 58, 59, 60, 62, 64, 91, 92 and 93. so any validation script could take out the enquoted sections first, or use lookaheads or similar regex tricks to parse. in fact, it is probably easiest to use a lexer to parse an email address to its fullest possibilities (treating the local and the domain part separately). Although typically the full email5 specification is not used and people stick with tried and tested email regex patterns they validate (i'd guess) more than 95% of real-life addresses.

 
Thanks! Those are good thoughts but my client has already shot down the idea of using separate fields for multiple addresses, and I just don't know regular expressions well enough to support them. Since the client is insisting that a change to semicolon delimiters will solve the issue for him, it looks like I'm going to have to go that way, anyway.
 
a textarea with aleach address on a separate line is often OK. and perhaps just ask for email addresses rather than names?
or ask them to type the names into the textarea and then use JavaScript to parse those names and pop up a multi page dialog form to capture the email.addresses?
or type the emails into a text are a and parse them client side to put them into separate text fields using the local part as the "name" and then the user can edit them if he/she wants. that might be an OK used experience.

for validation I'm sure that phpmailer has a good routine. and you/your client should consider using phpmailer in place of the standard mail() command.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top