Validate a UK phone number [closed] - c#

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
How do I validate a UK phone number in C# using a regex?

The regex in the accepted answer does not match all valid UK numbers, as it is too restricive (additional number ranges have been opened up in the meanwhile such as 0203, which it sees as invalid).
UK phone numbers follow fairly simple rules:
They can be either 10 or 11 digits long (with the exception of some special numbers, but you're unlikely to need to validate those)
They consist of an area code followed by a local number. The area code varies in length between three and five digits, and the local portion of the number takes up the remaining length of the 10 or 11 digits. For all practical purposes, no-one ever quotes just the local portion of their number, so you can ignore the distinction now, except for how it affects formatting.
They start with zero.
The second digit can be anything. Currently no valid numbers start with 04 or 06, but there's nothing stopping these ranges coming into use in the future. (03 has recently been brought into use)
They can be formatted with a set of brackets and with spaces (one or more, in varying positions), but those are all entirely optional.
Therefore, a basic working expression for UK phone numbers could look like this:
/^\(?0( *\d\)?){9,10}$/
This will check for 10 or 11 digit numbers, starting with a zero, with formatting spaces between any of the digits, and optionally a set of brackets for the area code.
(and yes, this would allow mis-matched brackets, as I'm not checking that there's only one closing bracket. Enforcing this would make the expression a lot more complex, and I don't have time for this right now, but feel free to add this if you wish)
By the way, in case you want to do additional filtering, you might want to also note the following rules:
Numbers starting 08, 09 and 070 are special price numbers, and would not generally be given as private numbers, so can be excluded if validating a private number.
07 numbers are mobile (except 070; see above) so can be excluded if you're specifically validating for a landline.

Related

How does DateTime.Parse() work in C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know that DateTime parsing can be tricky, but since there are a lot of mechanisms are relying on DateTime.Parse() especially its DateTime.Parse(string) overload, I think it makes sense to understand how does it work under the hood and how does it behave on different inputs.
What we know:
MSDN states when you use the DateTime.Parse(String) overload, the formatting is derived from the CurrentThread.Culture, however it draws the attention:
culture-specific data can change (and it did) between the different versions of the framework
the culture-specific data can be overridden by the OS settings
DateTime.Parse() tries to be smart
Because of these it's a bit hard for me to predict what will be result when somebody calls this function on different user inputs.
Even when I specify a culture, DateTime.Parse can recognize strings as valid DateTimes what you might not think to recognize. For example, all the following dates are valid - here are some of my findings:
var at = new System.Globalization.CultureInfo("de-AT", false);
System.Threading.Thread.CurrentThread.CurrentCulture = at;
// it doesn't care about the order:
DateTime.Parse("21.12.2020", at).Dump();
DateTime.Parse("2020.12.31", at).Dump();
// it accepts multiple separators:
DateTime.Parse("2020,12,31", at).Dump();
DateTime.Parse("2020/12/31", at).Dump();
DateTime.Parse("2020 12 31", at).Dump();
// it accepts multiple separators even in a single string:
DateTime.Parse("1999/12-31", at).Dump();
// year must consist at least 3 digits:
DateTime.Parse("100/12-31", at).Dump(); // this works
//DateTime.Parse("99/12-31", at).Dump(); -> this doesn't
DateTime.Parse("001/12-31", at).Dump(); // but this works again (3 digits)
// trimming (well, MSDN mentions this)
DateTime.Parse(" 100/12,31 ", at).Dump();
For me it's not so clear what's going on here. The separators / and , are not even mentioned in the DateTimeFormatInfo.CurrentInfo so I have no idea where did it come from. Are these separators hardcoded in DateTime.Parse? I tried to read the disassembled code, but it was a bit complex for me.
Is there any easy way to summarize what happens and which formats are supported?
I know that in a "real life example" if I have to parse a DateTime with a given format, I should use ParseExact instead, but since there are a lot of stuff relying on this (such as ASP.NET MVC model binding) I think it worths a question what it does exactly - and why does it recognize "100/3.14" as a valid DateTime instead of a division :)

Do upper case letters take up more memory? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to create a file system that will handle lots of searching through the directories. Would it make a difference if I used upper or lower case letters in terms of memory usage on the folder names?
Case does not affect the size of a character. Some characters take up different sizes in certain character encodings, but generally letters from the same language all have the same size.
No. Each character takes up the same amount of memory.
You can get into some technicalities with character sets and encoding, but unless you've got a really obscure one, uppercase and lowercase use the same number of bits.
No. Especially assuming that you're only using ascii characters.
No. Both are of type char which is defined in C# as 16-bit long numeric value. More reference:
https://msdn.microsoft.com/en-us/library/x9h8tsay.aspx
A data type char must have at least big enough to contain an encoding of at least the 95 different characters which make up the basic execution character set.
This equals a minimum of 8 bits, or one byte. Meaning a or A in a variable char will at least require 1 byte. So no, it's the same.

Adding two different digit Numbers in c# ( without using BigInteger) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a Task to do C#. I need to add two numbers.
The first number contains around 100 digits like "12822429847264872649624264924626466826446692............"
and second number also with 100 digits or more or less
by using this numbers i need task like add/sub/multiply/div
I done this using BigInteger in C#
But do I need to do this using arrays or strings?
Since they are both 100 digits just start with the last digit and in a for loop just add each one, but if the value is > 10 then remember to add one to the next digit.
This is how children learn to add, you just need to follow the same steps, but the answer should be in an array of 101 characters.
UPDATE:
Since you have shown some code now, it helps.
First, don't duplicate the code based on if str1 or str2 is larger, but make a function with that logic and pass in the larger one as the first parameter.
Determine the largest size and make certain the smaller value is also the same size, to make math easier.
The smaller one should have leading zeroes (padding), again to help keep the code simple.
You can also start by looking at the source code for structures such as BigInteger. They would provide you more insight into aspects such as computational efficiency and storage, particularly about multiplication and division. You can take a look at here or here.

mobile number length based on the country name selected in the dropdown [duplicate]

This question already has answers here:
RegEx for valid international mobile phone number [duplicate]
(6 answers)
Closed 9 years ago.
In my asp.net application, I have to use the validation for mobile number length based on the country name selected in the dropdown.
I have googled for the solution, but couldn't able to find exact one.
Can anybody share the usefull link or info to me?
Short answer:
You can't (unless you're limited to a very specific subset of countries).
Long answer:
Telephone systems are too varied and - in short - non-standardized to create one "catch all" solution.
While some countries, like the US, got fixed length numbers, others, e.g. Germany, got variable lenghts.
For example, the area code for landlines in Germany is always 5 digits long (4 digits if you're calling from another country). However, for mobile phones it's always 4 digits (3 digits calling from another country).
The actual (local) phone number then can be anything from 3 digits up to 7 or more.
My parents have a telephone they've got ages ago - 4 digits (9 digits total with area code). If I'd get a new contract today, I'd most likely end up with 6 or 7 digits (12+ digits total).
What you could do
Rather than checking for complete numbers, you could try to use a regular expression (see Shekhar's comment above) to verify a proper formatting (e.g. to include the country selection). While this isn't a perfect solution, it should help you avoiding confusing input users left by accident.

How to insert | and || operators in C#? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In VB.NET, I can quickly type And/AndAlso on the keyboard. In C#, I'm currently opening Character Map and copying the 'OR' vertical line character manually. Am I missing something that allows quick insertion of the line symbol?
It is also called the pipe key, on many keyboards (UK/US) it is a single broken vertical line (one some keyboards it is a single unbroken vertical line, but I mostly see it as a broken one).
Image from here.
Depends on the keyboard layout, but the | / pipe should be somewhere on the left of the enter key (US layout), or on the left of Z or 1 (first normal, the other with AltGr, UK layout).
Wherever you have the PIPE key, you could type, on the numeric keypad and keeping the ALT key pressed, the number 124
I don't know where you are from, but assume that you have a non-english keyboard. Unfortunately the C language (where this and other syntactic elements) originates from was developed with the english standard keyboard in mind.
I know some people here in Sweden are switching to english keyboard layout when coding - to get rid of the awkward placement of key C/C# characters like | { [ ] } \. (They are all combinations that require the AltGr key. Something had to be done to make place for the Swedish characters ÅÄÖ that all have their own keys.)
I have the "pipe" symbol as altGr + 1

Categories

Resources