enter image description here

Actually, everything in the picture. Yuzver can enter three types of numbers: +79, 79, 89 with the same body. If the number starts at +79, then you need to remove three characters, if with 79 or 89, then two, and instead put +79.

How to implement?

  • In comments because not sisharp. I have var transformedInput = inputValue .replace (/ [^ \ d +] / g, '') .replace (/ ^ 89 / g, '+ 79') .replace (/ ^ 79 / g, '+ 79 ') .replace (/ ^ 9 / g,' + 79 ') .replace (/ ^ (02) ([459]) / g,' + $ 3752 $ 2 ') // belarus .replace (/ ^ 033 / g, '+37533') .replace (/ ^ 044 / g, '+ 37544') .replace (/ ^ 7 ([04567]) / g, '+ 77 $ 1') // kazahstan; - eri
  • Alas, I do not write in javascript, I need to paraphrase into Sharp's syntax - georgypupkin
  • so open the directory and rewrite 3 replays) - eri
  • See the StartsWith and Substring methods StartsWith Substring . They will be enough. - Zufir
  • one
    Make the first characters "+79" static and ask the user to enter only the rest - Andrey NOP

3 answers 3

Good day!

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static string StringRepl(string Str) { string pattern = @"^(\+79|79|89)[ ]?"; string Result; Regex reg = new Regex(pattern); Result = reg.Replace(Str,"+79"); return Result; } static void Main(string[] args) { while (1 == 1) { string StrA; StrA = Console.ReadLine(); Console.WriteLine(StringRepl(StrA)); if (Console.ReadKey().Key == ConsoleKey.E) { break; }; } } } } 
  • one
    What is the use of the answer? Phone "89123897989" translates as "+ 79123 + 79 + 79 + 79" - do you think this is the answer? Could have already figured out - to search from the beginning of the line using the prefix ^ . - MihailPw
  • Thank you very much! Regular expressions did not use much, so there are some omissions. - ArchiFox
  • How can I RESULT return to button click processing (pivate void button1_Click)? - georgypupkin
  • @georgypupkin, sorry, I didn’t quite understand .. if you need a call in the event handler by pressing a button, then you need to do so {{static string StringRepl (string Str) {string pattern = @ "^ (\ + 79 | 79 | 89) []? "; string Result; Regex reg = new Regex (pattern); Result = reg.Replace (Str, "+79"); return Result; } private void button1_Click (object sender, EventArgs e) {textBox2.Text = StringRepl (textBox1.Text); }}} - ArchiFox
  • Thanks, took your algorithm. - georgypupkin

I propose to do better in the comments @ Andrew, otherwise:

 string nomertelefona = GetValidString(inputstring); string GetValidString(string inputstring) { if (inputstring.StartWith("+")) inputstring = inputstring.Substring(1); return string.Format("+79{0}", inputstring.Substring(2)); } 

If you need the extension method:

 public static string GetValidString(this string inputstring) // Метод расширения { if (inputstring.StartWith("+")) inputstring = inputstring.Substring(1); return string.Format("+79{0}", inputstring.Substring(2)); } 
  • Why two identical codes? Did not understand a little. - georgypupkin
  • You can use one of these, the second is the extension method - ikram

Make a phone input by a mask, you can search for a ready control (for the benefit of a bunch of them) or do something like this yourself:

Enter phone number

The selection of a country code can be made, for example, from a drop-down list.

Right, it’s as if you’ve never in your life filled out any form in applications or on websites.

TextBox has useful properties for organizing such input, such as MaxLength .
So that the user can enter only numbers, you can use, for example, this answer .
Well, do not forget to set these "pieces" the necessary values TabIndex


Upd. I haven't opened WinForms long time, it turns out that there is a standard out-of-box control - MaskedTextBox , which has the Mask property and even a set of standard masks to choose from.
Below are screenshots of what you can write from ComboBox + MaskedTextBox

enter image description here enter image description here