ASP.NET Regular Expression Validator - asp.net

I need a Regular Expression that can validate an exact 3 character(alpha only) code but also a blank field to set as the validation expression of a ASP.NET RegEx validator control.
I am currently using ^[a-zA-Z]{3}$
and this works out well to match the code but of course doesn't match a blank.
I have been looking at using something like this:
^(?:|)[a-zA-Z]{3}$

If your intention is to allow blank fields, then use the original pattern of ^[a-zA-Z]{3}$ since the RegularExpressionValidator doesn't validate blank fields. It will allow them.
However, if you want to prevent blank entries then you'll need to add a RequiredFieldValidator to validate the same control, in addition to the RegularExpressionValidator.

Have you tried using (^$)|(^[a-zA-Z]{3}$)?

Related

how to restrict a textbox to accept only two string i.e True/false (case insensitive) using regular expression in asp.net

I want to restrict a text box to accept only two strings i.e true/false using regular expression.
My code is working partially. For lower case, it's working fine but I want it to be case insensitive.
my regular expression looks like this
<asp:RegularExpressionValidator ID="regAssign1" runat="server"
ControlToValidate="tb_Assign" ErrorMessage="Wrong Input!" forecolor="Red"
ValidationExpression="^(true)|(false)$" ValidationGroup="Submit"></asp:RegularExpressionValidator>
I want to get this done using a regular expression validator only. Suggestions using any other options like use JavaScript or use a drop-down list should be avoided.
You should change your regex to:
"^(true|false)$"
Please note that when using that, editor still accept empty string so you need to add a RequiredFieldValidator as well. Also for both of them if you added :
Display="Dynamic"
it would look better.
The simple way is just like this...
^true$|^false$
This version allows whitespace before or after, which you can remove later with Trim.
^\s*true\s*$|^\s*false\s*$
I persistently tried to use ?i: for case insensitivity, it did not work in my RegularExpressionValidator. So here are some possible alternatives that will allow the three most likely case versions "true", "True", and "TRUE".
^true$|^false$|^True$|^False$|^TRUE$|^FALSE$
and allowing whitespace...
^\s*true\s*$|^\s*false\s*$|^\s*True\s*$|^\s*False\s*$|^\s*TRUE\s*$|^\s*FALSE\s*$
Also, as another answer pointed out, you will need a RequiredFieldValidator to disallow an empty TextBox.
Try this regex:
^(?i:true)|(?i:false)$
The i makes comparison case-insensitive for the words following the colon.
Alternatively, try this:
^([T|t][R|r][U|u][E|e])|([F|f][A|a][L|l][S|s][E|e])$

ASP.NET default regular expression for users' passwords

What is the default regular expression used by asp.net create user wizard?
According to the MSDN documentation, it should be something like this:
Regular Expression: #\"(?:.{7,})(?=(.*\d){1,})(?=(.*\W){1,})
Validation Message: Your password must be 7 characters long, and contain at least one number and one special character.
However, it does not work as it does not accept something like 3edc£edc, which is actually accepted when using the default create user wizard.
Any idea about how can I get this regular expression?
The error is in the ?: in (?:.{7,})(?=(.*\d){1,})(?=(.*\W){1,}) that is "consuming" the fist seven characters or more characters. It should be ?= OR you can invert the order: (?=(.*\d){1,})(?=(.*\W){1,})(?:.{7,})
Just change the order
^(?=(.*\d))(?=(.*\W)).{7,}
I additionally removed the {1,} and anchored it to the start of the string and you don't need a group around the last part
See it here on Regexr

disable characters from being entered in textbox

I am building a form and i want to prevent certain characters from being typed in a textbox?
I want to disable the " and ' characters in some of my textboxes
You can use a RegularExpressionValidator along with a regex such as [^"'] to allow all characters except " and '.
(please note, that regex is untested at the moment...)
ASP.NET Validators have both client and server APIs for validation.
The safest way is to check the form values on the server-side to see if the input is valid (doesn't include quote characters in your case) and respond with an error if it is invalid.
On the client side, I've had good luck using jQuery Validation.
Remember that arbitrary form payloads can be constructed outside of a browser, so you always have to check data validity on the server, even if client-side validation is in place.
Using an AJAX ASP FilterTextBox Control would be the way I'd go.
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="TARGETCONTROLIDHERE"
FilterType="Custom, Numbers, LowerCaseLetters, UpperCaseLetters, Symbols"
InvalidChars="&" />
You can simply remove those characters from the string before you perform your database operation.

Validation for TextBox For a User Form

In my User Registration Form, I want my user address text box to accpet alphanumeric as well as special character but should not contain only numeric or special character.
I am currently using regular expression validator ,what regular expression i can use.
Or Is there any different solution for that.
Regards
Here you go.. I have used it at one place, and works pretty well.. just provide your acceptable characters and you are good to go
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/FilteredTextBox/FilteredTextBox.aspx
You mean from your question that a string of alphabets is a must right ?

Test if the user has typed date format ASP.NET (VB)

I have two textbox called BIRTH & EMAIL, and I must test in button click if the user has typed a date & email format in those two TextBox.
How can do that ?
Take a look at DateTime.TryParse for the date.
For the e-mail, unfortunately using regular expressions is probably your best bet.
Use a RegularExpressionValidator for each field, in conjunction with a RequiredFieldValidator. There are some ready made regular expressions for both that can be accessed through the properties window of the control.
The ValidationExpression is where you set the regular expression.
Why dont you use the ASP .NET Validators?
http://support.microsoft.com/kb/316662
Seems like the RegularExpressionValidator along with the RequiredFieldValidator would be perfect for this.
Note: The Regular Expression Validator already has the email option in the pre populated list.
For the date field, I would use the date picker, or some other control that let the user enter the day, month, and year separately. That way you don't have to worry about ambiguous dates, such as 01/05/2010, or even worse 01/05/10 (is that y/m/d, d/m/y, m/d/y etc.), and other such problems. For email address, I would do a simple check for the "#" symbol, and a dot, but don't get any more stringent than that. Email addresses can be pretty weird. You can also make them type the email address twice to ensure that they typed it correctly.
As you're using VB.NET, I believe IsDate(dateValue) is a simple option to validate the date.
Regular Expressions are your best bet though.
There's a fairly long discussion on SO regarding email regexes here:
Using a regular expression to validate an email address

Resources