# Sub that checks whether the provided e-mail address is in a valid format (as specified by RFC 822). # # Arguments: # 1. The e-mail address to check. # # Returns: # - If valid, 1; otherwise 0. # sub emailcheck { // Code by Jeremy Morton, 2010-09-30 # If e-mail address validity check fails, tell user... Invalid e-mail address format (Must be similar to user\@domain.com) my $check = shift; # Remember: .* is greedy by default! return 0 unless $check =~ m/ ^ # RFC 822 says e-mail addys consist of any CHAR except specials, SPACE and CTLs. # We also have the @ and . to seperate user or hostnames. # CHAR is ASCII 0-127(0x00-7F). SPACE is ASCII 32(0x20). # CTLs are ASCII 0-31(0x00-1F) and 127(0x7F). Specials are ( ) < > @ , ; : \ " . [ ] # ASCII 127(0x7F) is a DEL control character - prudent to disallow this in an e-mail address. [^\x7F\x80-\xFF\x00-\x20\(\)\<\>\@\,\;\:\\\"\[\]]+ # ^ Username... even '(.)@host.com' is ok. \@ # ^ @ [^\x7F\x80-\xFF\x00-\x20\(\)\<\>\@\,\;\:\\\"\[\]\.]+ # ^ Host part 1 (no dots yet): 'blah@(host).com.com'. \. # ^ . [^\x7F\x80-\xFF\x00-\x20\(\)\<\>\@\,\;\:\\\"\[\]\.]+ [^\x7F\x80-\xFF\x00-\x20\(\)\<\>\@\,\;\:\\\"\[\]]* # ^ Host part 2 (remaining dots allowed): 'blah@host.(com.com)'. $ /x; # Address matches pattern; valid. return 1; }