仅允许asp.net表单的数字输入

时间:2013-12-09 18:08:59

标签: javascript forms asp-classic

我是asp和vb语言的新手,我希望在将输入限制为表单方面提供一些帮助。我想将输入限制为仅数字条目。目前表单功能,但如果输入任何字母或符号,我得到一个" Microsoft VBScript运行时错误' 800a000d' "

表单设置有2个输入和一个计算按钮:

<%
Dim nInput1, nInput2, nCOLInitial, _
nYourSavings, _
nCOLSavings, _ 
sSavingsText  

nInput1 = Int(request.QueryString("nInput1"))
nInput2 = Int(request.QueryString("nInput2"))
nCOLInitial = 40

nYourSavings = ((nInput2 - nCOLInitial) * 3973) * nInput1

if  nInput2 & nInput1 = 0 Then
nCOLSavings = 0
sSavingsText = ("Default Text")
ElseIf nInput2 <= 40 Then 
nCOLSavings = 0
sSavingsText = ("No Savings") 
Else
nCOLSavings = nYourSavings * 0.3123
sSavingsText = ("Savings") 
End If
%>

<form name="form1" action="page.asp" target="_self" method="get" runat="server">
<input type="number" name="nInput1" size="15" value=<% = nInput1 %>>
<input type="number" name="nInput2" size="15" value=<% = nInput2 %>>
<input type="submit" value="Calculate" name="B1">
</form>

<% = formatcurrency(nCOLSavings, 0) %>
<% = (sSavingsText) %>

我试图使用以下脚本,但它似乎只适用于Chrome:

<script type="text/javascript">
$(".numeric").numeric();
$(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });
$(".positive").numeric({ negative: false }, function() { alert("No negative values"); this.value = ""; this.focus(); });
$(".positive-integer").numeric({ decimal: false, negative: false }, function() { alert("Positive integers only"); this.value = ""; this.focus(); });
$("#remove").click(
    function(e)
    {
        e.preventDefault();
        $(".numeric,.integer,.positive").removeNumeric();
    }
);
</script>

谢谢!

3 个答案:

答案 0 :(得分:0)

正如您已标记VB.Net。如果输入是通过文本框。

If Not IsNumeric(Text1.Text) Then
    MsgBox "Please enter numbers only.", vbInformation    
 'you may also consider erasing it       
    End If

答案 1 :(得分:0)

你可以使用类似正则表达式的东西来限制输入:

<form id="form1" runat="server">
<asp:TextBox ID="txtName" runat="server"/>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:RegularExpressionValidator ID="regexpName" runat="server"     
                                ErrorMessage="This expression does not validate." 
                                ControlToValidate="txtName"     
                                ValidationExpression="^[a-zA-Z'.\s]{1,40}$" />
</form>

以下是一些文档: http://msdn.microsoft.com/en-us/library/ff650303.aspx

答案 2 :(得分:0)

基本问题是您正在尝试将非数字值CAST到Integer数据类型,这会导致您看到的“类型不匹配错误”(Microsoft VBScript运行时错误“800a000d”)。 Request.QueryString集合只是一个String数组,因此每次访问此集合时,所有值都将作为String返回。

此演示代码将导致运行时错误,因为字符串“abc”无法转换为整数:

REM Runtime error!~
nInput1 = Int("abc")

要解决此问题,必须首先使用IsNumeric()运算符检查输入是否为数字。一旦确认这些是数字,就可以安全地对它们执行操作,就好像它们是整数一样。

<%
    Dim nInput1, nInput2, nCOLInitial, _
    nYourSavings, _
    nCOLSavings, _ 
    sSavingsText  

    REM you must check that the input fields are both numeric before CASTing to Integer using Int()
    REM If you cast a string to an integer and it fails, you will see the runtime error you experienced
    If IsNumeric(request.QueryString("nInput1")) And IsNumeric(request.QueryString("nInput2")) Then

        nInput1 = Int(request.QueryString("nInput1"))
        nInput2 = Int(request.QueryString("nInput2"))
        nCOLInitial = 40

        nYourSavings = ((nInput2 - nCOLInitial) * 3973) * nInput1

        If nInput2 & nInput1 = 0 Then
            nCOLSavings = 0
            sSavingsText = ("Default Text")
        ElseIf nInput2 <= 40 Then 
            nCOLSavings = 0
            sSavingsText = ("No Savings") 
        Else
            nCOLSavings = nYourSavings * 0.3123
            sSavingsText = ("Savings") 
        End If

    Else

        REM Add warning!
        Response.Write("Input fields must be a number!")

    End If

%>

<form name="form1" action="so20477564.asp" target="_self" method="get" runat="server">
    <input type="number" name="nInput1" size="15" value="<% = nInput1 %>">
    <input type="number" name="nInput2" size="15" value="<% = nInput2 %>">
    <input type="submit" value="Calculate" name="B1">
</form>