Regular Expressions: A Workaround - Doc JavaScript
JavaScript Regular Expressions
A Workaround
If the user is running a browser that supports JavaScript but doesn't support regular expressions, an error is generated. The browser's JavaScript interpreter encounters an unrecognized literal (/.../
) and reports an error. One can use either the RegExp()
constructor or the eval()
function instead. We'll use the first one.
First, take a look at our solution, which includes validation for older browsers that do not support regular expressions:
This form's HTML is the same as the previous one, except that it doesn't generate an error on older browsers, and also demonstrates an alternative verification procedure. Here's the script (and the form):
<SCRIPT LANGUAGE="JavaScript">
<!--
function valid(form) {
var field = form.email;
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;
field.focus();
field.select();
return false;
} else {
if(str.indexOf("@") >= 0)
return true;
field.focus();
field.select();
return false;
}
}
// -->
</SCRIPT>
<FORM METHOD="POST"
ACTION="mailto:you@yourdomain.com"
ENCTYPE="text/plain"
onSubmit="return valid(this)">
Your e-mail address:<BR><INPUT TYPE="text" NAME="email" SIZE="40"><BR>
Subject:<BR><INPUT TYPE="text" NAME="subject" SIZE="40"><BR>
Comments:<BR><TEXTAREA NAME="comments" COLS="40" ROWS="5"></TEXTAREA><BR>
<INPUT TYPE="submit" VALUE="Send Mail">
</FORM>
Before dealing with regular expressions, the function must check if they are supported by the browser:
if (window.RegExp) ...
The window.
prefix is required because the if
statement cannot evaluate an object or property whose parent object is not explicitly specified (don't ask us why).
The function then assigns the regular expressions, as strings, to local variables. Notice that all backslashes must be escaped with a backslash (\\
), so these characters remain backslashes in the string. If you do not escape these backslashes, they are evaluated as metacharacters with the character that follows. The RegExp()
constructor requires its argument to be a regular expression, with the original backslashes in place. The string "\d"
, for example, is actually the same as "d"
, but in a regular expression these are not the same.
The next step is to create two regular expressions based on the above strings. In this script, the RegExp()
method is used to construct the regexps. The rest of the block is identical to the script in the previous section of this column.
After the else
statement, which is executed if the user is running a browser that does not support JavaScript regular expression, the function performs a very trivial e-mail verification, using the indexOf()
method to make sure the string has an "at" sign (@
).
Created: October 23, 1997, 1997
Revised: December 4, 1997
URL: https://www.webreference.com/js/column5/workaround.html