Ext.net C# testfiled regex validation - asp.net

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.

Related

asp net regular expression for not allowing blank spaces

I want my validator to not allowing the user to enter more then 1 blank space since my application will crash. And a maximum of 25 characters, what is the regex for this?
<asp:RegularExpressionValidator ValidationGroup="grpSearch" ID="valSearch" ControlToValidate="txtSearchFor" ValidationExpression="^[a-zA-Z0-9][a-zA-Z0-9 ]+$" runat="server" ForeColor="Red"/>
The expression I have tried there does not work.
You may use
ValidationExpression="^(?!.{26})[a-zA-Z0-9]+( [a-zA-Z0-9]*)?$"
See the regex demo.
Details:
^ - start of string
(?!.{26}) - no 26 chars allowed (25 and fewer only)
[a-zA-Z0-9]+ - 1+ alphanumeric chars
( [a-zA-Z0-9]*)? - optional group matching a space and 0+ alphanumeric chars
$ - end of string
Just in case it is of interest: to disallow the space at the end of string, the * quantifier (zero or more occurrences) should be replaced with the + quantifier (one or more occurrences).
Try this:
^[^ ]+ [^ ]+$
This is of a similar theme, but asserts your requirement for alpha-numeric characters only:
^(A-Za-z0-9|[^ ])+ (A-Za-z0-9|[^ ])+$
Tested here: https://regex101.com/r/3w6B6h/3

Regular Expression validation in ASP.NET for special chars

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.

asp.net validator that allows everything but not numbers

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]+$

Allow certain special characters - Regular expression

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

Escape quote in web.config connection string

I have a connection string in my web config:
<add name="MyConString" connectionString="Server=dbsrv;User ID=myDbUser;Password=somepass"word" providerName="System.Data.SqlClient" />
As you see, there is a quotation sign ( " ) in the password (given from other dept. I can't change this db users password).
How do I have to escape the quote in this connection string?
Btw: I already tried & quot; in the string. That didn't work - ado.net got an ArgumenException then: "Format of the initialization string does not conform to specification starting at index 57."
57 is where the & quot; is in my connection string.
I also tried enclosing the password part in ' - didn't work either.
Also tried "" and \" - web.config can't be parsed then.
Thanks for the solution:
I had to combine the escaping of the double quote and putting the password in single quotes:
<add name="MyConString" connectionString="Server=dbsrv;User ID=myDbUser;Password='somepass"word'" providerName="System.Data.SqlClient" />
Use " instead of " to escape it.
web.config is an XML file so you should use XML escaping.
connectionString="Server=dbsrv;User ID=myDbUser;Password=somepass"word"
See this forum thread.
Update:
" should work, but as it doesn't, have you tried some of the other string escape sequences for .NET? \" and ""?
Update 2:
Try single quotes for the connectionString:
connectionString='Server=dbsrv;User ID=myDbUser;Password=somepass"word'
Or:
connectionString='Server=dbsrv;User ID=myDbUser;Password=somepass"word'
Update 3:
From MSDN (SqlConnection.ConnectionString Property):
To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks. If the value contains both a semicolon and a double-quote character, the value can be enclosed in single quotation marks.
So:
connectionString="Server=dbsrv;User ID=myDbUser;Password='somepass"word'"
The issue is not with web.config, but the format of the connection string. In a connection string, if you have a " in a value (of the key-value pair), you need to enclose the value in '. So, while Password=somepass"word does not work, Password='somepass"word' does.
connectionString="Server=dbsrv;User ID=myDbUser;Password=somepass"word"
Since the web.config is XML, you need to escape the five special characters:
& -> & ampersand, U+0026
< -> < left angle bracket, less-than sign, U+003C
> -> > right angle bracket, greater-than sign, U+003E
" -> " quotation mark, U+0022
&apos; -> ' apostrophe, U+0027
+ is not a problem, I suppose.
Duc Filan adds:
You should also wrap your password with single quote ':
connectionString="Server=dbsrv;User ID=myDbUser;Password='somepass"word'"
if " isn't working then try " instead.
Odeds answer is almost complete. Just one thing to add.
Escape xml special chars like Emanuele Greco said.
Put the password in single quotes like Oded said
(this one is new) Escape single ticks with another single tick (ref)
having this password="'; this sould be a valid connection string:
connectionString='Server=dbsrv;User ID=myDbUser;Password='"&&;'
Use " That should work.

Resources