I think my question is simple.
I just need a little character check when I click in one button. If such textbox have more than one comma, I want a message: "Error. Please insert just one comma in the box."
I was searching but have not found anything like this. Can someone help me? Thanks all in advance!
Asked
Active
Viewed 1,182 times
0

Hypister
- 147
- 4
- 17
-
2Don't say "wrong validation". That will just confuse users, who likely don't know what validation means. – mason Oct 14 '14 at 23:25
-
`bool hascomma = "no comma here".Count(a => a == ',') > 1;` will return false (this is using LINQ) – RadioSpace Oct 14 '14 at 23:32
-
Although this problem CAN be solved using the counting method in the linked (and previously marked duplicate) Q&A, the test for "more than one" has a more efficient solution than counting them all. – Ben Voigt Oct 14 '14 at 23:48
-
IndexOf and LastIndexOf worked great for me, but I will take a look in another methods for future references. Thanks all! – Hypister Oct 15 '14 at 00:03
1 Answers
5
According to this POST:
You can compare IndexOf to LastIndexOf to check if there is more than one specific character in a string without explicit counting:
var s = "12121.23.2";
var ch = '.';
if (s.IndexOf(ch) != s.LastIndexOf(ch)) {
...
}
In your case:
var s = "Comma, another comma, something.";
var ch = ',';
if (s.IndexOf(ch) != s.LastIndexOf(ch)) {
...
}
You can set a boolean when you check if there is a another comma in the string (Textbox.Text).
As listed above here is another POST concerning your question.
As stated in the comments, you can use .Count()
. Look HERE for more.

Community
- 1
- 1

Hunter Mitchell
- 7,063
- 18
- 69
- 116
-
@GrantWinney Thank you very much for your feedback. I tried to also link some more posts with other solutions as well. – Hunter Mitchell Oct 14 '14 at 23:36
-
-
@Hypister you are welcome. If you take a look at the other posts, there are better and cleaner ways of doing this; as some people pointed out in the comments. – Hunter Mitchell Oct 14 '14 at 23:50
-
@EliteGamer yep, I'm taking a look in other comments to compare solutions. See ya friend! – Hypister Oct 15 '14 at 00:02