Regular Expression to avoid space in asp.net textbox - asp.net

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).

Related

Regular Expression validation in ASP.NET for special chars

I need to validate the scenario in Regex, I'm using RegularExpression validation in ASP.NET.
Shouldn't start or end with SPACE
Doesn't contain only SPACE
whole string shouldn't contain two special char "#" & "?"
Valid:
"as#d qwe2", "&^%$$(&+_", "12#$.p"
InValid:
" ", "asd ", " asd#", "ksdhf?kh", "asdf#asd"
I'm trying with this:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="RegularExpressionValidator"
ValidationExpression="^[^\s]+(\s+[^#?]+)*[^\s]$">Error</asp:RegularExpressionValidator>
Untested RegEx: ^([^ #?][^#?]*[^ #?]|[^ #?])$
There are a couple of problems with the regex you are currently using:
^[^\s]+ is matching almost all of the string if it does not start with a space character (thus matching # or ?).
Due to the way the regex is constructed, you can only input strings of length 2 and above. This is a minor setback, but can be avoided.
I would suggest using negative lookaheads since there are multiple 'checks' to do on the first character:
^(?!\s|.*\s$)[^?#]+$
(?!\s|.*\s$) will prevent a match if the string begins with \s or ends with \s and [^?#]+ matches all characters but ? and #. Space only strings will automatically be rejected because space only strings will have to begin with a space.

Regarding Applying validation in textbox through Regular Expression

I am trying to apply validation in Textbox control for limiting empty space in control. Following is the Regular Expression code I'm using:
Regularexpression validationexpression="^[^-\s][a-zA-Z0-9_\s-]+$" errortext="" />
Now my requirement is:
User should not enter empty space in begining. (Working fine)
Textbox limit is upto 10 numbers, user will able to enter as much as number he wants, no validation if he enter less than 10 numbers. (Working fine.)
validation should prompt if user enter the numbers like this "111
111 ", means show validation if there is empty space between
numbers. (Not working)
Currently I'm using following Regular Expression to achieve this thing, please let me know or update my regular expression so I can achieve this requirement.
Regularexpression validationexpression="^[^-\s][a-zA-Z0-9_\s-]+$" errortext="" />
^[a-zA-Z0-9_-]{1,10}$
Try this.See demo.
http://regex101.com/r/wQ1oW3/23
you can use a regex like
^[^\s][\d\w_-]{1,10}$
which will match as
http://regex101.com/r/lK4pF7/1
how it works?
^ asserts the pattern at the begining of the string.
[^\s] negation of \s validates those without an empty string at the begining
[\d\w_-] ensures that body contains only alphanum, _, - no spaces.
{1,10} minimum 1 and maximum 10 mathes, ensures length not greater than 10
$ asserts the pattern at end of string

RegEx Max and Min length characters for a Text Box

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.

Text box validation using regular expression in asp.net

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 .

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