|
Use VBScript's RegExp object to validate email address syntax
Nowadays, if your Web application requires a user to enter specific company
information, you probably have a field for an email address. No doubt, you'll want to
ensure that the address not only contains the @ and dot, but that the remaining
characters contain only letters, numerals, or underscores (and perhaps a dash or period).
At first, this may seem like a daunting task. And if you use standard VBScript's
string functions alone, it will be. Fortunately, the RegExp object provides an easier way.
The following code validates an email address in a textbox named Text1:
<head>
<script language="VBScript">
Sub checkEmail(sEmail)
Dim myReg
Set myReg = New RegExp
myReg.IgnoreCase = True
myReg.Pattern = "^[\w-\.]+@\w+\.\w+$"
msgbox myReg.Test(sEmail)
End Sub
</script>
</head>
<body>
<form>
<input type="text" id="txtEmail" name="txtEmail"></input>
<input type="button" onclick="checkEmail(document.forms(0).txtEmail.value)"
value="Verify"></input>
</form>
</body>
Here, the pattern accepts any number of numeric, underscore, letters, periods, or dash
characters before the @ character and only numerals, underscores, or letters before and
after the dot.

|