spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column5


JavaScript Regular Expressions

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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:

Your e-mail address:

Subject:

Comments:

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 (@).

http://www.internet.com

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior

Created: October 23, 1997, 1997
Revised: December 4, 1997
URL: http://www.webreference.com/js/column5/workaround.html