It's not easy. A pretty thorough search of the internet turned up some code that would not compile. In theory, if it could compile, we are still looking at 200 lines of code or so to call a C++ function, with all of its header specifications, to execute a regular expression. A regular expression is a preferred way to analyze what is in a string of characters. In this one case, the goal is to validate an email address.
In other languages like JavaScript, it is so much simpler. Copy the code below into a text editor. Save it with an .html extension. Run it in a browser. If you get a warning about running active content, ignore it.
<html>
<body>
<head>
<title> regextest.html regular expression test for email addresses</title>
<script>
// Returns true or false
function isValidEmail(sText) {
var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)*\w+$/;
return reEmail.test(sText);
}
</script>
</head>
<script>
sText = "arnold@arnold.com";
alert (sText + " | " + isValidEmail(sText) );
sText = "arnold@arnold.com.de";
alert (sText + " | " + isValidEmail(sText) );
sText = "arnold@arnold..com.de";
alert (sText + " | " + isValidEmail(sText) );
</script>
<br>
<p> Done </p>
</body>
</html>
You should get a succession of alert boxes that say "true", "true" and "false". The value of "true" indicates a valid email address.
There is nothing regular about the regular expression var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)*\w+$/;
Anything between the forward slashes "/" is the expression. It's arcane but it is saying that case will be ignored; there may be multiple word groups followed by periods on either side of the at "@" sign; the address cannot begin or end with a period "." and there cannot be more than one period in a row.
For more information, visit http://www.regular-expressions.info/email.html
Regular expression support has been added to DB2 400 structured query language (SQL) as of release 7.1. SQL code can be embedded in an SQL RPG program as shown below:
H option(*nodebugio:*srcstmt)
* Parameters
d this PR ExtPgm('VLDEML')
d 80a
d 10i 0
d this PI
d emailIn 80a
d theCount 10i 0
d theLength s 10i 0
d theEmail s 80a
/free
exec sql set option commit=*none, datfmt=*iso;
theCount = 0;
theEmail = emailIn;
exec sql
SELECT REGEXP_COUNT(
trim(:theEmail),
'^(?:\w+\.?)*\w+@(?:\w+\.)*\w+$')
INTO :theCount
FROM sysibm/sysdummy1;
return;
Save this code as VLDEML with the member type of SQLRPGLE. When theCount is 1, the email address is valid. When it is 0, the email address is not valid. Pass to this program an email address. It returns a 0 or 1. Again, 1 is equal to success - a valid email address.
Here is another example of why we may want to modernize our RPG programs. Few if any new enhancements are coming for RPG (or COBOL for that matter). SQL is a modern language and has been updated constantly. Plus, SQL skills on the DB2 400 platform translate nicely to other databases like MS Access, Oracle, mySQL and so on.
If the operating system for the IBM i is not 7.1 or greater, you will have to "roll your own" validation routine. Some basics would include:
One @ sign
At least one period after the @ sign
Cannot begin or end with a period
Cannot have two periods in a row
No special characters like double quotes, forward slash, backward slash and so on.
The international rules for the form of email address syntax actually allows for question marks "?" and single quotes and other special characters. In reality, the rules for many domains like gmail.com or yahoo.com will not allow special characters with the exception of the underscore character "_" and perhaps some others.
In theory, you could ping the domain name after the @ sign, but that implies that the network is always up. Also, entering email addresses for customers, vendors or employees may be done offline. Also, the results of a ping may take several seconds to complete.
Finally, be aware that new sub-domains are being added as time goes on. In addition to .com, .net, .org you also have .tv and .info as well as country codes that might follow as in: harry.chen@funnythings.com.cn. The code posted here will handle those eventualities.
In other languages like JavaScript, it is so much simpler. Copy the code below into a text editor. Save it with an .html extension. Run it in a browser. If you get a warning about running active content, ignore it.
<html>
<body>
<head>
<title> regextest.html regular expression test for email addresses</title>
<script>
// Returns true or false
function isValidEmail(sText) {
var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)*\w+$/;
return reEmail.test(sText);
}
</script>
</head>
<script>
sText = "arnold@arnold.com";
alert (sText + " | " + isValidEmail(sText) );
sText = "arnold@arnold.com.de";
alert (sText + " | " + isValidEmail(sText) );
sText = "arnold@arnold..com.de";
alert (sText + " | " + isValidEmail(sText) );
</script>
<br>
<p> Done </p>
</body>
</html>
You should get a succession of alert boxes that say "true", "true" and "false". The value of "true" indicates a valid email address.
There is nothing regular about the regular expression var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)*\w+$/;
Anything between the forward slashes "/" is the expression. It's arcane but it is saying that case will be ignored; there may be multiple word groups followed by periods on either side of the at "@" sign; the address cannot begin or end with a period "." and there cannot be more than one period in a row.
For more information, visit http://www.regular-expressions.info/email.html
Regular expression support has been added to DB2 400 structured query language (SQL) as of release 7.1. SQL code can be embedded in an SQL RPG program as shown below:
H option(*nodebugio:*srcstmt)
* Parameters
d this PR ExtPgm('VLDEML')
d 80a
d 10i 0
d this PI
d emailIn 80a
d theCount 10i 0
d theLength s 10i 0
d theEmail s 80a
/free
exec sql set option commit=*none, datfmt=*iso;
theCount = 0;
theEmail = emailIn;
exec sql
SELECT REGEXP_COUNT(
trim(:theEmail),
'^(?:\w+\.?)*\w+@(?:\w+\.)*\w+$')
INTO :theCount
FROM sysibm/sysdummy1;
return;
Save this code as VLDEML with the member type of SQLRPGLE. When theCount is 1, the email address is valid. When it is 0, the email address is not valid. Pass to this program an email address. It returns a 0 or 1. Again, 1 is equal to success - a valid email address.
Here is another example of why we may want to modernize our RPG programs. Few if any new enhancements are coming for RPG (or COBOL for that matter). SQL is a modern language and has been updated constantly. Plus, SQL skills on the DB2 400 platform translate nicely to other databases like MS Access, Oracle, mySQL and so on.
If the operating system for the IBM i is not 7.1 or greater, you will have to "roll your own" validation routine. Some basics would include:
One @ sign
At least one period after the @ sign
Cannot begin or end with a period
Cannot have two periods in a row
No special characters like double quotes, forward slash, backward slash and so on.
The international rules for the form of email address syntax actually allows for question marks "?" and single quotes and other special characters. In reality, the rules for many domains like gmail.com or yahoo.com will not allow special characters with the exception of the underscore character "_" and perhaps some others.
In theory, you could ping the domain name after the @ sign, but that implies that the network is always up. Also, entering email addresses for customers, vendors or employees may be done offline. Also, the results of a ping may take several seconds to complete.
Finally, be aware that new sub-domains are being added as time goes on. In addition to .com, .net, .org you also have .tv and .info as well as country codes that might follow as in: harry.chen@funnythings.com.cn. The code posted here will handle those eventualities.
Comment