ives-bolt / force-dot-com-esapi

Automatically exported from code.google.com/p/force-dot-com-esapi
0 stars 0 forks source link

Suggest to include methods that validate email addresses, domains or host names #8

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Enhancement:

It would be useful if the ESAPI would provide methods for validating the format 
of an email address, a domain or a host name. These are standard validation 
situations that would come in handy for every developer.

Original issue reported on code.google.com by sascha.k...@gmail.com on 11 Feb 2013 at 4:29

GoogleCodeExporter commented 8 years ago

Original comment by apex.es...@gmail.com on 11 Feb 2013 at 7:21

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Proposed code

    // validates whether a given string looks like an email address
    // This is a simplified version of the the RFC which only matches the typical email address formats
    // See http://en.wikipedia.org/wiki/Email_address#Examples for exotic examples that are only partially valid according to this regex
    global static Boolean isValidEmail(String email) {
        pattern validPattern = pattern.compile('^[A-Za-z0-9](([_\\+\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$'); 
        matcher validMatcher = validPattern.matcher(email);
        if (validMatcher.matches()) {
            return true;
        }
        return false;
    }

    // validates whether a given string looks like a host
    // examples "acme-international.com", "sales.acme-international.com", "mail.acme-international.uk.co"
    global static Boolean isValidHost(String host) {
        pattern validPattern = pattern.compile('^([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$'); 
        matcher validMatcher = validPattern.matcher(host);
        if (validMatcher.matches()) {
            return true;
        }
        return false;
    }

    // validates whether a given string looks like a domain
    global static Boolean isValidTopDomain(String topDomain) {
        pattern validPattern = pattern.compile('^([A-Za-z0-9]+)(([\\-]?[a-zA-Z0-9]+)+)\\.([A-Za-z]{2,})$'); 
        matcher validMatcher = validPattern.matcher(topDomain);
        if (validMatcher.matches()) {
            return true;
        }
        return false;
    }

Original comment by sascha.k...@gmail.com on 12 Feb 2013 at 4:10