Textboxvalidation: Empty or minimum 3 characters (spaces do not count) - asp.net

I have some textboxes I'm using as search-fields.
The textbox can be empty, but when a search-criteria is filled in, it must be at least 3 characters long, ignoring the spaces in the count.
I've found that a regularexpressionvalidator validates true when the textbox is empty, so that part is ok.
Q: regex for a minimumlenth of 3 characters. Spaces are allowed, but should not count in the length.
Thanks.

Have you tried something like this?
'(\s*\w\s*){3}'
This regular expression looks for a character (\w) optionally surronded by any whitespace (\s*) three times ({3}), which is what you're looking for.
Note: I don't know asp.net, but I think the regular expression is all you need to solve the problem.

Related

Allow Hyphens and apostraphes but no other special characters

I have the following validation expresion on an asp.net web form that allows alphanumeric characters, spaces,at least one alpha character, and a minimum of 3 characters and a maximum of 20:
ValidationExpression="(?!^[0-9]$)(?!^[a-zA-Z]$)^([a-zA-Z0-9 _]{3,20})$"
Now I have been asked to allow hyphens and apostraphes but no other special characters.
How can I implement this in my current validation?
This (?!^[0-9'-]$)(?!^[a-zA-Z'-]$)^([a-zA-Z0-9 _'-]{3,20})$?
Well, the main trick here is that - sign should be placed at the end of the character group for it to be parsed as a literal hyphen.
Try this:
(?=.*?[A-Za-z]+)^[a-zA-Z0-9_\-' ]{3,20}$

RegularExpression Validator For Textbox

In my requirement a Textbox should allow Alphabets,Numeric s, Special Characters,Special Symbols With at least one Alphabet.
I will try like this but i am not getting.
^\d*[a-zA-Z][a-zA-Z0-9#*,$._&% -!><^#]*$
You may want to have 2 regular expression validators; one for validating the allowed characters, and one for validating that at least on alphabet has been provided. You may be able to get at least one, but this way, you can have two separate validation messages to show the user explaining why the input is wrong.
Just match for special characters until you encounter a letter, then match for everything until the end of the string:
^[0-9#*,$._&% -!><^#]*[a-zA-Z0-9#*,$._&% -!><^#]*$
Use lookaheads :
/^(?=.*[a-zA-Z])[\w#*,$.&%!><^#-]*$/
Edit :
I assume the - is meant as the actual - character and not a range of space to !.
I removed the space character. You can of course add it if you want.
[ -!]
Effectively means :
[ -!] # Match a single character in the range between “ ” and “!”
And I have no idea what that range entails!

Number validation using a regular expression

In a web application, while validating the textbox using a regular expression, I have written the expression to validate only digits not starting with zero, with 3 digits after the decimal points. But if I type only a single digit, it's giving me a message. Can you help me with the regular expression? I'm looking for an expression which would not accept a leading digit of zero and accept only 3 decimals like 12.336, 1.254, 10.20, etc.
Depending on exactly what you want:
This will match numbers not begining with 0 and having exactly 3 decimal
^[1-9]\d*\.\d{3}$
This will match numbers not begining with 0 and having 1 to 3 decimal or none.
^[1-9]\d*(?:\.\d{1,3})?$
This should do the trick:
[1-9]\d*\.?\d{0,3}
If you wish to ignore whitespace, just add \s*:
\s*[1-9]\d*\.?\d{0,3}\s*
BTW, there are ton of visual tools for writing regular expressions – I recommend Expresso.
This is what you want:
^[1-9]\d*.?\d{0,3}$
Note this will also fail if there are spaces at either end of the string, remove the ^ at the start and the $ at the end if this is not as desired.

ASP.NET regular expression to restrict consecutive characters

Using ASP.NET syntax for the RegularExpressionValidator control, how do you specify restriction of two consecutive characters, say character 'x'?
You can provide a regex like the following:
(\\w)\\1+
(\\w) will match any word character, and \\1+ will match whatever character was matched with (\\w).
I do not have access to asp.net at the moment, but take this console app as an example:
Console.WriteLine(regex.IsMatch("hello") ? "Not valid" : "Valid"); // Hello contains to consecutive l:s, hence not valid
Console.WriteLine(regex.IsMatch("Bar") ? "Not valid" : "Valid"); // Bar does not contain any consecutive characters, so it's valid
Alexn is right, this is the way you match consecutive characters with a regex, i.e. (a)\1 matches aa.
However, I think this is a case of everything looking like a nail when you're holding a hammer. I would not use regex to validate this input. Rather, I suggest validating this in code (just looping through the string, comparing str[i] and str[i-1], checking for this condition).
This should work:
^((?<char>\w)(?!\k<char>))*$
It matches abc, but not abbc.
The key is to use so called "zero-width negative lookahead assertion" (syntax: (?! subexpression)).
Here we make sure that a group matched with (?<char>\w) is not followed by itself (expressed with (?!\k<char>)).
Note that \w can be replaced with any valid set of characters (\w does not match white-spaces characters).
You can also do it without named group (note that the referenced group has number 2):
^((\w)(?!\2))*$
And its important to start with ^ and end with $ to match the whole text.
If you want to only exclude text with consecutive x characters, you may use this
^((?<char>x)(?!\k<char>)|[^x\W])*$
or without backreferences
^(x(?!x)|[^x\W])*$
All syntax elements for .NET Framework Regular Expressions are explained here.
You can use a regex to validate what's wrong as well as what's right of course. The regex (.)\1 will match any two consecutive characters, so you can just reject any input that gives an IsValid result to that. If this is the only validation you need, I think this way is far easier than trying to come up with a regex to validate correct input instead.

Regular Expression to limit string length

I have an issue where I need to use a RegularExpressionValidator to limit the length of a string to 400 Characters.
My expression was .{0,400}
My question: Is there a way to limit the length of characters to 400 without taking into consideration blank spaces?
I want to be able to accept blank spaces in the string but not count it in the length. Is this possible?
I pretty much agree with Greg, but here's the regex you want:
^\s*([^\s]\s*){0,400}$
#Boopid: If you really meant only the space character, replace \s with a space in the regex.
It sounds like you might want to write your own validator class instead of using the RegularExpressionValidator. Regular expressions certainly have their uses, but this doesn't sound like one of them.
Your custom validator could remove all the spaces, then check the length of the string. Ultimately, the code will be more readable than a regular expression that does the same thing.

Resources