LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

判断物料代码中只允许存在:“数字0-9、字母a-z和A-Z、三个字符._-”,除此之外,不允许使用任何其他字符的asp函数

admin
2025年8月27日 9:8 本文热度 45

ASP物料代码验证函数

判断ERP系统的物料代码中只允许存在:“数字0-9字母a-zA-Z三个字符._-”,除此之外,不允许使用任何其他字符的asp函数。

下面是一个完整的ASP页面,包含物料代码验证函数以及一个简单的测试界面:

<!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>

&lt;%

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

%&gt;

            </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>

&lt;%

Dim productCode

productCode = "ABC123._-"


If IsValidProductCode(productCode) Then

    Response.Write "物料代码有效"

Else

    Response.Write "物料代码包含非法字符"

End If

%&gt;

            </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

%>

函数说明

这个ASP页面包含了一个完整的物料代码验证解决方案:

1、IsValidProductCode函数

  • 使用正则表达式验证字符串是否只包含允许的字符

  • 首先检查输入是否为Null或空字符串

  • 使用模式^[a-zA-Z0-9._-]+$确保从开始到结束都只包含允许字符

  • 返回Boolean值表示验证结果

2、测试功能

  • 页面包含一个表单,可以输入物料代码进行测试

  • 显示验证结果(有效或无效)

3、使用示例

  • 提供了如何在代码中调用此函数的示例

您可以直接将整个代码保存为.asp文件并在支持ASP的服务器上运行,或者只提取函数部分集成到您的项目中。


该文章在 2025/8/28 12:58:47 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved