<!DOCTYPE html>
<html>
<head>
<title>ERP物料代碼驗證</title>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
border-bottom: 2px solid #4CAF50;
padding-bottom: 10px;
}
.code-block {
background: #f8f8f8;
border-left: 4px solid #4CAF50;
padding: 15px;
margin: 15px 0;
overflow-x: auto;
font-family: Consolas, Monaco, 'Andale Mono', monospace;
}
.test-form {
background: #e8f5e9;
padding: 15px;
border-radius: 5px;
margin: 20px 0;
}
input[type="text"] {
padding: 8px;
width: 300px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background: #45a049;
}
.result {
margin-top: 15px;
padding: 10px;
border-radius: 4px;
}
.valid {
background: #dff0d8;
color: #3c763d;
border: 1px solid #d6e9c6;
}
.invalid {
background: #f2dede;
color: #a94442;
border: 1px solid #ebccd1;
}
.note {
background: #e3f2fd;
padding: 10px;
border-radius: 4px;
margin: 15px 0;
border-left: 4px solid #2196F3;
}
</style>
</head>
<body>
<div class="container">
<h1>ERP物料代碼驗證函數</h1>
<div class="note">
<p>此函數用于驗證ERP系統(tǒng)的物料代碼,只允許以下字符:</p>
<ul>
<li>數字 0-9</li>
<li>字母 a-z 和 A-Z</li>
<li>三個特殊字符:點(.)、下劃線(_)和連字符(-)</li>
</ul>
<p>不允許任何其他字符,包括中文、空格及其他特殊字符。</p>
</div>
<h2>ASP 驗證函數代碼</h2>
<div class="code-block">
<pre>
<%
Function IsValidProductCode(product_code)
If IsNull(product_code) Or Trim(product_code) = "" Then
IsValidProductCode = False
Exit Function
End If
Dim regEx
Set regEx = New RegExp
' 設置正則表達式模式:只允許數字、字母(大小寫)以及 ._-
regEx.Pattern = "^[a-zA-Z0-9._-]+$"
' 檢查是否完全匹配(從開始到結束)
regEx.Global = True
If regEx.Test(Trim(product_code)) Then
IsValidProductCode = True
Else
IsValidProductCode = False
End If
Set regEx = Nothing
End Function
%>
</pre>
</div>
<h2>測試驗證函數</h2>
<div class="test-form">
<form method="post" action="<%= Request.ServerVariables("SCRIPT_NAME") %>">
<p>請輸入物料代碼進行測試:</p>
<input type="text" name="test_code" value="<%= Server.HTMLEncode(Request.Form("test_code")) %>">
<input type="submit" value="驗證">
</form>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim testCode, isValid
testCode = Request.Form("test_code")
If testCode <> "" Then
isValid = IsValidProductCode(testCode)
Response.Write "<div class='result "
If isValid Then
Response.Write "valid'><strong>驗證結果:</strong>物料代碼有效"
Else
Response.Write "invalid'><strong>驗證結果:</strong>物料代碼包含非法字符"
End If
Response.Write "</div>"
End If
End If
%>
</div>
<h2>使用示例</h2>
<div class="code-block">
<pre>
<%
Dim productCode
productCode = "ABC123._-"
If IsValidProductCode(productCode) Then
Response.Write "物料代碼有效"
Else
Response.Write "物料代碼包含非法字符"
End If
%>
</pre>
</div>
<h2>函數說明</h2>
<ul>
<li>函數使用正則表達式進行驗證,確保高效準確</li>
<li>會先檢查輸入是否為Null或空字符串</li>
<li>使用<code>^[a-zA-Z0-9._-]+$</code>正則模式:
<ul>
<li><code>^</code> - 字符串開始</li>
<li><code>[a-zA-Z0-9._-]</code> - 允許的字符集</li>
<li><code>+</code> - 至少一個字符(不允許空字符串)</li>
<li><code>$</code> - 字符串結束</li>
</ul>
</li>
<li>函數返回Boolean值:True表示有效,False表示無效</li>
</ul>
</div>
</body>
</html>
<%
Function IsValidProductCode(product_code)
If IsNull(product_code) Or Trim(product_code) = "" Then
IsValidProductCode = False
Exit Function
End If
Dim regEx
Set regEx = New RegExp
' 設置正則表達式模式:只允許數字、字母(大小寫)以及 ._-
regEx.Pattern = "^[a-zA-Z0-9._-]+$"
' 檢查是否完全匹配(從開始到結束)
regEx.Global = True
If regEx.Test(Trim(product_code)) Then
IsValidProductCode = True
Else
IsValidProductCode = False
End If
Set regEx = Nothing
End Function
%>?