C# RegularExpressionValidator Trim and Count - c#

I have a textbox with a RegularExpressionValidator. I want to require the user to enter at least n characters. I'd also like to remove whitespace both at the start and end of the textbox. I'd still like to allow spaces within the textbox, I just want to remove the excess at the beginning and end.
I basically don't know how to combine the trim regex and the count together for use in a REV.
trim: ^\s*((?:[\S\s]*\S)?)\s*$
count: .{10}.*
I basically want to know if the input, after leading and trailing whitespace is removed, is greater than n characters.

You could use word boundaries to ignore the whitespace in the beginning, accept 10 characters, then end with another word boundary with a pattern like this:
\b.{10}\b
Be sure to also use a RequiredFieldValidator to cover empty inputs since the RegularExpressionValidator does not handle such cases.

Related

Underscore in regex not validating

How do I add underscore as a part of my regex string.
Here is my string that checks for uppercase, lowercase, numbers and special characters. The rest of the special characters work. Validation isn't working for underscores.
#"^[^\s](?=(.*[A-Za-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?=(.*[!##$%^&*()-+=\[{\]};:<>|_.\\/?,\-`'""~]{1,})).*[^\s]$"
Any ideas?
Thanks
This is the regex that AWS Cogito uses, it should apply to your situation:
#"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[\^$*.\[\]{}\(\)?\-“!##%&\/,><’:;|_~`])\S{8,99}$"
You can check regexes at http://regexstorm.net, it's faster than building your application everytime.
I've approached it like this: I took your requirements and made them into separate positive lookaheads:
Check for:
uppercase (?=.*[A-Z])
lowercase (?=.*[a-z]) (note that I broke A-Z and a-z up into separate groups)
numbers (?=.*\d)
special characters (?=.*[!##$%^&*()-+=\[{\]};:<>|_.\\/?,\-`'""~])
You can then combine them in any order and I've combined them in the same order as I listed them above and anchored it with the beginning of the line using ^. Don't add any extra matches before, in-between or after the groups in your requirement that could cause the regex to enforce a certain ordering of the groups:
The lookahead for any non-word character \W makes it impossible to match Underscore1_ since it will only match on "anything other than a letter, digit or underscore" - which is all Underscore1_ contains.
The starting [^\s] (and ending [^\s]) that consumes one character is likely destroying a lot of good matches. Underscore1_ or _1scoreUnder shouldn't matter, but if you start with _ and consume it with [^\s] like you do, the later lookahead for a special character will fail (unless you have a second special character in the password).
#"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!##$%^&*()-+=\[{\]};:<>|_.\\/?,\-`'""~])"
If you have a minimum length requirement of, say, 7 characters, you just have to add .{7,}$ to the end of the regex, making it:
#"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!##$%^&*()-+=\[{\]};:<>|_.\\/?,\-`'""~]).{7,}$"
Without a minimum length, a password of one character from each group will be enough, and since there are 4 groups, a password with only 4 characters will pass the filter.
I see no point in putting an upper length limit into the regex. If the user interface has accepted a string that is thousands of characters long, then why reject it for being too long later? The length of what you store is probably going to be much smaller anyway since you'll be storing the bcrypt/scrypt/argon2/... encoded password.
Suggestion: Also add space (or even whitespaces) to the list of special characters.
In you regexp add underscore in 3rd Capturing Group regex101
#"^[^\s](?=(.*[A-Za-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W_]){1,})(?=(.*[!##$%^&*()-+=\[{\]};:<>|_.\\/?,\-`'""~]{1,})).*[^\s]$"

Regex to match comma separated string with no comma at the end of the line

I am trying to write a regex that will allow input of all characters on the keyboard(even space) but will restrict the input of comma at the end of the line. I have tried do this,that includes all the possible characters,but it still does not give me the correct output:
[RegularExpression("^([a-zA-Z0-9\t\n ./<>?;:\"'!##$%^&*()[]{}_+=|\\-]+,)*[a-zA-Z0-9\t\n ./<>?;:\"'!##$%^&*()[]{}_+=|\\-]+$", ErrorMessage = "Comma is not allowed at the end of {0} ")]
^.*[^,]$
.* means all char,don't need so long
^([a-zA-Z0-9\t\n ./<>?;:\"'!##$%^&*()[]{}_+=|\\-]+,)*[a-zA-Z0-9\t\n ./<>?;:\"'!##$%^&*()[]{}_+=|\\-]+(?<!,)$
^^
Just add lookbehind at the end.
a regex that will allow input of all characters on the keyboard(even space) but will restrict the input of comma at the end of the line.
Mind that you can type much more than what you typed using a keyboard. Basically, you want to allow any character but a comma at the end of the line.
So,
(?!,).(?=\r\n|\z)
This regex is checking each line (because of the (?=\r\n|$) look-ahead), and the (?!,) look-ahead makes sure the last character (that we match using .) is not a comma. \z is an unambiguous string end anchor.
See regex demo
This will work even on a client side.
To also get the full line match, you can just add .* at the beginning of the pattern (as we are not using singleline flag, . does not match newline symbols):
.*(?!,).(?=\r\n|\z)
Or (making it faster with an atomic group or an inline multiline option with ^ start of line anchor, but will not work on the client side)
(?>.*)(?!,).(?=\r\n|\z)
(?m)^.*?(?!,).(?=\r\n|\z) // The fastest of the last three
See demo

Regular Expression to check the spaces and minimum entries in C#

I am using c# for programming!
I want to write one regular expression in c# which will check first and last space in a sentence and will allow spaces in between it as well as there should be minimumm 2 charater entry in field, no limit for maximum characters, no special keys are allowed (#,#,$ etc) characters allowed
Please suggests!
It's not really clear exactly what you want. Your comment -- contradicting the question itself -- suggests something like this, perhaps...
^[A-Za-z0-9]+(?:\s*[A-Za-z0-9]+)+$
This means that the string must start and end with an alphanumeric, and all characters except the first and last must be either alphanumeric or whitespace.

How do I ensure a text box is alphanumeric but without a leading digit?

My web application contains a text box for which I would like to restrict its input. I would like to prevent the user from entering text that:
Starts with white space
Starts with something other than a digit
Contains alphanumeric characters after the leading character.
Thank you for your suggestions!
Not to start with white space of alpha numeric: [a-zA-Z]+
Followed by 0 or more alphanumeric: [a-zA-Z0-9]*
Final expression
^[a-zA-Z]+[a-zA-Z0-9]*$
For ASCII characters you could use:
^[a-zA-Z][a-zA-Z0-9]*$ // Note you don't need the "+" after the first character group.
// or...
(?i:^[a-z][a-z0-9]*$) // Slightly shorter, albeit more unreadable, syntax (?i: ... ) makes the expression case-insensitive
If you want to match empty string just wrap the expression in "( ... )?", like so:
^([a-zA-Z][a-zA-Z0-9]*)?$
If you want to work in Unicode you might want to use:
^\p{L}[\p{L}\p{Nd}]*$
Unicode w. empty string:
^(\p{L}[\p{L}\p{Nd}]*)?$
To read more about unicode possibilities in regex, see this page on Regular-Expressions.info.
Edit
Just collected all possibilities in one answer.

How to supress whitespace regex and evenly space during Split

I'm trying to create a regex to tokenize a string. An example of the string is
ExeScript.ps1 $1, $0,%1, %3
Or it may be carelessly typed in as . ExeScript.ps1 $1, $0,%1, %3`
I use a simple regex string.
Regex RE = new Regex(#"[\s,\,]");
return (RE.Split(ActionItem));
I get a whole bunch of zeros between the script1.ps1 and the $1
When it is evenly spaced, as in first example I get no space between the script1.ps1 and the $1.
What am I doing wrong. How do I supress whitespace and ensure each array cell has a value in it which is now a whitespace.
Bob.
Try this regex:
Regex RE = new Regex(#"[\s,\,]+");
The + makes the delimiter "one or more" of the previous items. This may help, but it won't detect the situation of two commas (it would interpret it as one delimiter, which may not be what you want). Another possibility would be:
Regex RE = new Regex(#"\s*,\s*");
which is zero or more spaces, followed by a comma, followed by zero or more spaces.
You may also have to decide how you want to handle inputs such as:
foo, ,bar
which you might view as a list of three items, the second of which is a single space.

Categories