I need to validate the scenario in Regex, I'm using RegularExpression validation in ASP.NET.
Shouldn't start or end with SPACE
Doesn't contain only SPACE
whole string shouldn't contain two special char "#" & "?"
Valid:
"as#d qwe2", "&^%$$(&+_", "12#$.p"
InValid:
" ", "asd ", " asd#", "ksdhf?kh", "asdf#asd"
I'm trying with this:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="RegularExpressionValidator"
ValidationExpression="^[^\s]+(\s+[^#?]+)*[^\s]$">Error</asp:RegularExpressionValidator>
Untested RegEx: ^([^ #?][^#?]*[^ #?]|[^ #?])$
There are a couple of problems with the regex you are currently using:
^[^\s]+ is matching almost all of the string if it does not start with a space character (thus matching # or ?).
Due to the way the regex is constructed, you can only input strings of length 2 and above. This is a minor setback, but can be avoided.
I would suggest using negative lookaheads since there are multiple 'checks' to do on the first character:
^(?!\s|.*\s$)[^?#]+$
(?!\s|.*\s$) will prevent a match if the string begins with \s or ends with \s and [^?#]+ matches all characters but ? and #. Space only strings will automatically be rejected because space only strings will have to begin with a space.
Related
Working on Ext.net project. I need to set a validation on password field for not allowing blank spaces before the password or after password and also the length of the password should not be more than 15 characters including blank spaces. I have done following so far but does not work.
The issue is it counts space between text as invalid.
E.g. It does not allow "Pass word", what I want to not allow " password" or "password ".
<ext:TextField ID="txtConfirmPwd" AllowBlank="false" InputType="Password" Name="txtConfirmPwd" runat="server" StyleSpec="width:96%;" Regex="^[^\s.^\s]{1,15}$" InvalidClass="invalidClass" Validator="ComparePwd" IDMode="Static">
<Listeners>
<Valid Handler="InvalidClass(this,true);" />
<Invalid Handler="InvalidClass(this,false);" />
</Listeners>
</ext:TextField>
You may use
Regex="^\S(?:.{0,13}\S)?$"
Details:
^ - start of string
\S - a nonwhitespace
(?:.{0,13}\S)? - 1 or 0 sequences of:
.{0,13} - any zero to thirteen chars
\S - a nonwhitespace symbol
$ - end of string.
This means, the first char must be a char other than whitespace and then there can be any up to 14 chars with the last one being a nonwhitespace.
You may actually use lookaheads to achieve the same, ^(?!\s)(?!.*\s$).{1,15}$. The (?!\s) is a negative lookahead that fails the match if the first (as the pattern is immediately following ^) char is a whitespace char and (?!.*\s$) fails the match if the whitespace appears right at the end of the string. However, it is unnecessarily complex for the current task.
As I put in the title I need a validator (regular expresion, customvalidator, etc) that allows the user to input everything (lettrs, special characters, spaces) but not numbers.
Valid string would be "D'Elia" or "Del Riego" Not Valid String would be " " or "p1" or "1" So I have to allow all letters, all special characters including space, no numbers, and not spaces at the beginning (well, al least that the field doesn't allow that only one space and nothing more can be written)
How can I do that?
Thank you,
Sabrina
You need to use a positive lookahead at the start to check for atleast one letter.
^(?=.*?[A-Za-z])[^0-9]+$
I want to allow only spaces, hyphens, underscores & commas along with alphabets & numbers. But this is not working as desired.
<asp:RegularExpressionValidator ControlToValidate="txtQuestion"
Display="dynamic" runat="server" ErrorMessage="*"
ValidationExpression="^[a-zA-Z0-9-_,\s]$"></asp:RegularExpressionValidator>
Character ShownResult
- valid
_ valid
, valid
-_ invalid //this should be valid for me
Put - symbol at the last or at the first inside the character class or consider escaping it otherwise it would act as a range operator.
^[a-zA-Z0-9_,\s-]+$
Add + after the character class to allow one or more characters otherwise it would allow only a single character from the list.
^[a-zA-Z0-9\-_,\s]*$
Escape - so that it does not define a range.Also use *or + to include one or more characters.
See demo.
http://regex101.com/r/hQ1rP0/21
If you want text field to except only Numbers and Char #%?* use following RE
ValidationExpression="[0-9#%?]"
This will work for any combination of characters
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.
I'm using the ASP Validation control a wanted to test that multiline textbox content was between 2 and 40 characters. Any character can be provided, including the new line character. What would be the expression? What I have below fails.
<asp:RegularExpressionValidator ID="RegularExpressionValidator"
runat="server"
ValidationExpression="^[.]{2,40}$"
ControlToValidate="CommentsTextBox"
ErrorMessage="The Comment field must be between 2 and 40 characters"
Display="Dynamic">*</asp:RegularExpressionValidator>
When you put the dot inside square brackets, it loses its special meaning and just matches a literal dot. The easiest way to match any character in an ASP RegularExpressionValidator is like this:
^[\s\S]{2,40}$
[\s\S] is the standard JavaScript idiom for matching anything including newlines (because JS doesn't support DOTALL or s-mode, which allows the dot to match newlines). It works the same in .NET, so you don't have to worry about whether the dot matches newlines, or whether the regex will be applied on the server or the client.
You're treating the period as a literal inside the brackets. You just want:
^(.|\n){2,40}$