Validation Expression for asp.net - 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,}"

Related

ASP.NET "stacked" regular expression field validators

I have 2 ASP.NET regular expression validators:
^\s*[-+.'\w]+#\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$ check whether the email address is valid
#(adres.pl|vp.pl) check if value contains any of these strings
The problem now is:
The strings in bullet 2 above should NOT be allowed, so o email address "john#adres.pl" should not be allowed.
However, the validators are only positive, meaning that they check if they DO contain the structure.
So when someone currently enters "test#test.com", I get "invalid value"
My code below:
<asp:TextBox ID="tbEmail" runat="server" />
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="emailinvalid" ValidationExpression="^\s*[-+.'\w]+#\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$" ID="rev1" runat="server"/>
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="not allowed" ValidationExpression="#(adres.pl|vp.pl)" ID="rev2" runat="server"/>
I was initially thinking of combining expressions 1 and 2, but then I wouldn't know which one fails, and don't know which error to display.
What I would expect is:
"35435gd" -> emailinvalid
"john#adres.pl" -> not allowed
"john#gmail.com" -> all validators are ok
Or perhaps validator rev2 should only execute if rev1 does not throw an error, so "stacking" for a lack of a better term, which a: seems cumbersome and b: I wouldn't know how to do it.
How can I solve for this?
UPDATE 1
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="invalid" ValidationExpression="^\s*[-+.'\w]+#(?!(?:adres|vp)\.pl\b)\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$" ID="RegularExpressionValidator5" runat="server"/>
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="not allowed" ValidationExpression="#(?!(?:adres|vp)\.pl\b)" ID="RegularExpressionValidator4" runat="server"/>
"gddg" results in "not allowed", whereas I'd expect "invalid".
When I switch the order of these validators, both errors "invalid" and "not allowed" show.
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="invalid" ValidationExpression="^\s*[-+.'\w]+#(?!(?:adres|vp)\.pl\b)\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$" ID="RegularExpressionValidator5" runat="server"/>
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="not allowed" ValidationExpression="#(?!(?:adres|vp)\.pl\b)" ID="RegularExpressionValidator4" runat="server"/>
You can use a single pattern and you might write your existing pattern as:
^\s*[-+.'\w]+#(?!(?:adres|vp)\.pl\b)\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$
The part excluding either adres.pl or vp.pl can be written as #(?!(?:adres|vp)\.pl\b) excluding those matches directly after the #.
You could also use #(?!(?:adres|vp)\.pl\s*$) if that is the last part of the email address just like you currently do for this part \.(?!co\s*$)
See a regex demo.
Note that your pattern would allow leading and trailing whitespace chars for the e-mail address.

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.

regular expression allow the user to give few spcl char?

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\&\'\- ]+$

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?

In asp.net, I need to add validator to textbox that forces the input to be numbers

In asp.net, I need to add a validator to a textbox that forces the input to be numbers.
Is this built in?
I have already added a required field validator to the textbox.
You could use a Regex Validator to ensure the text is numeric
I think the regex would be
[0-9]*
e.g.
<asp:TextBox ID="tbxNumbers" runat="server" />
<asp:RegularExpressionValidator ID="revNumericValidator" runat="server"
ValidationExpression="^[0-9]*$" ControlToValidate="tbxNumbers" ErrorMessage="Must be Numeric" />
EDIT:
As the other two posters also pointed out you can also use \d to represent a Numeric Character
<asp:RegularExpressionValidator runat="server"
ControlToValidate="numbersOnlyTextBox"
ErrorMessage="Enter only numeric characters."
ValidationExpression="^\\d+$" />
Use a range validator.
<asp:TextBox ID="MyTextBox" MaxLength="4" Width="75"
Text="0" runat="server"></asp:TextBox>
<asp:RangeValidator ID="MyRangeValidator" Display="Static" Type="Integer"
MaximumValue="9999" MinimumValue="0" EnableClientScript="true"
ControlToValidate="MyTextBox" runat="server" SetFocusOnError="true"
ErrorMessage="Ooops"></asp:RangeValidator>
This permits you to use numbers with decimal places (by using Type="Double" or "Currency"), or other kinds of numbers that Windows recognizes.
Check MSDN for more info on the Range Validator Control.
I think there needs to be more clarification of the requirements here. What kind of numbers are we talking about? Positive integers? Any integer? A number with a decimal place? What about commas in the number (1,000)?
I recommend a RegularExpressionValidator to do your work, but these questions make a difference when it comes to which RegEx you use.
In order to provide a better user experience, another thing to add is an AjaxToolkit FilteredTextBox extender, with a FilterType of either "Custom, Numbers" or just "Numbers". The first choice is for when you want to be able to specify decimal points and negative numbers. In that case you must also specify the ValidChars attribute with something like "-.". This will stop a user from entering characters that are not going to make up a valid number such as -123.45 . Note that it does not stop the user from entering the '-' & '.' in incorrect places e.g. "2-..-3" can still be entered. You will need the validators mentioned in other answers to catch these cases.
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="numbersOnlyTextBox"
FilterType="Custom, Numbers"
ValidChars="-." />
Or
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="numbersOnlyTextBox"
FilterType="Numbers" />

Resources