Has anyone written cobol code to valid entry of an email address I searched the internet on this issue and only .net, c#, and php come up?

Validating input of an email address

  • Here's a thread that contains code to vaildate the format of an email address:
  • FYI, that code will flag many valid email addresses as invalid. It is designed around older USA email addresses like (someone@email.com) as long as the domain ends with ".COM", ".ORG", ".NET", or ".EDU". It doesn't accept older ".INFO" and ".GOV" domains and flags as invalid domains that include a country code (.us, .uk, .eu, etc.). It also won't work with country codes because they have a second "." character (someone@email.co.uk). In addition, there are many valid domains that exist today that don't match the old standard. Some examples include ".doctor", ".shop", ".fitness", ".dog", etc. You can view the entire list of currently valid top level domains at data.iana.org/.../tlds-alpha-by-domain.txt

    The sample code from 2006 is a good starting point. Just make sure you update it to recognize the expanded list of valid domains that exist today as well as formatting to accept valid country codes.
  • What I am trying to do is some validation during entry to say at least check to see that only one @ sign is in the string, and at least one standard character before and after the @, and at least one period after the the @ sign. In our case, the entry person would then press an icon to send a test email to the email address with instructions to reply back that the email information is correct. We do not to be responsible for the validation other than the obvious. It sounds like I will need to review the obvious tests from those other languages and convert them to Cobol.

    Thanks for the replies
  • You can find plenty of Regular Expressions on the web for validating the format of an email address.  

    Have a look here:  


    or here:


    These are just a few examples, but a Google search will turn up plenty more. Once you've found a regex you're happy with, you can use it in your ACUCOBOL code using the C$RegExp library routine.

  • Thanks Chuck I will check the sights and the use of C$RegExp.
  • Like pretty much all regexes for email-address validation, these are either broken or nearly impossible to maintain.

    RFC 5322 allows a vast range of possible address expressions, some of which aren't actually usable in practice. This page gives a regex which the author claims accurately matches RFC 5322's "preferred" syntax:


    ... but the author then goes on to explain why you shouldn't use it.

    It also doesn't seem to support angle-addrs. Most of the regexs I've seen for this purpose don't. And, personally, my feeling is that if you don't support angle-addrs, you have a usability issue, because some MUAs like to put an angle-addr in the clipboard when you copy an email address. Not handling angle-addrs is like not allowing hyphens in credit-card numbers: you're forcing the user to do a job that the machine can do trivially.

    But maintenance is the greatest concern. I use regexes every day (because vim is my editor of choice), but I try to avoid them in code. And when I do use them, I break them up into short, well-commented sections. Regular expressions are very difficult for most people to read and reason about - particularly the "extended" regular-expression syntax popularized by Perl, which defines things that are formally equivalent to Turing machines (real regexes are only DFA-equivalent) and consequently have nasty potential problems like not halting.

    In this case, I don't see any good reason to use a regular expression. Implement individual tests. There aren't that many of them, and the OP listed a bunch (exactly one @ character, etc). They aren't hard. They will be very easy for a maintainer to understand and update.
  • Never late than never.

    Maybe the small code in the zip file can be usefull. It's derivated from the example given in this thread.

    Two programs : called by a "call" or as a function.

    It can probably be ameliorated.



  • I suspect that provided utility program is good, but it looks like it does a very full-featured check.  Here are two simpler snippets that might do the quick-and-dirty check you want:

    1. Regular expression: "^[^@\.]*@[^@\.]*\.[^@\.]*$" - basically start of line, NOT at sign or period, then at sign, then more other characters, then period, then more other characters and end of line.

    2. In cobol use INSPECT:






       PRD-COUNT =1


    You could go further with INSPECT and test where in the string those fields are and make sure the period comes after the @ sign. 

  • I'd recommend using PRD-COUNT >= 1 in the IF statement since you can have
    more than one period in a valid email address, both before and after the @
    sign. For example (from the first Sherlock episode),
    **PERSONAL INFORMATION REMOVED** has three periods and is a valid email format.

  • For the fun, two other ways of testing a valid email address.

    One uses a C validation routine, the other uses regex and calls the standard regex library under Linux (we also use it to check English postcodes).

    Compile the first with :

    cob -x test_check_email.cbl fncheck_email.cbl validateEmail.c

    the other one :

    cob -x test-fnemailcheckwithregex.cbl fnemailcheckwithregex.cbl fnztrim.cbl fnregex.c

    You can link then to the runtime if you use int/gnt.

    Seems complicated but there is only one call in the code :

    if fnEmailCheck(anEmail) = 1 then *> email valid : do this... else *> email invalid : do that end-if

    You don't have to worry about the length of the email string and have one function replacing many statements that you have to duplicate in each program where you want to check an email...

    At least, we use it for years (the first) to our utmost satisfaction !