Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for multiple imei validation

Tags:

c#

regex

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;
        }
like image 985
shariq_khan Avatar asked Dec 27 '12 07:12

shariq_khan


2 Answers

15 digits, than a pattern like (comma and 15 digits) n-times:

^[0-9]{15}(,[0-9]{15})*$
like image 163
MarcinJuraszek Avatar answered Oct 07 '22 17:10

MarcinJuraszek


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.

like image 41
nhahtdh Avatar answered Oct 07 '22 17:10

nhahtdh