客户端JavaScript校验代码:
01:<html>
02:<head>
03: <title>Validation Example</title>
04: <script langauge="Javascript">
05: // 如果输入值为空,函数返回false
06: function checkEnteredSomething (stringToCheck, fieldName) {
07: var reSpaceCheck = /^\s*$/; //创建正则表达式对象
08:
09: if (reSpaceCheck.test(stringToCheck)) {
10: alert('You must enter something into the ' + fieldName + ' field.');
11: return false; //输入出错
12: }
13:
14: return true; //输入正确
15: }
16: </script>
17:</head>
18:<body>
19: <form name="testForm" id="testForm" method="post" action=""
20: onsubmit="return(checkEnteredSomething(
window.document.testForm.username.value, 'name'));">
21: <center>
22: What is you name? <input type="text" name="username" id="username" value="" /><br />
23: <input type="submit" name="submitButton" id="submitButton" value="Enter" />
24: </center>
25: </form>
26:</body>
27:</html>
可以看到,用JavaScript编写正则表达式校验并不难。在第7行,用斜线(/)创建正则表达式对象。二个斜线之间就是正则表达式类型。"^"是字符串起始,"\s"表示空格(space)和跳格(tab),"*"是通配符,"$"是字符串结束。
第9行是正则表达式的检测方法,测试输入的字符串是否与正则表达相匹配。如果测试为真,就说明输入值为空。我把校验过程包装在一个函数里,可以将它存为包含文件,凡有需要输入校验的网页都可以用。
那么对客户端不支持JavaScript的浏览器、个人电子助理(PDA)、移动电话怎么办呢?可以用服务端ASP网页的VBScript来编写代码。
服务端VBScript校验代码:
01:<%@ LANGUAGE = VBScript
02: EnableSessionState = False%>
03:<!-- test.asp File -->
04:<%
05:Dim username
06:Dim reSpaceCheck
07:Dim formPost
08:Dim failedTest
09:
00:if Request.Form.Item("submitButton") = "Enter" then
11: formPost = true
12:else
13: formPost = false
14:end if
15:
16:if formPost then
17: username = Request.Form.Item("username")
18: Set reSpaceCheck = new RegExp
19: reSpaceCheck.pattern = "^\s*$"
20:
21: failedTest = reSpaceCheck.test(username)
22:end if
23:%>
24:<html>
25:<head>
26: <title>Validation Example</title>
27:</head>
28:<body>
29: <center>
30: <%if formPost then
31: if failedTest then%>
32: <b><font color="red">You must enter something into
the name field.</font></b>
33: <%else%>
34: <b>Hello <%=username%>!</b>
35: <%end if%>
36: <br />
37: <%end if%>
38: <form name="testForm" id="testForm" method="post" action="test.asp">
39: What is you name? <input type="text" name="username"
id="username" value="" /><br />
40: <input type="submit" name="submitButton" id="submitButton"
value="Enter" />
41: </form>
42: </center>
43:</body>
21:</html>
二者之间没有太大不同。在第18行创建正则表达式对象,19行设置正则表达类型,然后进行比较判别。
匹配示例
现在举例说明更复杂的校验,如IP地址的测试。
客户端JavaScript匹配校验代码:
01:<html>
02:<head>
03: <title>Match Example</title>
04: <script langauge="Javascript">
05: // 如果IP地址非法,返回false
06: function checkIPAddress (ipAddressString) {
07: var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
08: var passedTest = false;
09:
10: if (reSpaceCheck.test(ipAddressString)) {
11: ipAddressString.match(reSpaceCheck);
12: if (RegExp.$1 <= 255 && RegExp.$1 >= 0
13: && RegExp.$2 <= 255 && RegExp.$2 >= 0
14: && RegExp.$3 <= 255 && RegExp.$3 >= 0
15: && RegExp.$4 <= 255 && RegExp.$4 >= 0) {
16: passedTest = true;
17: }
18: }
19:
20: if (!passedTest) {
21: alert('You must enter a valid IP Address.');
22: return false;
23: }
24:
25: return true;
26: }
27: </script>
28:</head>
29:<body>
30: <form name="testForm" id="testForm" method="post" action=""
31: onsubmit="return(checkIPAddress(
window.document.testForm.ipAddress.value));">
32: <center>
33: Enter an IP Address: <input type="text" name="ipAddress"
id="ipAddress" value="" /><br />
34: <input type="submit" name="submitButton" id="submitButton"
value="Enter" />
35: </center>
36: </form>
37:</body>
38:</html>
第7行代码创建正则表达式对象。但这次表达式的类型不同。小括号()里的参数是为后面的子测试用的。"\d"表示数字匹配,"\d+"则要求至少匹配一个数字。在测试输入值匹配之后,用子测试确定每个节的数字都在0 - 255之间。子测试时用到全局对象RegExp,它在调用String.match方法时自动生成。
使用VBScript要多写些代码,但也能实现同样的功能。
服务端VBScript匹配校验代码:
01:<%@ LANGUAGE = VBScript
02: EnableSessionState = False%>
03:<!-- test2.asp File -->
04:<%
05:Dim ipAddress
06:Dim reIPAddressCheck
07:Dim formPost
08:Dim passedTest
09:Dim Matches
00:Dim num1
11:Dim num2
12:Dim num3
13:Dim num4
14:
15:if Request.Form.Item("submitButton") = "Enter" then
16: formPost = true
17:else
18: formPost = false
19:end if
20:
21:if formPost then
22: ipAddress = Request.Form.Item("ipAddress")
23: Set reSpaceCheck = new RegExp
24: reSpaceCheck.pattern = "^(\d+)\.(\d+)\.(\d+)\.(\d+)$"
25:
26: if reSpaceCheck.test(ipAddress) then
27: Set Matches = reSpaceCheck.Execute(ipAddress)
28: num1 = Matches.item(0).SubMatches.item(0)
29: num2 = Matches.item(0).SubMatches.item(1)
30: num3 = Matches.item(0).SubMatches.item(2)
31: num4 = Matches.item(0).SubMatches.item(3)
32: if num1 <= 255 and num1 >= 0 _
33: and num2 <= 255 and num2 >= 0 _
34: and num3 <= 255 and num3 >= 0 _
35: and num4 <= 255 and num4 >= 0 then
36: passedTest = true
37: end if
38: end if
39:end if
40:%>
41:<html>
42:<head>
43: <title>Match Example</title>
44:</head>
45:<body>
46: <center>
47: <%if formPost then
48: if passedTest then%>
49: <b>You entered <%=ipAddress%> as a valid IP Address.</b>
50: <%else%>
51: <b><font color="red">You must enter a valid IP Address.</font></b>
52: <%end if%>
53: <br />
54: <%end if%>
55: <form name="testForm" id="testForm" method="post" action="test2.asp">
56: Enter an IP Address: <input type="text" name="ipAddress" id="ipAddress"
value="" /><br />
57: <input type="submit" name="submitButton" id="submitButton" value="Enter" />
58: </form>
59: </center>
60:</body>
61:</html>
与JavaScript不同, VBScript没有match命令。所以用正则表达式的Execute方法来处理匹配测试。Execute返回结果中包含四个子数组,对应于每节数字的检测结果。然后检测该节数字是否在0 - 255范围内。检测无误,就得到正确的IP地址。
……