Validate first 2 characters of a textfield follwed by 4 int - c#

I need validation in a text box that when I enter some value in the text box, the first two characters should be characters only, then it allows only followed by 4 ints.
i wrote code like below:
if (Regex.IsMatch(Txtvalue.Text, "^[A-Za-z]{2}d{4}.+$"))
It shows error only. not validate properly.

You might want to use an editor (e.g. regex101 ) for regexes to debug them while writing.
One problem I see on your regex is that you are matching d literally.
I suppose you meant to write \d to match digits.
So ^[A-Za-z]{2}\d{4}.+$ should work.
Another thing I am suspecting is that you don't want the + quantifier as this will prevent ab1234 from being matched when it is not followed at least by a single character. To solve this use the * quantifier instead.

Related

How to make regex to enforce comma between every word?

I am writing a C# ASP.NET MVC application and I need to enforce some rules for user input.
In a form a user can write some incident case numbers which is on the form IRxxxxxx where x can be a number (0-9). I would like to make a regex that checks that the input can consist of either exactly one case number or multiple seperated by a commas (and some possible whitespace before and after the commas). I have tried a couple of things, but is not able to get it right.
Valid inputs could be:
IR123456
IR123456,IR123456,IR123456 (and so on)
IR123456, IR123456,IR123456 (notice the space after the first comma)
Invalid inputs could be:
IR123
IR1234567
ir123456
IR123456,,IR123456
This should work in most Regex engines:
^IR\d{6}(,\s?IR\d{6})*$
This looks for "IR" followed by any 6 digits. If there are additional IDs, this makes sure that there is a comma following the preceding one (with an optional space).
Regex101
You can try this regex: ^((^|,)\s*IR\d{6})+$

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|[,\.])+

Regular expression for adding special character to phone number

I have added the following regular expression for validating a mobile phone number:
(^07[1,2,3,4,5,7,8,9][0-9]{7,8}$)
I want to allow the user to enter a # character too and I'm not sure where to fit it in. They may need to enter # character after they have dialed a number, or at the beginning of a number to dial a direct number or an extension.
First, your current regex matches 'numbers' of the format 07,12345678 as well. So you need to change [1,2,3,4,5,7,8,9] to [1-9] (when you have a - between two characters in a character class, it usually means that there's a range)
If you want to accept an optional # character, you can use the ? quantifier which means 0 or 1 times.
^#?07[1-9][0-9]{7,8}#?$
regex101 demo
Except that, as you can see in the demo, it will also match numbers with two hashes; one at the front and one at the end. One option to circumvent this is to use some conditionals (which C# can support).
^(#)?07[1-9][0-9]{7,8}(?(1)|#?)$
regex101 demo
(?(1)|#?) basically means that if the first hash was matched, then nothing more should be matched. Otherwise, if no hash was initially matched, then it can match a hash, if there is one at the end of the number.
In C#, it will be a bit like this:
Regex.Match(myString, #"^(#)?07[1-9][0-9]{7,8}(?(1)|#?)$");
Or you could use a negative lookahead to make sure there's never more than one hash in the number:
^(?!.*#.*#.*$)#?07[1-9][0-9]{7,8}#?$

Regex length validation: Ignore leading and trailing whitespaces

I'm trying to limit our users between 5 and 1024 characters per post.
We are currently using asp.net RegularExpressionValidator for this.
This is kinda working if I set the expression to the following:
body.ValidationExpression = string.Format("^[\\s\\S]{{{0},{1}}}$",
MinimumBodyLength,
MaximumBodyLength);
However, this does not take NewLines and leading/tailing/multiple spaces into account. So users can type stuff like:
Ok (three spaces) . (dot), and it will still be validated because three spaces count as characters. The same goes for newlines. We can also type a single . (dot) followed by 5 spaces/newlines.
I have tried multiple regex variations I've found around the web, but none seem to fit my needs exactly. I want them to type minimum 5 characters, and maximum 3000 characters, but I don't really care how many newLines and spaces they use.
To clearify, I want people to be able to type:
Hi,
My name is ben
I do not want them to be able to type:
Hi .
or
A B
(lots of newlines or spaces)
It is possible that regex might not be the way to go? If so, how can I search and replace on the string before the regex evaluates (while still catch it in the validator with the old expression)?
Use the regex below:
body.ValidationExpression = string.Format("^((\S)|((\s+|\n+|(\r\n)+)+\S)|(\S(\s+|\n+|(\r\n)+))+){{{0},{1}}}$",
MinimumBodyLength,
MaximumBodyLength);
It treats as single entity either a single character or single character after (or before) any number of whitespace characters.
If I understood you problem, you want to count only word characters. If that's the point, you could try this:
body.ValidationExpression = string.Format("^\w{{{0},{1}}}$",
MinimumBodyLength,
MaximumBodyLength);

match any a-z/A-Z and - character after certain regular expression

i need a certain string to be in this format:
[0000] anyword
so between the [] brackets i need 4 numbers, followed by a whitespace. after that only characters ranging from a to z and - characters are allowed.
so this should be allowed:
[0000] foo-bar
[0000] foo
[0000] foo-bar-foo
etc..
so far i have this:
\[[0-9]{4}\]\s
this matches the [0000] , so it maches the brackets with 4 numbers in it and the whitespace.
i can't seem to find something that allows charachters after that. i've tried putting a single "." at the end of the expression as this should match any character but this doesnt seem to be working.
\[[0-9]{4}\]\s^[A-Z]+[a-zA-Z]*$
the above isn't working either..
i need this expression as a Validationexpression for an asp.net custom validator.
any help will be appreciated
(\[[0-9]{4}\])\s+([A-z\-]+) should hopefully work. It'll capture the numbers and letters into two capture groups as well.
This works for your input: http://regexr.com/?30sb7. Unlike Cornstalk's answer it does not capture anything, and - can indeed be placed later in a range if it's escaped.
Try this one
#"\[[0-9]{4}\] [a-zA-Z]+(-[a-zA-Z]+)*"

Categories

Resources