I use Asp.net and C#.
I need force the User to add in a TextBox Control only between 4 and 128 characters text.
I would like to use a ValidationExpression Property for a Validation Control.
Could you point me out a correct Regular Expression?
Notes: I'm using this code right now, but it seems not working properly if there are double spaces or break line in the TextBox
ValidationExpression="^.{4,128}$"
Thanks for your time on this!
Your expression is correct. Just use the Singleline modifier, to make the dot also match newline characters.
RegexOptions.Singleline
Or as inline modifier
"^(?s)(.){4,128}$"
RegexOptions Enumeration
Regular Expression Options
The full stop or period character (.) is known as dot. It is a wildcard that will match any character except a new line (\n).
Reference: http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx
Try this instead:
ValidationExpression = "^(.|\n|\t){4,128}$"
I added tabs (\t) as well.
Tell me if it worked or not!
try this ValidationExpression = ^(\w*)(\s*)(.*){4,128}$" it will cover periods and spaces as well.
Related
I have lot of aspx pages with textboxes and I am using VB.NET.
<asp:TextBox runat="server" ID="txtADHD" MaxLength="6"></asp:TextBox>
I am using an regular expression validator
ValidationExpression="^\d+$"
This only helps me when there is space between two numbers and not when there is just space.
(1 space 1) it regular expression is validated.
Space and then I enter 1 the regular expression is not fired.
Is there an easy way to avoid space or modify this regular expression?
If you only care about spaces, then you can use the String.Replace() method, like this:
Dim replacedString As String = txtADHD.Text.Replace(" ", String.Empty)
Note: This will not take out new lines, tabs, etc., but if you have single line text, then that should not be an issue.
"What's the reqular expression to check there is no white space in a string", the following pattern will work:
ValidationExpression="^[\S]*$"
This will find any string that only contains non-white space (spaces, new lines, tabs, etc).
What is the Regular Expression Validator for only Letters and Numbers in asp.net?
I need to enter only 0-9,a-z and A-Z. I don't want to allow any special characters single or double quotes etc. I am using asp.net 3.5 framework.
I tried ^[a-zA-Z0-9]+$ and ^[a-zA-Z0-9]*$. They are not working.
Any help will be appreciated.
Try the following.
^[a-zA-Z0-9]+$
go to this example and also alphanumerics for more
then try this
^[a-zA-Z0-9]*$
If length restriction is necessary use
^[a-zA-Z0-9]{0,50}$
This will match alphanumeric strings of 0 to 50 chars.
you can try this....
^[a-zA-Z0-9]+$
see more info at here
You can define a regular expression as follows,
Regex myRegularExpression = new Regex(" \b^[a-zA-Z0-9]+$\b");
be sure to include System.Text.RegularExpression
and then use the Regex to match it with your user-control as follows,
eg : if your user-control is a textbox
myRegularExpression.isMatch(myTextBox.Text);
Dear English speaking people. With all due respect. A-Z are not the only letters in the world. Please use \w instead of [A-Za-z0-9] if you support other languages in your apps
I have a textbox and have to use the regular expressions in asp.net
My text should not allow the spaces in first and last place.
Output should be:
Valid:
[India Bangalore]
Not valid:
[ India Bangalore ]
i.e : user can enter the spaces in between the words but not in first position and last position.
If you have solution in JavaScript that is also fine.
Trim() should remove any trailing or leading spaces.
Try this please :
^[^\s].*[^\s]$
It simply match input which:
not to start with any white space ^[^\s] followed by any character even white spaces .* and not end with any white space [^\s]$.
Any way calling Trim() method on input string in server-side is much easy .
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}$
I have an ASP.NET website with a regular expression validator text box.
I have changed the expression in the regular expression validation property "validator expression" and after compiling (rebuild) and running, the validation CHANGEs are not reflecting.
The previous validation is working fine but the changed validation is not working.
Please help me!
edit:
First code:
([a-zA-Z0-9_-.]+)\#((base.co.uk)|(base.com)|(group.com))
Second code:
#"([a-zA-Z0-9_\-.]+)#((base\.co\.uk)|(base\.com)|(group\.com)|(arg\.co\.uk)|(arggroup\.com))"
Assuming your "first code" is a literal regex, you need to escape the hyphen in the character class with a backslash.
Your "second code" is a regex formatted as a C# verbatim string that will match an email address such as whatever#base.co.uk just fine. There is nothing wrong with this regex.
You'll have to post the code in which you are using this regex if it doesn't work the way you want.
In this part: [a-zA-Z0-9_\-.] you are escaping the hyphen. The proper way to put a hyphen in a regex character class is in first position (or it thinks it is part of a range):
[-a-zA-Z0-9_.]
Then you removed the backslash from before the #. In Perl the # would be taken as part of a list name, but in C# I am not sure what effect it would have to not escape it.
The escaping of the periods is also suspect. You might need to double them up: e.g. \\. Instead, what I would do for a period is use a character class: i.e. [.] Inside the character class the period loses its special meaning.
Try this:
#"([-a-zA-Z0-9_.]+)\#((base[.]co[.]uk)|(base[.]com)|(group[.]com)|(arg[.]co[.]uk)|(arggroup[.]com))"