phone number validation that accept only + and - signs - c#

i have to write the Regular expression that accept two signs only + and - and can be of any no. of digit but + should be the first sign if it is there

I am assuming the following:
+ may only be at the beginning of the number
- should not be
at the beginning or end of the number
consecutively repeated
You could try this regular expression:
^\+?(?:[\d]+\-)*[\d]+$
Passes the following string examples:
+12-34-5678-90
12-345-678-90
1234567890
Fails the following string examples:
++12-34-5678-90
12+34-5678-90
12-34-5678-90-
12--34-5678-90
EDIT
As #Alovchin pointed out, the OP expressed the need to allow for dots (.) as well in the numbers in one of the answer comments. Although this requirement does not reflect in the question I am going to add it here just-in-case.
If the requirement is to allow for dots (.) as well then the above regex will need to be updated to the following.
^\+?(?:[\d]+[\-\.])*[\d]+$
Hope this helps.

Try this one
^\+?[0-9-]+$
Check here This is for indian numbers.

In C# try this one:
^\+?(?![\-\.])[\d\-\.]+(?<![\-\.])$
There's a better answer by Tanzeel Kazi.

Related

Validating Positive number with comma and period

I need a regular expression validation expression that will
ALLOW
positive number(0-9)
, and .
DISALLOW
letter(a-z)
any other letter or symbol except . and ,
for example, on my asp.net text box, if I type anything#!#--, the regular expression validation will disallow it, if I type 10.000,50 or 10,000.50 it should allowed.
I've been trying to use this regex:
^\d+(\.\d\d)?$
but my textbox also must allow , symbol and I tried using only integer regex validation, it did disallow if I type string, but it also disallow . and , symbol while it should allow number(0-9) and also . and , symbol
Don't Use \d to match [0-9] in .NET
First off, in .NET, \d will match any digits in any script, such as:
654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖
So you really want to be using [0-9]
Incomplete Spec
You say you want to only allow "digits, commas and periods", but I don't think that's the whole spec. That would be ^[0-9,.]+$, and that would match
...,,,
See demo.
Tweaking the Spec
It's hard to guess what you really want to allow: would 10,1,1,1 be acceptable?
We could start with something like this, to get some fairly well-formed strings:
^(?:[0-9]+(?:[.,][0-9]+)?|[1-9][0-9]{0,2}(?:(?:\.[0-9]{3})*|(?:,[0-9]{3})*)(?:\.[0-9]+)?)$
Play with the demo, see what should and shouldn't match... When you are sure about the final spec, we can tweak the regex.
Sample Matches:
0
12
12.123
12,12
12,123,123
12,123,123.12456
12.125.457.22
Sample Non-Matches:
12,
123.
1,1,1,1
Your regex would be,
(?:\d|[,\.])+
OR
^(?:\d|[,\.])+$
It matches one or more numbers or , or . one or more times.
DEMO
Maybe you can use this one (starts with digit, ends with digit):
(\d+[\,\.])*\d+
If you need more sophisticated price Regex you should use:
(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)?
Edit: To make it more reliable (and dont get 00.50) you can add starting and ending symbol check:
(^|\s)(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)($|\s)?
I think the best regex for your condition will be :
^[\d]+(?:,\d+)*(?:\.\d+)?$
this will validate whatever you like
and at the same time:
not validate:
numbers ending in ,
numbers ending in .
numbers having . before comma
numbers having more than one decimal points
check out the demo here : http://regex101.com/r/zI0mJ4
Your format is a bit strange as it is not a standard format.
My first thought was to put a float instead of a string and put a Range validation attribute to avoid negative number.
But because of formatting, not sure it would work.
Another way is the regex, of course.
The one you propose means :
"some numbers then possibly a group formed by a dot and two numbers exactly".
This is not what you exepected.
Strictly fitted your example of a number lower than 100,000.99 one regex could be :
^[0-9]{1-2}[\.,][0-9]{3}([\.,][0-9]{1-2})?$
A more global regex, that accept all positive numbers is the one posted by Avinash Raj : (?:\d|[,\.])+

Should not allow single zero or 0.##

How do I restrict single zero in a numeric (Decimal may alow) textbox? Textbox can accept any number but it should not accept only zero or "0.##" as value.
For example: "900.55", "200.00" is valid but "0" and "0.105"is invalid.
I tried ^[1-9]\d\.?\d[0-9]* but it accepting the "0" and "0.##"
You're almost there.
^[1-9][0-9]*(\.[0-9]+)?$
The input must
start with a number 1-9
be followed by any sequence of 0-9
and optional:
a dot followed by one or more 0-9.
Notes:
This also disallows 3.. If you don't want that, replace the last + with a *.
You can use \d instead of [0-9]. I've used the latter to stay consistent with [1-9] and keep things simple.
I think you're close, but it may be easier to include the decimal and following digits in a group and make that entire group only allowable once like this one:
^[1-9][0-9]*(\.\d*)?
Also, here's a useful site for testing regular expressions.
I think it would be simpler to do this:
float result;
return float.TryParse(textBox1.Text, out result) && result < 1;
If the textbox only accepts valid numbers, and all you want to assert is that it is > 0. All you really need then is
^[1-9]
or if trailing prefix zero's are allowed
^0*[1-9]
You could alternatively write it using a negative lookahead:
^(?!0\b)\d+(\.\d*)?$
This has the added bonus of accepting numbers with a preceding 0 like 022.
I faced the same situation and solve this.
try out it.
#"[^0]*[0-9].\d{2}$

