Wednesday, June 21, 2017

Numeric and decimal textbox validation by jQuery, c# Asp.Net



Numeric and decimal textbox validation by jQuery, c# Asp.Net
Refer jQuery and give class name of textbox. For the class name validate the Numeric and Decimal.

  $(document).ready(function () {

            $(".numericOnly").keypress(function (e) {
                if (String.fromCharCode(e.keyCode).match(/[^0-9]/g)) return false;
            });
            $(".numericOnly").focusout(function (e) {
                if (isNaN($(this).val())) {
                    $(this).val('');
                }
                else {
                }
            });
            $(".decimalOnly").keypress(function (e) {
                if (String.fromCharCode(e.keyCode).match(/[^0-9.]/g)) return false;
            });
            $(".decimalOnly").focusout(function (e) {
                if (isNaN($(this).val())) {
                    $(this).val('');
                }
                else {
                }
            });

        });

-------------- TEXTBOXES ----------------

<asp:TextBox ID="SAMPLE1" runat="server" class="numericOnly"></asp:TextBox>
<asp:TextBox ID="Sample2" runat="server" CssClass="decimalOnly"></asp:TextBox>