<!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系统的物料代码,只允许以下字符:</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
%>