如何只允许TextBox中的十进制数字

时间:2021-02-12 00:58:57

I am working on asp.net web application.

我正在研究asp.net Web应用程序。

I have a textbox in that textbox I need to allow decimal number before pressing the submit button. so how to do this at client side before submitting the submit button.

我在该文本框中有一个文本框我需要在按下提交按钮之前允许十进制数字。那么在提交提交按钮之前如何在客户端执行此操作。

if it could be done by javascript, ajax aur from the code behind please let me know.

如果它可以通过javascript完成,ajax aur来自后面的代码请告诉我。

3 个解决方案

#1


2  

If you want a code-behind alternative, you can rely on TryParse. Sample code for Button1 and TextBox1:

如果你想要一个代码隐藏替代品,你可以依靠TryParse。 Button1和TextBox1的示例代码:

string currentDot1 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
string currentDot2 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
string currentDot3 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.PercentDecimalSeparator;
decimal curVal = 0m;
if (TextBox1.Text.Trim().Length > 0 && (TextBox1.Text.Contains(currentDot1) || TextBox1.Text.Contains(currentDot2) || TextBox1.Text.Contains(currentDot3)) && decimal.TryParse(TextBox1.Text, out curVal))
{
    //curVal is valid decimal
}
else
{
    //NO DECIMAL
}

#2


2  

You can do it two way, either through built in validation of asp.net or using javascript. Through javascript you can do it as follows (This will prevent user from entering character key, hence only numbers will be allowed):

您可以通过内置的asp.net验证或使用javascript两种方式完成。通过javascript你可以这样做(这将阻止用户输入字符键,因此只允许数字):

<asp:Text onkeypress="return isNumber(event);" runat="server"/>

<script>
    function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }
</script>

#3


1  

Rather than writing your own custom validation code in javascript or server-side validation, there are libraries out there that allow you to do the validation client side with jquery.

不是在javascript或服务器端验证中编写自己的自定义验证代码,而是有一些库允许您使用jquery执行验证客户端。

Add these script references (you would need jquery as well):

添加这些脚本引用(您还需要jquery):

    <script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
    <script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>

Then you can do the following:

然后你可以做以下事情:

<script type="text/javascript>
        $(document).ready(function()
        {
            $("#idOfMyForm").validate(
            {
                rules: {
                    IdOfMyTextBox: {
                        required: true,
                        number: true
                    }
                }
            });
            $("#submitButton").click(function(e){
                if(!$("#idOfMyForm").valid(){
                    e.preventDefault(); //stop the form from submitting
                    alert("My form is not valid.");
                }
                //continue submitting the form, do whatever you need to do.
            });
        });
</script>

#1


2  

If you want a code-behind alternative, you can rely on TryParse. Sample code for Button1 and TextBox1:

如果你想要一个代码隐藏替代品,你可以依靠TryParse。 Button1和TextBox1的示例代码:

string currentDot1 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
string currentDot2 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
string currentDot3 = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.PercentDecimalSeparator;
decimal curVal = 0m;
if (TextBox1.Text.Trim().Length > 0 && (TextBox1.Text.Contains(currentDot1) || TextBox1.Text.Contains(currentDot2) || TextBox1.Text.Contains(currentDot3)) && decimal.TryParse(TextBox1.Text, out curVal))
{
    //curVal is valid decimal
}
else
{
    //NO DECIMAL
}

#2


2  

You can do it two way, either through built in validation of asp.net or using javascript. Through javascript you can do it as follows (This will prevent user from entering character key, hence only numbers will be allowed):

您可以通过内置的asp.net验证或使用javascript两种方式完成。通过javascript你可以这样做(这将阻止用户输入字符键,因此只允许数字):

<asp:Text onkeypress="return isNumber(event);" runat="server"/>

<script>
    function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }
</script>

#3


1  

Rather than writing your own custom validation code in javascript or server-side validation, there are libraries out there that allow you to do the validation client side with jquery.

不是在javascript或服务器端验证中编写自己的自定义验证代码,而是有一些库允许您使用jquery执行验证客户端。

Add these script references (you would need jquery as well):

添加这些脚本引用(您还需要jquery):

    <script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
    <script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>

Then you can do the following:

然后你可以做以下事情:

<script type="text/javascript>
        $(document).ready(function()
        {
            $("#idOfMyForm").validate(
            {
                rules: {
                    IdOfMyTextBox: {
                        required: true,
                        number: true
                    }
                }
            });
            $("#submitButton").click(function(e){
                if(!$("#idOfMyForm").valid(){
                    e.preventDefault(); //stop the form from submitting
                    alert("My form is not valid.");
                }
                //continue submitting the form, do whatever you need to do.
            });
        });
</script>