You must Sign In to post a response.
  • Category: Visual Studio

    C# if statement for making parameters so it's only numbers

    I'm trying do an if statement to so it checks if the user entry is 0-9, a decimal point, or a minus sign. The only issue I'm having is to state those parameters in three different if statements. For numbers would I just use
    if ( number <= 0 || number >= 0)
    {
    Console.WriteLine("great");
    }

    but for the negative sign I have no idea how to go about the use of that sign other than an array. In the array would I simply use the symbol "-" and do an if statement for that array?

    As for the decimal point I was thinking of it going through a loop to see if it has any decimal points and if it counts more than 1 then it give an error. Please help!
  • #752548
    why don't you use regular expression for validation of the input

  • #752553
    Hi,

    First thing is validation. Validate the input string based on your requirement. Then you use the conditiona statment. If you are looking only numeric positive value then use below code before doing the condition check.


    function CheckNum(e)
    {
    var inpKeyCode = window.event.keyCode;
    if (inpKeyCode > 31 && (inpKeyCode < 48 || inpKeyCode > 57))
    {
    return false;
    }
    return true;
    }


    You may call above function like below,

    <input type="text" id="txtPhone" name="txtPhone" onkeypress="return CheckNum(event);" />


    Regards,
    Asheej T K

  • #752561
    Hai Christina,
    The Regular expression will be best way to handle all these. You can define there for single'.','-' sign and all..
    Only one regular expression can fulfill all the requirements.
    Otherwise you can also write the C# server side code where you need to check the characters '.' and '-' sign etc.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments