21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

if (*c != '%' || !isxdigit(c[1]) || !isxdigit(c[2])) *ptr++ = *c;<br />

else {<br />

*ptr++ = (SPC_BASE16_TO_10(c[1]) * 16) + (SPC_BASE16_TO_10(c[2]));<br />

c += 2;<br />

}<br />

}<br />

*ptr = 0;<br />

if (nbytes) *nbytes = (ptr - out); /* does not include null byte */<br />

return out;<br />

}<br />

See Also<br />

• RFC 1738: Uniform Resource Locators (URL)<br />

• Recipe 3.4<br />

3.9 Validating Email Addresses<br />

<strong>Problem</strong><br />

Your program accepts an email address as input, and you need to verify that the supplied<br />

address is valid.<br />

Solution<br />

Scan the email address supplied by the user, and validate it against the lexical rules<br />

set forth in RFC 822.<br />

Discussion<br />

RFC 822 defines the syntax for email addresses. Unfortunately, the syntax is complex,<br />

and it supports several address formats that are no longer relevant. The fortunate<br />

thing is that if anyone attempts to use one of these no-longer-relevant address<br />

formats, you can be reasonably certain they are attempting to do something they are<br />

not supposed to do.<br />

You can use the following spc_email_isvalid( ) function to check the format of an<br />

email address. It will perform only a syntactical check and will not actually attempt<br />

to verify the authenticity of the address by attempting to deliver mail to it or by performing<br />

any DNS lookups on the domain name portion of the address.<br />

The function only validates the actual email address and will not accept any associated<br />

data. For example, it will fail to validate “Bob Bobson ”,<br />

but it will successfully validate “bob@bobson.com”. If the supplied email address is<br />

syntactically valid, spc_email_isvalid( ) will return 1; otherwise, it will return 0.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Validating Email Addresses | 101

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!