I need a regular expression to convert US tel number to link

Basically, the input field is just a string. People input their phone number in various formats. I need a regular expression to find and convert those numbers into links.
Input examples:
(201) 555-1212
(201)555-1212
201-555-1212
555-1212
Here's what I want:
(201) 555-1212 - Notice the space is gone
(201)555-1212
201-555-1212
555-1212
I know it should be more robust than just removing spaces, but it is for an internal web site that my employees will be accessing from their iPhone. So, I'm willing to "just get it working."
Here's what I have so far in C# (which should show you how little I know about regular expressions):
strchk = Regex.Replace(strchk, #"\b([\d{3}\-\d{4}|\d{3}\-\d{3}\-\d{4}|\(\d{3}\)\d{3}\-\d{4}])\b", "<a href='tel:$&'>$&</a>", RegexOptions.IgnoreCase);
Can anyone help me by fixing this or suggesting a better way to do this?
EDIT:
Thanks everyone. Here's what I've got so far:
strchk = Regex.Replace(strchk, #"\b(\d{3}[-\.\s]\d{3}[-\.\s]\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]\d{4}|\d{3}[-\.\s]\d{4})\b", "<a href='tel:$1'>$1</a>", RegexOptions.IgnoreCase);
It is picking up just about everything EXCEPT those with (nnn) area codes, with or without spaces between it and the 7 digit number. It does pick up the 7 digit number and link it that way. However, if the area code is specified it doesn't get matched. Any idea what I'm doing wrong?
Second Edit:
Got it working now. All I did was remove the \b from the start of the string.
Remove the [] and add \s* (zero or more whitespace characters) around each \-.
Also, you don't need to escape the -. (You can take out the \ from \-)
Explanation: [abcA-Z] is a character group, which matches a, b, c, or any character between A and Z.
It's not what you're trying to do.
Edits
In response to your updated regex:
Change [-\.\s] to [-\.\s]+ to match one or more of any of those characters (eg, a - with spaces around it)
The problem is that \b doesn't match the boundary between a space and a (.
Afaik, no phone enters the other characters, so why not replace [^0-9] with '' ?
Here's a regex I wrote for finding phone numbers:
(\+?\d[-\.\s]?)?(\(\d{3}\)\s?|\d{3}[-\.\s]?)\d{3}[-\.\s]?\d{4}
It's pretty flexible... allows a variety of formats.
Then, instead of killing yourself trying to replace it w/out spaces using a bunch of back references, instead pass the match to a function and just strip the spaces as you wanted.
C#/.net should have a method that allows a function as the replace argument...
Edit: They call it a `MatchEvaluator. That example uses a delegate, but I'm pretty sure you could use the slightly less verbose
(m) => m.Value.Replace(' ', '')
or something. working from memory here.

Only match regular expression in c# string

I am not too good with regular expressions so this might be an obvious question.
I want my expression to match if a certain number of characters are found and fail if any extra characters are present. For example if I have a string that should have 4 digits the following should be true.
1234 - match
ab1234cd - does not match
012345 - does not match
What I have so far is \d{4} but my understanding is that this would just match any string that has 4 digits together in it anywhere. I want to match only if a string contains 4 digits and nothing else.
Any help would be appreciated. Thanks.
Use ^ and $ to mark the start/end of the string.
Depending on how you are implementing it (single line mode or multiline mode) you can use something similar to:
^\d{4}$
To only match the (beginning of the string) four digits (end of string).
\b[0-9]{4}\b or ^\d{4}$ should both work. Maybe I could expand a little bit on what GrayWizardx said (just in case you do not use Regular Expressions in C# that much), the regular expressions provided above look for lines that have only 4 digits. By default (if memory serves me well), the regular expression engine looks at the first line only, so if you have a string made from more than 1 line and you would like to check the entire string (for instance, the string has been loaded from a file), you would add the option RegexOptions.MultiLine. in this way, the engine will take a look at the other lines as well.
Hope this has been helpful :)
I believe \b[0-9]{4}\b should do the trick.

.NET Regex, only numeric, no spaces

I'm looking for a regular expression that accepts only numerical values and no spaces.
I'm currently using:
^(0|[1-9][0-9]*)$
which works fine, but it accepts values that consist ONLY of spaces. What is wrong with it?
The reason why is that * will accept 0 or more. A purely empty string has 0 numbers and hence meets the requirements. You need 1 or more so use + instead.
^(0|[1-9][0-9]+)$
EDIT
Here is Andrews more robust and simpler solution.
^\d+$
this regex works perfecttly
^\d*[0-9](|.\d*[0-9])?$

Categories

Resources