Can't detect '#' as special key (Regex Asp.net) - c#

I'm new on Asp. I have a problem to using regex for checking password input. Here the regex
<asp:RegularExpressionValidator ID="Regex1" runat="server"
ErrorMessage="Password must contain: Minimum 8 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet, 1 Number and 1 Special Character"
Font-Italic="True" Font-Size="Small" ForeColor="Red"
ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]{8,}"
ControlToValidate="TextBoxNewPassword" Display="Dynamic" />
When I input "Hamlida123#" regex did'nt allow it. How to solve this?

You need to include the '#` character specifically in the regex, like so:
ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&#])[A-Za-z\d$#$!%*?&#]{8,}"
Demo
Based on your current regex, I assume that you are only allowing certain non-word characters, and so you would need to list every allowable character in your regex as shown above.

Related

Combine 2 or more regex in 1

I've 2 regular expression:
string regex1 = "(?i)(^(?!^.*?admin)(?!^.*?admin[admin\d]).*$)"; this will check for 'admin' substring in the given string and case is insensitive.
string regex2 = "^[^<>?]{5,100}$"; this will check for special char(^<>?) and length between 5 to 100 only.
I want a regular expression where both the regex can be validated at once with the use of only single regex.
Ex-
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtBox1" ErrorMessage="Validation Failed!"
ValidationExpression="(?i)(^(?!^.*?admin)(?!^.*?admin[admin\d]).*$)">
</asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtBox2" ErrorMessage="Length Validation Failed!"
ValidationExpression="^[^<>?]{5,100}$">
</asp:RegularExpressionValidator>
Q. Can we have a single "RegularExpressionValidator" that serves both the above functionality?
The (?i)(^(?!^.*?admin)(?!^.*?admin[admin\d]).*$) regex is too redundant, it is equal to (?i)^(?!^.*?admin).*$. It basically matches any string that contains no admin substring.
The ^[^<>?]{5,100}$ regex disallows <, > and ? in the string and sets string length limit.
Combining the two is done by replacing the .* in the first pattern with the consuming part of the second regex ([^<>?]{5,100}):
(?i)^(?!^.*?admin)[^<>?]{5,100}$
Details
(?i) - case insensitive mode on
^ - start of string
(?!^.*?admin) - no admin substring allowed anywhere after 0 or more chars other than line break chars, as few as possible
[^<>?]{5,100} - five to a hundred chars other than <, > and ?
$ - end of string.

asp validation for multiple spaces between strings

