Regular Expression Validator -Not taking lower case alphabets - asp.net

I have a problem with Regular Expression Validator. It takes 0-9, A-Z and prevents ' and " But it not takes lowercase alphabets. Here is my expression ^[a-z|A-Z|0-9|]+[^\"\']*$

You should use:
^[a-zA-Z0-9]+$
The | , "OR", should be used in groups ([a-z]|[A-Z|..). Also, by adding [^"']*, you allow users to enter phrases like a ##%$^&&&&*&^&*#$##$ (starting with alphanumeric character, followed by any non-quote char).
My suggested RegEx means:
^ <start>
[a-zA-Z0-9] Any alplhanumeric character, case-insensitive
+ at least once
$ <end>

If all you need is that the text contain exclusively letters and digits, you could use:
/^[a-zA-Z0-9]+$/
Notice that there is no | in the character set, all the things you put inside the [] are implicitly "or"ed together. Adding a | would allow for a literal | in the text.
Change the + to * if an empty string is valid. You don't need to exclude quotes specifically, since you're not allowing them in at all.

Shorter version of similar regexp:
^[\w]+$
Note: it will also match non-Latin characters and "_", which may be either good or bad depending on your requirements.

Related

r gsub regex keep only [A-z0-9_] but ^ also remains [duplicate]

http://regexr.com/3ars8
^(?=.*[0-9])(?=.*[A-z])[0-9A-z-]{17}$
Should match "17 alphanumeric chars, hyphens allowed too, must include at least one letter and at least one number"
It'll correctly match:
ABCDF31U100027743
and correctly decline to match:
AB$DF31U100027743
(and almost any other non-alphanumeric char)
but will apparently allow:
AB^DF31U100027743
Because your character class [A-z] matches this symbol.
[A-z] matches [, \, ], ^, _, `, and the English letters.
Actually, it is a common mistake. You should use [a-zA-Z] instead to only allow English letters.
Here is a visualization from Expresso, showing what the range [A-z] actually covers:
So, this regex (with i option) won't capture your string.
^(?=.*[0-9])(?=.*[a-z])[0-9a-z-]{17}$
In my opinion, it is always safer to use Ignorecase option to avoid such an issue and shorten the regex.
regex uses ASCII printable characters from the space to the tilde range.
Whenever we use [A-z] token it matches the following table highlighted characters. If we use [ -~] token it matches starting from SPACE to tilde.
You're allowing A-z (capital 'A' through lower 'z'). You don't say what regex package you're using, but it's not necessarily clear that A-Z and a-z are contiguous; there could be other characters in between. Try this instead:
^(?=.*[0-9])(?=.*[A-Za-z])[0-9A-Za-z-]{17}$
It seems to meet your criteria for me in regexpal.

Asp.net RegEx Validation

I need to use an asp:RegularExpressionValidator control in my asp.net site and I want to restrict the characters to digits and semi-colon only.
So it needs to be able to take any amount of digits and ;
Valid values:
123
123;456
123;456;789;....
What is the regex for that?
Try this simple regex: [0-9;]+ or [\d;]+
\d match a digit [0-9]
; the literal character ;
+ means that it match between one character and unlimited times, as many times as possible,
If you want to guarantee that at least numbers are present in your expression you could do this too:
#npinti has a valid point better will be: ^[\d;]+$
where ^ indicates the begging of your expression and $ the end of it.
Online Demo

regex to allow alphanumeric characters on both sides of an equal sign?

I need a regular expression to validate whether text entered in an asp.net textbox has the following format
A-za-z123456789 /s = /s A-za-z123456789
Regular expression explained:
one or more alphanumeric characters
followed by any number of spaces
an equal sign
followed by any number of spaces
one or more alphanumeric characters
[a-zA-Z0-9]*\s*\=\s*[a-zA-Z0-9]*
Replace * with + if you want one or more rather than "any" (which includes zero)
Considering your answer to the comment about requiring one or more alphanumeric characters each side:
[a-zA-Z0-9]+\s*\=\s*[a-zA-Z0-9]+
This version will only match if there is at least one alphanumeric character each side of the "=".
If zero valid
"^[a-zA-Z\\d]+\\s*=\\s*[a-zA-Z\\d]+$"
If zero not valid
"^[a-zA-Z1-9]+\\s*=\\s*[a-zA-Z1-9]+$"

How to write a regex for any text except quotes or multiple hyphens?

Can anybody tell me how to write a regular expression for "no quotes (single or double) allowed and only single hyphens allowed"? For example, "good", 'good', good--looking are not allowed (but good-looking is).
I need put this regex like following:
<asp:RegularExpressionValidator ID="revProductName" runat="server"
ErrorMessage="Can not have " or '." Font-Size="Smaller"
ControlToValidate="txtProductName"
ValidationExpression="^[^'|\"]*$"></asp:RegularExpressionValidator>
The one I have is for double and single quotes. Now I need add multiple hyphens in there. I put like this "^[^'|\"|--]*$", but it is not working.
^(?:-(?!-)|[^'"-]++)*$
should do.
^ # Start of string
(?: # Either match...
-(?!-) # a hyphen, unless followed by another hyphen
| # or
[^'"-]++ # one or more characters except quotes/hyphen (possessive match)
)* # any number of times
$ # End of string
So, the regexp has to fail when ther is ', or ", or --.
So, the regexp should try this in every position, and if it's found, then fail:
^(?:(?!['"]|--).)*$
The idea is to consume all the line with ., but to check before using . each time that it not ', or ", or the beginning of --.
Also, I like the other answer very much. It uses a bit different approach. It consumes only non-'" symbols ([^'"]), and if it consumes -, it check if it's not followed by another -.
Also, there could be one more approach of searching for ', or ", or -- in the string, and then failing the regex if they are found. I could be achieved by using regex conditional expression. But this flavor of regex engine doesn't seem to support such kind of conditions.

Regular expression to match maximium of five words

I have a regular expression
^[a-zA-Z+#-.0-9]{1,5}$
which validates that the word contains alpha-numeric characters and few special characters and length should not be more than 5 characters.
How do I make this regular expression to accept a maximum of five words matching the above regular expression.
^[a-zA-Z+#\-.0-9]{1,5}(\s[a-zA-Z+#\-.0-9]{1,5}){0,4}$
Also, you could use for example [ ] instead of \s if you just want to accept space, not tab and newline. And you could write [ ]+ (or \s+) for any number of spaces (or whitespaces), not just one.
Edit: Removed the invalid solution and fixed the bug mentioned by unicornaddict.
I believe this may be what you're looking for. It forces at least one word of your desired pattern, then zero to four of the same, each preceded by one or more white-space characters:
^XX(\s+XX){0,4}$
where XX is your actual one-word regex.
It's separated into two distinct sections so that you're not required to have white-space at the end of the string. If you want to allow for such white-space, simply add \s* at that point. For example, allowing white-space both at start and end would be:
^\s*XX(\s+XX){0,4}\s*$
You regex has a small bug. It matches letters, digits, +, #, period but not hyphen and also all char between # and period. This is because hyphen in a char class when surrounded on both sides acts as a range meta char. To avoid this you'll have to escape the hyphen:
^[a-zA-Z+#\-.0-9]{1,5}$
Or put it at the beg/end of the char class, so that its treated literally:
^[-a-zA-Z+#-.0-9]{1,5}$
^[a-zA-Z+#.0-9-]{1,5}$
Now to match a max of 5 such words you can use:
^(?:[a-zA-Z+#\-.0-9]{1,5}\s+){1,5}$
EDIT: This solution has a severe limitation of matching only those input that end in white space!!! To overcome this limitation you can see the ans by Jakob.

Resources