--- loncom/interface/lonhtmlcommon.pm	2007/03/12 18:09:36	1.157
+++ loncom/interface/lonhtmlcommon.pm	2007/05/07 14:02:45	1.158
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common html routines
 #
-# $Id: lonhtmlcommon.pm,v 1.157 2007/03/12 18:09:36 albertel Exp $
+# $Id: lonhtmlcommon.pm,v 1.158 2007/05/07 14:02:45 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1841,6 +1841,43 @@ sub set_form_elements {
     return $output;
 }
 
+##############################################
+##############################################
+
+# javascript_valid_email
+#
+# Generates javascript to validate an e-mail address.
+# Returns a javascript function which accetps a form field as argumnent, and
+# returns false if field.value does not satisfy two regular expression matches
+# for a valid e-mail address.  Backwards compatible with old browsers without
+# support for javascript RegExp (just checks for @ in field.value in this case). 
+
+sub javascript_valid_email {
+    my $scripttag .= <<'END';
+function validmail(field) {
+    var str = field.value;
+    if (window.RegExp) {
+        var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
+        var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; //"
+        var reg1 = new RegExp(reg1str);
+        var reg2 = new RegExp(reg2str);
+        if (!reg1.test(str) && reg2.test(str)) {
+            return true;
+        }
+        return false;
+    }
+    else
+    {
+        if(str.indexOf("@") >= 0) {
+            return true;
+        }
+        return false;
+    }
+}
+END
+    return $scripttag;
+}
+
 1;
 
 __END__