Phone Number Validation in Multiple Countries [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am having trouble find regex validation patterns for phone numbers in different countries and have little time to try and write my own and was hoping a regex guru would be able to help.
I've checked the usual sources like regexlib already, so if anyone can help i'd be grateful with any of them
I need a separate phone number validation expression for each of the following:
Germany
US
Australia
New Zealand
Canada
Asia
France

The format is here.
Writing the regex is not trivial, but if you specify the rules, would not be difficult.

Instead of making an elaborate regular expression to match how you think your visitor will input their phone number, try a different approach. Take the phone number input and strip out the symbols. Then the regular expression can be simple and just check for 10 numeric digits (US number, for example). Then if you need to save the phone number in a consistent format, build that format off of the 10 digits.
This example validates U.S. phone numbers by looking for 10 numeric digits.
protected bool IsValidPhone(string strPhoneInput)
{
// Remove symbols (dash, space and parentheses, etc.)
string strPhone = Regex.Replace(strPhoneInput, #”[- ()\*\!]“, String.Empty);
// Check for exactly 10 numbers left over
Regex regTenDigits = new Regex(#”^\d{10}$”);
Match matTenDigits = regTenDigits.Match(strPhone);
return matTenDigits.Success;
}

Phone number is a number, what you want to validate there ?
Here you can see how different numbers look like.
And there is no such country like Asia, this is a mainland with several countries.

It's close to impossible to get a single regular expression that will cover all countries.
I'd go with [0-9+][0-9() ]* -- this simply allows any digit to start (or the "+" character), then any combination of digits or parentheses or spaces.
In general validation any further is not really going to be of much use. If the user of the page wants to be contacted by phone, they'll enter a valid phone number -- if not, then they won't.
A better way to enforce a correct phone number and eliminate most simple miskeying is to require the number to be entered twice -- then the user is likely to at least check it!

Related

Regular expression for ensuring domain name is English characters only [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What's a good, clear regex for matching a domain name that must consist of:
Only English alpha characters, plus numbers
Including spaces or other separator characters that are valid, and reliably handled within a domain name
To clarify, this is for the purposes of validating a domain name. Whilst there are moves in the internet community to support internationalisation of domain names, I've done a fair bit of research into this and to keep my explanation fairly simple, only domain names that include characters that are part of a modern UK English character set (including numbers) are reliably handled by the Domain Name System (DNS). I'm not indicating a desire to prohibit internationalisation - I've done a lot of work during my career doing the opposite!
To answer this, what I was looking for is something like this (tested and works). Sorry the original question wasn't explicit enough about what I was trying to do, however I've upvoted the suggestions that have helped me provide this answer to the commmunity:
^[\w- .]*$
'\w' = shorthand for [a-zA-Z0-9_]
'- .' = allow '-', ' ', '.'
asterisk = any of the previous characters zero or more times
You can use this one:
(?i)[a-z0-9\p{Z}]
where \p{Z} is "All separators" class and i ignore-case option.
You may use [a-zA-Z\d\s\p{P}]+ as the most simple solution. Or go with non-unicode solution >>
POSIX defines character classes [:...:] , but not every regex engine support them.
But alternative sets can be used then...
[:alnum:] [A-Za-z0-9] Alphanumeric characters
[:space:] [ \t\r\n\v\f] Whitespace characters
[:punct:] [\]\[!"#$%&'()*+,./:;<=>?#\^_`{|}~-] Punctuation characters
So putting them together you will get
^[A-Za-z0-9 \t\r\n\v\f\]\[!"#$%&'()*+,./:;<=>?#\^_`{|}~-]+$
This way you see what you going to match and what not. Please note that some characters are escaped by \ as without escaping they would have different meaning.

C# converting different String formats to date [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a WPF window which contains a textbox in which the user can enter a date in any format that he desires and i have to parse it in a datetime variable, simple enough? I tried various formats, some of them work with datatime.parse some don't, so what should i do about such strings?
some e.g. formats (year is not mandatory):
21 Sept
21st Sept
21 September
21st September
21 Sep
21/09
21-09
whatever catches the users fancy as long as it is a proper readable date, can anyone help me with this?
Update : based on comments, i am modifying the input string format....what if user can enter in any way but always day followed by month followed by year(optional).....is that manageable ??
Its not an answer but an Idea:
Why dont you use a Datepicker wo ou spare yourself alot of Job trying to convert every possible date to a valid one, and it looks nice too. Take alook at WPF Toolkit's Calender Datepicker Control
Its futile to do what you are doing.
A user can insert n number ways of adding date. How will you judge what format he has inserted.
Look for calendar controls because calendar is same everywhere and any user no matter where he is will insert correct date in it. In that case you can ask calendar for selecteddate or something kind of property.
There is no good answer to this problem, as best as I can tell.
DateTime.Parse still relies on the date being in a particular format. If you wanted - you could try multiple CultureInfos and hope that one of them works - but that would only work for a tiny subset of written dates real people would find acceptable.
Even if you wrote a lot of code to try and figure out all the possible ways regular people write dates - you'd still have globalization problems - some dates would be valid for interpretation in more than one way.
04/05 <-- What day is that?
Unless this is just for the sake of doing it; I think the best thing to do is simply provide a date-picker control or something similar that makes it easy for the user and gets you the date . Short of that - indicate the date format you expect.
DateTime.Parse works with the current CultureInfo unless you specify another info. Meaning:
21 Sept // invalid
21st Sept // invalid
21 September // acceptable by `en` CultureInfo
21st September // invalid
21 Sep // acceptable by `en` CultureInfo
21/09 // invalid by `en` CultureInfo (09/21 is valid)
21-09 // invalid by `en` CultureInfo (09-21 is valid)
But if you want a nice way for users to input a data consider using a control that will always catch a valid value instead of relying that a user will input the exact information you need manually.

What algorithm can break text up into its component words? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was pleasantly surprised to find how easy it is to use iTextSharp to extract the text from a pdf file. By following this article, I was able to get a pdf file converted to text with this simple code:
string pdfFilename = dlg.FileName;
// Show just the file name, without the path
string pdfFileNameOnly = System.IO.Path.GetFileName(pdfFilename);
lblFunnyMammalsFile.Content = pdfFileNameOnly;
string textFilename = String.Format(#"C:\Scrooge\McDuckbilledPlatypus\{0}.txt", pdfFileNameOnly);
PDFParser pdfParser = new PDFParser();
if (!pdfParser.ExtractText(pdfFilename, textFilename))
{
MessageBox.Show("there was a boo-boo");
}
The problem is that the text file generated contains text like this (i.e. it has no spaces):
IwaspleasantlysurprisedtofindhoweasyitistouseiTextSharptoextractthetextfromatextfile.
Is there an algorithm "out there" that will take text like that and make a best guess as to where the word breaks (AKA "spaces") should go?
Though I agree with Gavin that there's an easy way to solve this problem in this case but the problem itself is an interesting one.
This would require a heuristic algorithm to solve. I will just explain in a bit on why I think so. But first, I'll explain my algorithm.
Store all the dictionary words in a Trie. Now take a sentence, and look up in the trie to get to a word. The trie tracks the end of the word. Once you find a word, add a space to it in your sentence. This will work for your sentence. But consider these two examples:
He gave me this book
He told me a parable
For the first example, the above algorithm works fine but for the second example, the algorithm outputs:
He told me a par able
In order to avoid this, we will need to consider a longest match but if we do that then the output for the first example becomes:
He gave met his book.
So we are stuck and hence add heuristics to the algorithm that will be able to judge that grammatically He gave met his book doesn't make sense.

Password Policy Setting for a Website in c# 2008 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a web application in c# 2008.
I'm assigned a task to set the password policy for this web site.
The policy is
The 1st character is Upper Case
The 2nd character is lower case
The Character is a "special character"
The 4th through 8th character are random digits
The password is exactly 8 characters
The password should expire after 6 months
I'm not able to figure out this. Thanks in advance.
If you want to do it "right" and correct, go for Regular Expressions. If you don't have any experience with them, forget it if it's urgent.
Instead go with the quick and dirty way. This is untested pseudo-code:
if (password.Length == 8)
{
check password[0] for upper case
check password[1] for lower case
check password[2] for special char
check password[3] && password[7] for "random digits"
//return false, throw error, whatever you want in the case of any failures.
}
else
{
return error "your password is too short"
}
Not sure what you want to do for making the password expire in 6 months. If you are treating your password as a custom class with an "expiration date" field, and you just want 6 months from now, just use MyPassword.ExpirationDate = DateTime.Now.AddMonths(6);
Its not a good practice to ask it in here without trying anything. It sounds like you are trying to make your job done by others. I can suggest the way you should do instead of providing code.
You can do it by using regular expressions. You can search for it . There are many resources.You should constuct a regular expression that will check for the constraints you want except the password expiration. You should check password expiration on your database. You can define a job that will work every midnight , which will check the password database and detect the passwords that expires.

How do I capture named groups in C# .NET regex? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I'm trying to use named groups to parse a string.
An example input is:
exitcode: 0; session id is RDP-Tcp#2
and my attempted regex is:
("(exitCode)+(\s)*:(\s)*(?<exitCode>[^;]+)(\s)*;(\s)*(session id is)(\s)*(?<sessionID>[^;]*)(\s)*");
Where is my syntax wrong?
Thanks
In your example:
exitcode: 0; session id is RDP-Tcp#2
It does not end with a semi-colon, but it seems your regular expression expects a semi-colon to mark the end of sessionID:
(?<sessionID>[^;]*)
I notice that immediately following both your named groups, you have optional whitespace matches -- perhaps it would help to add whitespace into the character classes, like this:
(?<exitCode>[^;\s]+)
(?<sessionID>[^;\s]*)
Even better, split the string on the semi-colon first, and then perhaps you don't even need a regular expression. You'd have these two substrings after you split on the semi-colon, and the exitcode and sessionID happen to be on the ends of the strings, making it easy to parse them any number of ways:
exitcode: 0
session id is RDP-Tcp#2
Richard's answer really covers it already - either remove or make optional the semicolon at the end and it should work, and definitely consider putting whitespace in the negated classes or just splitting on semi-colon, but a little extra food for thought. :)
Don't bother with \s where it's not necessary - looks like your output is some form of log or something, so it should be more predictable, and if so something simpler can do:
exitcode: (?<exitCode>\d+);\s+session id is\s+(?<sessionID>[^;\s]*);?
For the splitting on semi-colon, you'll get an array of two objects - here's some pseudo-code, assuming exitcode is numeric and sessionid doesn't have spaces in:
splitresult = input.split('\s*;\s*')
exitCode = splitresult[0].match('\d+')
sessionId = splitresult[1].match('\S*$')
Depending on who will be maintaining the code, this might be considered more readable than the above expression.

Categories