I need to validate user input into a textbox to be a decimal number of precision exactly one? For example below are some of the cases..
if user inputs 1 then it's invalid
if user inputs 0 then its invalid
if user inputs 1.0 then its valid
if user inputs 1.1 then its valid
if user inputs 1.11 then its invalid
if user inputs 100.1 then its valid
How can I achieve this using C#(.net 3.5)?
Its a WPF textbox.
Thanks,
-Mike
For a non-regex way of achieving this:
string input;
decimal deci;
if (input.Length >=3 && input[input.Length - 2].Equals('.') && Decimal.TryParse(input , out deci))
{
// Success
}
else
{
// Fail
}
Easiest way, to me, seems to be to use regular expressions. By interpreting the user input as a string, you can compare it with something like '[0-9]+\.[0-9]'
Yes regular expression is the best way to check your condition:
For exactly NNNN.N (1234.5) use:
/^\d{4}\.\d$/
For optional .N (1234 1234. 1234.5) go with:
/^\d{4}(\.\d?)?$/
For numbers up to size of NNNN.N (1 .5 12.5 123. 1234 1234.5) go with:
/^(?=.*\d)\d{0,4}(\.\d?)?$/
And if you want to allow +/- at the beginning, then replace ^ with ^[-+]?.
Related
I'm using String.Format for displaying validation messages. I'm trying to achieve a scenario where, if decimal is there, show 12.34, else don't show any decimal points like, 12.
I tried to achieve it by using type as number. My string is,
Please enter value between {1:N} and {2:N}. // Displays 1.00 and 2.00
Please enter value between {1:N0} and {2:N0}. // Displays 1 and 2
What I should do to fix this? I need comma separation depending on culture. Using {1:G} will not provide that.
Try using :G . for isntance: Please enter value between {1:G} and {2:G}. Or {1:0.##}
The 0 means a digit will always be shown. Use a # to show optional digits.
{1:0.##}
My program asks the user to input a number from 1 - 10 in a text box in. When I the user inputs the number I have it converted into an int, by using this:
if (!int.TryParse(inputBox.Text, out input))
I used the ! because if the number cannot be TryParse'd into a int it throws an error to the user.
This works, until I enter a number that begins with a +, for example +5, or +1. It isn't catching the fact that there is a + in front of the int. If I enter more than one + it throws an error like it should.
How would someone make an error proofing line(s) of code that checks for this type of input?
I think you want to allow 1-10 without the positive sign (+).
int number;
var input = numberTextBox.Text;
if (!input.StartsWith("+") && int.TryParse(input, out number))
{
//valid input, check if it's between 1-10
}
But I think the requirement is really strange. "+10" is considered the same as "10", it is a valid input.
There is an overload of Int32.TryParse that accepts a NumberStyles value and an IFormatProvider. - https://msdn.microsoft.com/en-us/library/zf50za27(v=vs.110).aspx
The default used by Int32.TryParse is NumberStyles.Integer, which allows leading and trailing whitespace, and leading signs.
int.TryParse("+5", NumberStyles.None, NumberFormatInfo.InvariantInfo, out x), for example, returns false - however, it also returns false for -5, since the option to include a leading sign doesn't differentiate between a + and -. This is a hint that you probably shouldn't either, "+5" is every bit an integer as "5" and "-5".
You can remove + from textBox at runtime or check + character as shown in below code
if (inputBox.Text.Contains("+"))
{
// throw or show message
return;
}
if (!int.TryParse(inputBox.Text, out input))
{
}
If you want to show popup while press, you can check above condition in textbox TextChangeEvent,
Or use keypress event to restrict input.
I am trying to put a negative number in a textbox, however whenever i click on the textbox and try to type for they hyphen it creates the error, also I don't want it to create this same error if i accidentally leave the textbox blank. as it has done before
TextBox tbox = this.Controls.Find("Team" + r.ToString() + "Q" + c.ToString(), true).FirstOrDefault() as TextBox;
int t1 = Convert.ToInt32(tbox.Text);
if (r == 1) team1score += t1;`
Yes, it's probably because when you type -, the CalculateTotals method is called and it tries to convert - to an integer, and fails. You don't show how you're doing the conversion, which is the most important part of your code. You probably should do something like this:
int myInt;
if (!int.TryParse(senderTB.Text, out myInt))
{
// The value in the textbox isn't an integer.
// Use 0 as the default.
myInt = 0;
}
That's not entirely correct, though, because the user might type something like 4000000000, which is larger than an int.
A quick fix would be to modify your regular expression so that it requires at least one number:
Regex reg = new Regex(#"^-?[0-9]+$");
Replacing the * with a + won't allow just a hyphen to match. This will fix your immediate problem, but it's not complete error checking. But it might be good enough for your purposes.
In general, you can easily use regular expressions to validate the form of a signed integer (i.e. an optional hyphen followed by one or more digits), but it's very difficult to use regular expressions to make sure the number is within the range of a signed integer. Making sure that the number is not less than -2147483648 or greater than 2147483647 is rather difficult to do with regular expressions.
You probably want a combination of the approaches: use the regular expression to prevent the user from typing illegal characters into the text box, and use int.TryParse to validate the number in your computeTotals method. And rather than defaulting to a value of 0, have the program display a message box informing the user of the error.
I currently use this to format my numbers.
#string.Format("£{0:#,###,###.##}", 1000) outputs £1,000
However when I enter a zero value it does this:
#string.Format("£{0:#,###,###.##}", 0.0) outputs £
How do i make this output even when i enter zero values? e.g £0.0
Thanks
The # character means "only use a digit when you need to".
I suspect you want:
#string.Format("£{0:#,###,##0.##}", value)
However, it would generally be a better idea just to use:
#string.Format("{0:c}", value)
... and let the .NET framework do the right thing.
The first 0 is the placeholder, means the first parameter. 00 is an actual format.
For example it could be like this:
Console.WriteLine(string.Format("£{0:#,###,##0.##}", 0));
I am attempting to validate a date text box to make sure the correct date format was entered.
I converted this vb6 code:
If (IsDate(txtBirthDate)) And (IsNumeric(Right(txtBirthDate, 4))))
into this C# code -
int output = 1;
DateTime output2;
if ((! DateTime.TryParse(txtBirthDate.Text, out output2)) & (!int.TryParse((txtBirthDate.Text.Substring(txtBirthDate.Text.Length - 5)), out output)))
{
MessageBox.Show("error")
}
What I am attempting to do is make sure that the last 4 digits of the date text box are numeric (the year - i.e 1990 in 05/10/1990) and if it is not a number, then show error. Although I cannot verify everything is numeric due to the "/" in the date format.
The code does not show an error and builds. But when I debug the application I receive an error. The error states:
Index and length must refer to a location within the string.
Parameter name: length.
Any ideas on how to accomplish this?
To check if a date is in a specific format, use DateTime.TryParseExact():
if (!DateTime.TryParseExact(output , "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out output2))
{
MessageBox.Show("error")
}
EDIT: Change the format according to your needs:
"d/M/yyyy" for UK and "M/d/yyyy" for US
Edit: It sounds like the cause of your error is your string is too short. Test the string length before you test the last 4 characters.
Three other issues:
The And operator in C# is &&. You are using & which is a bitwise operator.
To check the last four characters of the string, you should use .Length - 4, not 5.
You are negating the return values in your C#, but not in your VB. To match the VB, omit the !. But, it looks like that's not what you are actually trying to do. It looks like you want to show an error message if either the string is not parsable as a date or the year portion is not 4 digits. In that case use a an or comparison (||):
if (!DateTime.TryParse(txtBirthDate.Text, out output2) ||
txtBirthDate.Text.Length < 4 ||
!int.TryParse((txtBirthDate.Text.Substring(txtBirthDate.Text.Length - 4)), out output))
{
MessageBox.Show("error")
}
Keep in mind that yyyy/M/d is also parsable as a Date, contains a 4-digit year, but will fail your test. Is that what you want?
From the error provided it looks like you just need a length check to make sure you can process the code. You should also use && instead of & (or in this case probably ||) to ensure that the boolean expressions stop executing once a true state has been encountered.
if (txtBirthDate.Text.Length < 5 ||
(!DateTime.TryParse(txtBirthDate.Text, out output2) ||
!int.TryParse((txtBirthDate.Text.Substring(txtBirthDate.Text.Length - 5)), out output)))
{
MessageBox.Show("error")
}
However, this might be a good case for the use of a regular expression.
Regex reg = new Regex("^\d{2}/\d{2}/\d{4}$");
if (!reg.IsMatch(txtBirthDate.Text))
MessageBox.Show("error");
Tweaking of the regular expression to match fringe cases (leading zero's, alternate formats, etc) may be necessary.
int result;
if (int.TryParse(txtBirthDate.Text.Substring(test.LastIndexOf("/")), out result))
{
//its good
}
else
{
//its bad
}