i am making a regex for imei, but i want it should optionally take multiple imei, example 123456789123456 this will be accepted and if commo i.e , will be at the end then it should also allow comma but only after the 15th digit not before the 15th digit, i want it should validate this
123456789123456
and if comma is added it should only at after the 15th digit and if comma added it should validate like
123456789123456,123456789123456,123456789123456
a comma between all the 15th digit will be accepted not before 15th digit such multiple imei i had made this ^[0-9]{15,15}|[,]$ but it is not working as it allows comma , before the 15th digit, which i dont want, how can i modify my regex? or i want to change the whole regex? i am using such code
Regex regex = new Regex("^[0-9]{15,15}|[,]$");
if (regex.IsMatch(textBox2.Text))
{
return false;
}
else
{
return true;
}
15 digits, than a pattern like (comma and 15 digits) n-times:
^[0-9]{15}(,[0-9]{15})*$
You probably want something like this:
^\d{15}(,\d{15})*$
It will accept 1 sequence of 15 digits, or multiple sequences of 15 digits, separated by commas. Note that spaces and extra commas are not allowed.
If you want to allow spaces, you should remove all spaces before validation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With