tech @ Beacon Deacon

Time-stamped email with basic email validation

James "Jamie" Johnson

Just for fun, I am going to put up some code for time-stamped (or date-stamped) email (e-mail). If you click on the button below, you will see some prompts that are pre-populated. It is not a server-side mailer form, and would need a configured mail application on the client machine. The code also validates the email address.


Here is the code:

<script type="text/javascript">
 var today = new Date();
 var weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
 var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
 var meridian = 'a.m.';
 var hours = today.getHours();
 if(hours>12){ meridian = 'p.m.'; hours = hours - 12; } 
 var minutes = today.getMinutes();
 if(minutes<10) { minutes = "0" + minutes.toString(); }
</script>
<script type="text/javascript">
var year = today.getYear()+1900;
</script>
<!--[if IE]>
<script>
var year = today.getYear(); 
</script>
<![endif]-->
<script type="text/javascript">
function err(){
alert('This information is required to send email.\n\nEmail will not be sent.');
return false;
}
function emailerr() {
alert('Invalid email address!\n\nTry again!');
email();
}
function email(){
 var recipient = prompt('Recipient(s)','somebody@mail.com');
 if(recipient){
   if(recipient.indexOf('@')==-1) { 
    emailerr();
   } else {
    if(recipient.length<5) { emailerr(); }
    var checkemail = recipient.split('@');
    if(checkemail.length>2) { emailerr(); }
    if(checkemail.length<2) { emailerr(); }
    if(checkemail[0]<1) { emailerr(); }
    if(checkemail[1]<3) { emailerr(); }
if(checkemail[1].indexOf('.')==-1) { emailerr(); }
    var checkemail2 = checkemail[1];
    checkemail2 = checkemail2.split('.');
    if(checkemail2[0] < 1) { emailerr(); }
    if(checkemail2[1] < 1) { emailerr(); }
   }
   var checkrecipient = recipient.split
 var date = weekday[today.getDay()] + ", " + months[today.getMonth()] + " " + today.getDate() + ", "+ year + ", at " + hours + ":" + minutes + " " + meridian; 
 var subject = prompt('Subject', date);
 if(subject) {
  var body = prompt('Message','Dear');
  if(body){
  location.href="mailto:"+recipient+"?subject="+subject+"&body="+body;
  } else { err(); }
 } else { err(); }
 } else { err(); }
}
</script>
<br />
<input onclick="email()" value="Send Email" type="button" />

Take notice. I could have used Date() instead of date to pre-populate the subject, but using my date variable allowed further customization of the output, while Date() yields what you get with seconds and timezone, etc.