regular expression allow the user to give few spcl char? - asp.net

I am givng a regular expression validation to a text box, which is not allowing the using to enter spcl. char. its allowing the user to enter only numbers or char.
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox><asp:RegularExpressionValidator ValidationExpression="^[0-9a-zA-Z ]+$"
ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"
ControlToValidate="TextBox2"></asp:RegularExpressionValidator>
I want to allow the user to give few spcl char like apostrophe ' ampersand & and hyphen - with numbers and char. means if the user wants to use these three spcl. then he/she can use, is this possible if yes then how?

Yes it is very possible. Just include the characters in your expression.
^[0-9a-zA-Z\&\'\- ]+$

Related

Enter/Return Key Triggers RegularExpressionValidator

I have a asp:Textbox that I am trying to make sure the user does not copy and paste in Tags. The below code works at checking for < and > but it also will throw the error message if a user hits the Enter or Return key to create a new line in the text box. What changes do I need to make to the ValidationExpression to allow the user to be able to use the Enter/Return keys, but not allow the < or > keys?
<asp:RegularExpressionValidator ID="RegularExpressionVal3" runat="server"
ErrorMessage="You cannot put tags into the text box (e.g. &lthtml&gt)"
ControlToValidate="txtMSPC"
ValidationExpression="^(?!<.*?>).*" ForeColor="Red" />
For the client-side validation only, you may use
ValidationExpression="^(?![\\s\\S]*<[^>]*>)[\\s\\S]*"
Details:
^ - start of string
(?![\\s\\S]*<[^>]*>) - no < followed with 0+ chars other than > and then > in the string
[\\s\\S]** - any 0+ chars

RegularExpressionValidator asp.net

I am trying to make sure that user only can input a-z and 0-9 characters.
To do this I have used the RegularExpressionValidator class:
<asp:TextBox ID="input" Text="search" runat="server" OnTextChanged="searchFunc"></asp:TextBox>
<asp:RegularExpressionValidator ID="regExp" runat="server"
ErrorMessage="only a-z or 0-9 allowed"
ControlToValidate="input"
ValidationExpression="^[a-z0-9]+$" />
Though in the code behind I am trying to check if the user entered valid input by using
regExp.IsValid
But this method returns True even if user inputs !&%()
I can't understand what I've done wrong. Is it my regular expression that is wrong?
Call regExp.IsValid only after the validation has been performed otherwise the default value is set to true.
Try calling regExp.Validate() before checking the IsValid property.

Multiline textbox new line character regularexpressionvalidator issues

I've been trying to validate a multiline textbox. And of course it doesn't work.
<asp:TextBox ID="t_noteTextBox" runat="server" Width="700" Text='<%# Bind("t_note") %>' TextMode="MultiLine" Rows="3" MaxLength="700" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Special Characters not allowed." ForeColor="Red"
ControlToValidate="t_noteTextBox" ValidationExpression="(?m)([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_]|[.]|[,]|[\r]|[\n])*" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator runat="server" ID="valInput" ControlToValidate="t_noteTextBox" ValidationExpression="^[\s\S]{0,740}$"
ErrorMessage="Please enter a maximum of 740 characters" Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator>
The expression works fine as long as I don't add a carriage return. As soon as I go to a new line i get an error.
I even looked up and found the ?m multiline option.
however that doesn't work either.
Testing the regex in Expresso works well.
(?m)([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_]|[.]|[,]|[\r]|[\n])*
But as soon as I put it on the website it doesn't.
Any ideas?
Multiline mode alters the behavior of the anchors (^ or $), which you aren't using.
Singleline mode causes the dot metacharacter to match everything including linefeeds, but you aren't using the dot that way. Like most metacharacters, the dot loses its special meaning inside a character class, so [.] just matches a literal ..
I suspect your problem is that you're allowing the validation to be done on the client side. That is, you haven't set EnableClientScript to "false". Client-side validation uses JavaScript instead of .NET, and the JavaScript regex flavor doesn't support inline modifiers like (?m) and (?s). But that shouldn't matter to you, since you aren't using the dot as a metacharacter.
This should be all you need:
ValidationExpression="[a-zA-Z0-9_.,\s-]*"
You can simplify this quite a bit.
(?m)([\w., -])*
The multiline flag (?m) only allows the anchors ^ and $ to match before and after linebreaks, as opposed to start and end of string normally, so it's not doing what you want here.
If you use \s instead of matching a space, it will match the newline as well.
([\w.,\s-])*

Validation Expression for asp.net

I need to validate textbox value for a password, on client-side.
I want to use RegularExpressionValidator.
Please provide me, the value for 'VALIDATION EXPRESSION'for following two conditions:-
"Password should contain minimum of 8 characters"
"Password should ahve atleast one non -alphanumeric character"
<asp:RegularExpressionValidator
ID="PasswordFormatValidator"
runat="server" Display="Dynamic"
ErrorMessage="Invalid Password Format"
ValidationExpression="??????????????"
ControlToValidate="txtEmail">Invalid Email Format
</asp:RegularExpressionValidator>
Or shall I use Custom Validator. If so, please provide the expression for the req condition.
Here is lots of information about this asp control which demonstrate about the regular expression and how setup ValidationExpression.
Visit MSDN:RegularExpressionValidator Control
for example:
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="TextBox1"
ValidationExpression="\d{5}"
Display="Static"
EnableClientScript="false"
ErrorMessage="Zip code must be 5 numeric digits"
runat="server"/>
check this also for more information:
Use Regular Expressions to Constrain Input in ASP.NET
Password
ValidationExpression="(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]"{8,10})$
Validates a strong password. It must be between 8 and 10 characters, contain at least one digit and one alphabetic character, and must not contain special characters.
if you just want to check length must be minimum.(atleast 1) can contain any value. you can replace 1 to check any minimum length of password.
ValidationExpression=".{1,}"

how to validate a filename using asp.net regular expression validator

i have the following code to validate my file name entered using regular expression validator
but even after enter correct file name format, its hitting error saying enter valid filename
<asp:TextBox ID="TxtFileName" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="FileNameRegularExpressionValidator" runat="server"
ErrorMessage="Enter valid FileName"
ControlToValidate="TxtFileName"
ValidationExpression="^(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$">
</asp:RegularExpressionValidator>
At the moment, your regex requires the filename to start with a backslash. Also, your filenames may only contain the lowercase form of letters. Is that intentional?
Also, you're repeating your repeated group, a surefire recipe to bring your server down to its knees with catastrophic backtracking once someone enters an invalid filename that's more than a few characters long.
Perhaps
ValidationExpression="(?i)^[\w\s0-9.-]+\.(txt|gif|pdf|doc|docx|xls|xlsx)$">
would be more suitable?

Resources