I've a textbox for name field where I've used asp validation for proper name format. I want to validate multiple spaces between the strings. How can I do that? The leading and trail spaces are removed by trim() function but how can I validate multiple spaces between the strings? like
multiple spaces
no space
My validation code::
<label>
<span>Full name</span>
<input type="text" id="txt_name" runat="server" required="required"/>
<asp:RegularExpressionValidator ID="rev_txt_name" runat="server" ControlToValidate="txt_name" ForeColor="Red"
ErrorMessage="Invalid name!" SetFocusOnError="True" ValidationExpression="^[a-zA-Z'.\s]{2,50}"></asp:RegularExpressionValidator>
</label>
The pattern you are using allows matching whitespace anywhere inside the string and any occurrences, consecutive or not, since it is part of a rather generic character class. You need to use a grouping and quantify it accordingly:
^(?=.{2,50}$)[a-zA-Z'.]+(?:\s[a-zA-Z'.]+)*$
Note that the (?=.{2,50}$) lookahead requires the whole line to be of 2 to 50 chars long.
See the regex demo.
Details:
^ - start of string
(?=.{2,50}$) - a positive lookahead requiring any 2 to 50 chars other than a newline up to the end of the string
[a-zA-Z'.]+ - 1+ letters, single quote or dot chars
(?: - a non-capturing group start:
\s - 1 whitespace
[a-zA-Z'.]+ - 1+ letters, single quote or dot chars
)* - zero or more (*) occurrences
$ - end of string

Conditional Validation on a text field

I have a text field and I want to have a validation which verify if it starts with 0 the length will be 10, else will be 13.
I want to know if I can write in validation expression like "^0..." not in function.
I hope you can help me.
I code in ASP.NET, C#.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Text="dsg" ErrorMessage="RegularExpressionValidator" ValidationExpression="[^0]{0-10},[^1]{0-13}" ControlToValidate="TextBox1"></asp:RegularExpressionValidator>
This doesn't work , but I want something like this.
Your regex [^0]{0-10},[^1]{0-13} means:
[^0] - Match anything but 0 (even a comma)
{0-10}, - Match literal string {0-10}, (because of the hyphen inside {...})
[^1] - Match anything but 1
{0-13} - Match literal {0-13} string.
If you plan to use only server-side validation, you can use .NET regex to validate
if it starts with 0 the length will be 10 , else will be 13.
:
^(0)?(?(1)\d{9}|\d{13})$
See demo
In case you also plan to use it on the client side, you will need a JavaScript regex flavor where you cannot use conditionals:
^(?:0\d{9}|\d{13})$
See another demo
[^0]{0-10} means from 0 up to 10 characters not equal to 0. The ^ does not have the same meaning in the beginning of the regular expression and later.
A regular expression for a string of 10 characters starting with 0 is ^0.{9}$.
The other is ^[^0].{12}$.
So together: ^(0.{9}|[^0].{12})$

Need Regular Expression for Email Id with special characters .()<>\\" space ;

I need a Regular expression for Email Id that contains special characters like .;<>[]\" space and even # sign.It should not allow any multiple spaces in between. The username can't start with a space but it may contain a space inbetween like
"John "Smith\#yahoo.com
The Email Id can be like below
very.(),:;<>[]\".VERY.\"very#\\ \"very\".unusual"#strange.example.com
The Regex should work with C# like this
[RegularExpression("^[a-zA-Z0-9_\\.-]+#([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")] and with jQuery
Thanks in advance
Try This
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ErrorMessage="Please Enter Valid Email ID"
ValidationGroup="Submit" ControlToValidate="txtEmail"
CssClass="requiredFieldValidateStyle"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
Ok, I will post this, but you should bear in mind that this might return unexpected results when used without boundaries/anchors:
([-a-zA-Z0-9_\\:;,(). #"[\]<>]+)#((?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6})
Here is a demo.
This regex will work in both C# and JQuery since it does not use any named character classes, nor balancing groups, nor named capturing groups, etc.
Try this to validate mail:
^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
test regex: see demo

Regular Expression for email for a specific domain name

This is my code:
<asp:RegularExpressionValidator ID="regexEmail"
runat="server" ValidationExpression="(\w+#[test]+?\.[com]{3})"
ErrorMessage="Please Enter Valid Email Id"
ControlToValidate="txtEmailAddress" />
which is not working. But my problem does not end there. I would also like to know how to validate only characters in the username, and not numbers and special characters.
Please if anybody could help with this code. Thanks in Advance.
^[a-zA-Z]+#yourdomain\.com$
should do the trick
At first I think there is a miss understanding of the square brackets. With [com]{3} you are createing a character class and match 3 characters out of this class, that means this will match com (I think as you wanted), but also ccc, cmc and so on.
Similar for [test]+?, this matches at least 1 character from t, e and s
When you say:
validate only characters in the username, and not numbers and special
characters
I think you mean only letters? Or only ASCII letters?
What you meant is probably
(\w+#test\.com)
\w is a character class that contains A-Za-z0-9 and _ (and maybe anything thats a letter in unicode, I am not sure). If you want only ASCII characters then create your own character class with [A-Za-z], if you want to allow any letter use the Unicode property \p{L}
Something like this
#"^(\p{L}+#test\.com)$"
[test] is probably not what you want. It's equivalent to [tes] and means 't' or 'e' or 's'.
Did you try something like:
^[a-zA-Z]+#.*\.com$
This will validate to emails of the form xxx#xxx.com
As C# literal this is "^[a-zA-Z]+#.*\\.com$".
MSDN has an article about email validation.
http://msdn.microsoft.com/en-us/library/01escwtf.aspx
cheers

Categories

Resources