regular expression to check a symbol - asp.net

I have a text box to input.
<asp:TextBox ID="txt_namepair" runat="server" Text="<Name>=<Value>">
</asp:TextBox>
I want to add validation using regular expression validator. Now I need regular expression to check the input format as
"alpha numeric string = alphanumeric string"
like <name>=<value> pairs.
When I click on add button those = pairs will be displayed in a listbox.But what I wanted is, I want to check whether those , pairs have equal sign(=) between them by using regular expression. And and pairs should be alpha numeric characters.

You can use the following regular expression
^[a-zA-Z0-9 ]*=[a-zA-Z0-9 ]*$
to match your string
"alpha numeric string = alphanumeric string"
P.S. Do you really need spaces? If not delete the space from the regular expression after 9.
You can test the code I have provided at the following site
http://regexhero.net/tester/

Related

RegularExpressionValidator is not working in ASP.NET

My RegularExpressValidator1 does not fire. Not sure what is missing below. Any suggestion?
<td class="txtbox1"><asp:TextBox ID="txtCard" runat="server" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ControlToValidate="txtCard"
Forecolor="Red"
ValidationExpression="(0*[1-9]\d*)" runat="server"
ErrorMessage = "Enter numeric number only.">
</asp:RegularExpressionValidator>
</td>
Take a closer look at Microsoft's page How To: Use Regular Expressions to constrain input in ASP.Net.
Enclosing the expression in the caret (^) and dollar sign ($)markers ensures that the expression consists of the desired content and nothing else. A ^ matches the position at the beginning of the input string and a $ matches the position at the end of the input string. If you omit these markers, an attacker could affix malicious input to the beginning or end of valid content and bypass your filter.
If you scroll down to the section titled Common Regular Expressions, it includes a RegEx for positive integers:
^\d+$
Their pattern restricts the input to only numbers, whereas yours seems like it's only checking to see if a number is included in any fashion.
As an example, consider this input:
1234 and text!
Using ^\d+$ (Source)
0 matches
Using (0*[1-9]\d*) (Source)
1 match: 1234
As an unrelated note, I'd suggest modifying your placeholder text from "Enter a numeric number only." to something like "Enter a number" or "Enter a numeric value", as "numeric number" is a bit redundant!

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

Regular expression, required field only with alphanumeric

So how do I add that it should not have empty field in this same line including my other validation for alphanumeric reg ex.
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
I tried this and didn't work
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$ | /\S/
This is for validation controls
<asp:RegularExpressionValidator id="userLocationValidation" runat="server"
ControlToValidate="userLocation"
ValidationExpression="/\S/"
ErrorMessage="Only use letters from the english alphabet a-z">
</asp:RegularExpressionValidator>
Use:
^[A-Za-z0-9 _]+[A-Za-z0-9][A-Za-z0-9 _]*$
The '+' says one or more, so empty fields will fail.
^[a-zA-Z0-9 _]+$
+ means at least one
if you mean at least one not white-space character something like
^[a-zA-Z0-9 _]*[a-zA-Z0-9_]+[a-zA-Z0-9 _]*$

Date Regular Expression Validator

I have a regular expression validator on a text box to validate that the text entered is a valid date.
See reg ex below:
ValidationExpression="^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$"
Now I want to allow the following in the textbox: mm/dd/yyyy
How can I update my regex so that if mm/dd/yyyy is entered it does not throw a validation error?
Thanks in advance.
ValidationExpression="^[0-9m]{1,2}/[0-9d]{1,2}/[0-9y]{4}$"
Basically allows 0-9 or m in the first field, 0-9 or d in the second, 0-9 or y in the third (in regular expression [] brackets contain a list of possible options, - denote ranges of values when placed within brackets).
This is a more accurate way to restrict the Date to a more meaningful format
^[1-12]{1,2}/[1-31]{1,2}/[2000-2050,1900-1999]{4}$
This is still not perfect as this will allow - for instance - a date 02/31/2013.
Just realized this is quiet buggy.

Regular Expression to test for the number of any characters in a range

I'm using the ASP Validation control a wanted to test that multiline textbox content was between 2 and 40 characters. Any character can be provided, including the new line character. What would be the expression? What I have below fails.
<asp:RegularExpressionValidator ID="RegularExpressionValidator"
runat="server"
ValidationExpression="^[.]{2,40}$"
ControlToValidate="CommentsTextBox"
ErrorMessage="The Comment field must be between 2 and 40 characters"
Display="Dynamic">*</asp:RegularExpressionValidator>
When you put the dot inside square brackets, it loses its special meaning and just matches a literal dot. The easiest way to match any character in an ASP RegularExpressionValidator is like this:
^[\s\S]{2,40}$
[\s\S] is the standard JavaScript idiom for matching anything including newlines (because JS doesn't support DOTALL or s-mode, which allows the dot to match newlines). It works the same in .NET, so you don't have to worry about whether the dot matches newlines, or whether the regex will be applied on the server or the client.
You're treating the period as a literal inside the brackets. You just want:
^(.|\n){2,40}$

Resources