Question

I have created textbox dynamically which goes on adding on button click which is working fine, but now I want to validate that each textbox which is dynamically created should take only numerical data as input.

How can I do that? I have not used jQuery to create textbox dynamically but used C# in asp.net.

Pas de solution correcte

Autres conseils

You can use CompareValidator,

Compares the value entered by the user in an input control with the value entered in another input control, or with a constant value.

<asp:TextBox ID="TextBox1" runat="server />
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1" Type="Integer"
   Operator="DataTypeCheck" ErrorMessage="Wrong value. Value must be an integer!" />

As an alternative, you can use RegularExpressionValidator with "\d+" pattern.

Add a class in your dynamically created textbox see code

<asp:TextBox ID="TextBox1" runat="server CssClass="numeric" />

client side code

<script type="text/javascript">
  $(document).ready(function () {
     $('.numeric').keyup(function () {
                    this.value = this.value.replace(/[^0-9\.]/g, '');
                });
        });
 </script>

How many textbox you add dynamically it will work

You can add the rule to each item matched by the wildcard selector with .each(), like this:

$("#SubmitForm").validate();
$("input[id*=Hours]").each(function() {
$(this).rules("add", {
    number: true,
    messages: {
        number: "Please enter a valid Hours"
    }
});
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top