You can do Regex
match for your purpose.
The Regex
below works for your specific examples. But, it'll accept comma ,
at random places. Demo here.
^\d+(\,\d+)*(\.\d{1,4})?$
A better Regex
would be the following. Check the demo. Base Regex taken from this post.
^\$?(\d{1,3},(\d{3},)*\d{3}|\d+)(.\d{1,4})?$
Update
To answer comment - above Regex
allows 0090
. Try this one. Demo here.
^((([1-9]\d{0,2},(\d{3},)*\d{3}|[1-9]\d*)(.\d{1,4})?)|(0\.\d{1,4}))$
Update 2
C# usage
var currencyString = "1,234,456.7890";
var regex = @"^((([1-9]\d{0,2},(\d{3},)*\d{3}|[1-9]\d*)(.\d{1,4})?)|(0\.\d{1,4}))$";
var isValidCurency = Regex.IsMatch(currencyString, regex);
Update 3
As per OP, comma ,
is allowed at any place. Updated Regex
with demo here.
^(([1-9]\d*(\,\d+)*(\.\d{1,4})?)|(0\.\d{1,4})|0)$
Update 4
Use this one. Demo HERE.
^((([1-9]\d{0,2}(,\d{3})*)(\.\d{1,4})?)|[1-9]\d*|(0\.\d{1,4})|0)$
Well, this :)
^((([1-9]\d{0,2}(,\d{3})*|[1-9]\d*)(\.\d{1,4})?)|[1-9]\d*|(0\.\d{1,4})|0)$