locked
javascript for validation in C# RRS feed

  • Question

  • User-1722422105 posted

    hi all

    my code in javascript working for numbers and also

    decimal numbers. please give some code for only numbers

    function isNumber(evt) {
    var iKeyCode = (evt.which) ? evt.which : evt.keyCode
    if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
    return false;

    return true;
    }

    Thursday, July 26, 2018 5:32 AM

All replies

  • User-369506445 posted

    Hi

    You can use RegularExpressionValidator for this. below is the sample code:

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
        ControlToValidate="TextBox1" runat="server"
        ErrorMessage="Only Numbers allowed"
        ValidationExpression="\d+">
    </asp:RegularExpressionValidator>

    above TextBox only allowed integer to be entered because in RegularExpressionValidator has field called ValidationExpression, which validate the TextBox. However, you can modify as per your requirement.

    Thursday, July 26, 2018 5:48 AM
  • User-369506445 posted

    For the server side you can use this way.... 

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) 
            && !char.IsDigit(e.KeyChar) 
            && e.KeyChar != '.')
        {
            e.Handled = true;
        }
    
        // only allow one decimal point
        if (e.KeyChar == '.' 
            && (sender as TextBox).Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }
    }

    Thursday, July 26, 2018 5:51 AM
  • User1724605321 posted

    Hi sidu ,

    decimal numbers. please give some code for only numbers

    Use javascript , you can try  :

    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;
    }
    <input type="text" class="textfield" value="" id="extra7" name="extra7" onkeypress="return isNumber(event)" />

    Best Regards,

    Nan Yu

    Thursday, July 26, 2018 7:36 AM
  • User-1722422105 posted

    Hi 

    thanks for reply 

    i want some application  code which have the all controls in asp.net with C# , ajax , jquery

    for study purpose , i want to learn 

    Thanks and regards

    siddu

    Thursday, July 26, 2018 9:20 AM
  • User-1171043462 posted

    Only Numbers

    <asp:TextBox ID="txtNumeric" runat="server" onkeyup = "keyUP(event.keyCode)" onkeydown = "return isNumeric(event.keyCode);" onpaste = "return false;"></asp:TextBox>
    <script>
    var isShift=false;
    function keyUP(keyCode)
    {
         if(keyCode==16)
               isShift = false;
    }
    function isNumeric(keyCode)
    {
          if(keyCode==16)
                isShift = true;
          return ((keyCode >= 48 && keyCode <= 57 || keyCode == 8 ||
                (keyCode >= 96 && keyCode <= 105)) && isShift == false);
    
    }
    </script>

    Ref:TextBox Validation using JavaScript

    Thursday, July 26, 2018 11:57 AM
  • User1204533129 posted

    https://stackoverflow.com/questions/7295843/allow-only-numbers-to-be-typed-in-a-textbox 

    it will work 

    bets regards ,

    kamal.

    Thursday, October 4, 2018 7:50 PM