Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何让我的文本框只允许数字1-14?_C#_Asp.net - Fatal编程技术网

C# 如何让我的文本框只允许数字1-14?

C# 如何让我的文本框只允许数字1-14?,c#,asp.net,C#,Asp.net,这是我到目前为止所做的,但我显然犯了一个错误 try { dblNights = Convert.ToDouble(txtNights.Text); if (dblNights > 1 && 14) { } else { string script = "alert(\"Number of Nights Must be between 1 and 14!\");"; ScriptManager

这是我到目前为止所做的,但我显然犯了一个错误

try
{
    dblNights = Convert.ToDouble(txtNights.Text);

    if (dblNights > 1 && 14)
    {
    }
    else
    {
        string script = "alert(\"Number of Nights Must be between 1 and 14!\");";
        ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
        txtNights.Focus();
    }
}//End Try

catch
{
    string script = "alert(\"Number of Nights Must be an Integer!\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);

    txtNights.Focus();
}//End Catch
如果输入了除1-14以外的数字,我不太确定如何使显示错误框。其他一切都在起作用,只是不是那样。我做错了什么


谢谢。

问题:您没有正确使用
逻辑AND运算符

这:

必须是:

       if (dblNights >= 1 && dblNights <= 14)
        {
             /*valid range some thing here*/
        }

问题:您没有正确使用
逻辑AND
运算符

这:

必须是:

       if (dblNights >= 1 && dblNights <= 14)
        {
             /*valid range some thing here*/
        }
您可以使用HTML5

<input type="number" min="1" max="14" />

您可以使用HTML5

<input type="number" min="1" max="14" />

我会使用RangeValidator


省去了你重新发明轮子的麻烦。您可以将其Type属性设置为Integer或Double,而不必担心解析值只是为了验证它们。

我会使用RangeValidator


省去了你重新发明轮子的麻烦。您可以将它的Type属性设置为Integer或Double,而不必担心解析值只是为了验证它们。

如果您的
不正确,您的
。它应该类似于
if(dblNights>=1&&dblNightsDon不要像这样使用try-catch,这是一种糟糕的样式。请改用int.TryParse。@admdrew-我只是认为RegularExpressionValidator的代码要少得多,不需要调用ScriptManager。RegisterStartupScript@PhilVallone是的,完全可以。考虑到上面的例子是服务器端,他可以同时实现这两个方面,而您的解决方案ion是客户端的。@PhilVallone我同意客户端验证是有帮助的,但如果应用程序需要,通常你也会用服务器端验证来备份。你只是不能总是保证客户端会真正遵循验证(那些讨厌的黑客似乎从来没有这样做过)。您的
if
不正确。它应该类似于
if(dblNights>=1&&dblNightsDon不要像这样使用try-catch,这是一种糟糕的样式。请改用int.TryParse。@admdrew-我只是认为RegularExpressionValidator的代码要少得多,不需要调用ScriptManager。RegisterStartupScript@PhilVallone是的,完全可以。考虑到上面的例子是服务器端,他可以同时实现这两个方面,而您的解决方案ion是客户端的。@PhilVallone我同意客户端验证是有帮助的,但如果应用程序需要,通常你也会用服务器端验证来备份。你只是不能总是保证客户端会真正遵循验证(那些讨厌的黑客似乎从来没有这样做过)。