How to Check if entered number is Positive or negative


To Check if entered number in the text box is positive decimal or positive integer number.

1. To check for Positive Decimal Numbers in the entered text. All you have to do is use existing functions in dotnet and write your own methods like below.


public bool CheckForPositiveDecimalNumbers(string input)
{
bool isValid = true;
int count = 0;
input = input.Trim();
for (int i = 0; i < input.Length; i++)
{
if (!char.IsDigit(input, i))
{
if (input.Length != 1)
{
if (input[i].CompareTo('.').Equals(0))
{
count++;
if (count > 1)
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
else
isValid = false;
}
}
return isValid;
}


2. To check for Positive integer Numbers in the entered text.


public static bool CheckForPositiveIntegerNumbers(string input)
{
bool isValid = true;
int count = 0;
bool decimaldotPresent = false;
input = input.Trim();
for (int i = 0; i < input.Length; i++)
{
if (!char.IsDigit(input, i))
{
if (input.Length != 1)
{
if (input[i].CompareTo('.').Equals(0))
{
decimaldotPresent = true;
count++;
if (count > 1)
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
else
{
isValid = false;
break;
}
}
else if (decimaldotPresent)
{
if (int.Parse(input[i].ToString()) != 0)
{
isValid = false;
break;
}
}
}
return isValid;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: