Click here to Skip to main content
15,512,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friend.
I am creating some codes for masking. my requirement is that. once I enter customer mobile number and credit number into the textbox so some number should be convert into XX or * . like 9818233223, 3298472934729347
9818xxxx23 3298XXXXXXXX347
so i have created some codes but in console application it is working fine but once i am using these codes into window forms so it is not working. i am using Regex pattern please check my codes and make it clear for me

What I have tried:

public string CustomerMobileNumberMasking(string input)
            string Output = "";
            input = string.Empty;

                if (!string.IsNullOrWhiteSpace(input))
                    string pattern = @"\d(?!\d{0,1}$)";
                    string result = Regex.Replace(input, pattern, m => new string('*', m.Length));
                    Output = result;

                return Output;
            catch (Exception ex)

               return Output;
Updated 15-Aug-22 10:25am

1 solution

Your regex is pretty much meaningless:
Any single digit, followed by missing suffix of one or zero other digits t the end of the string.

To be honest, I wouldn't use a regex to replace this: use a regex to valid the string as all numeric, and check the length - but then use string.Substring to break the string card number into three pieces and replace the centre section with stars: it'll be easier, and a lot more understandable should the rues change later!
Share this answer
faizyab 2009 16-Aug-22 2:27am    
Sir if you have any valid pattern so please provide to me . as per my requirement i can use only these codes.
please make it clear. or send me valid codes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900