473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

RegularExpressionValidator that limits the number of characters that can be entered

I have several TextBoxes with TextMode="MultiLine" in which I want to limit
the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not include
the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters to
be entered, but will limit the user to entering no more than 250 characters?
Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Jan 7 '07 #1
9 3907
Try

[a-zA-Z0-9\n]{0,250}
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I have several TextBoxes with TextMode="MultiLine" in which I want to limit
the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters
to be entered, but will limit the user to entering no more than 250
characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Jan 7 '07 #2
This does it just fine:

If TextBox1.Text.Length 250 Then
' Do Whatever
End If

It will recognize the \n char

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I have several TextBoxes with TextMode="MultiLine" in which I want to limit
the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters
to be entered, but will limit the user to entering no more than 250
characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Jan 7 '07 #3
That would determine whether it has more than 250 characters, but it is not
a Regular Expression. I want to determine whether it has too many characters
using a Validator control (other than a CustomValidator), and I am assuming
that the RegularExpressionValidator is the best choice for this. I want to
use a Validator control so that I can test the validity of my whole form
using the Page.IsValid property, and I don't want to use the CustomValidator
because it is more (and often less efficient if there is an alternative)
work to implement the ServerValidate event. Also, to have the validation
done before the postback, you must write a client-side script as well. If I
could find a ValidationExpression that worked (and I know that there is
one), it would save me a lot of work. Any ideas for a ValidationExpression?
Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Mudhead" <no*****@yourhouse.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
This does it just fine:

If TextBox1.Text.Length 250 Then
' Do Whatever
End If

It will recognize the \n char

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>I have several TextBoxes with TextMode="MultiLine" in which I want to
limit the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters
to be entered, but will limit the user to entering no more than 250
characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Jan 7 '07 #4
Two problems there:

1. It does not allow spaces
2. It still does not allow the user to enter MultiLine text into the TextBox

Using [.]{0,250} takes care of problem #1. However, I could be wrong (but
I'm not sure), but I don't think that \n is what we need to use to allow for
MultiLine input. However, I am not sure what we should be using, since the
documentation page I mentioned in my original posting says that . includes
all characters except \n. I noticed that on the following documentation
page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

The RegexOption member SingleLine has some way of changing the meaning of .
to include all characters. At the top of the documentation page it says
something about a way of specifying this option inline, but I was having
trouble figuring out how to do this and could not find any examples. If
anybody can help me with this, or let me know where I can see some examples
of the inline technique of specifying an option, I would appreciate it.
Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Just Me" <news.microsoft.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Try

[a-zA-Z0-9\n]{0,250}
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>I have several TextBoxes with TextMode="MultiLine" in which I want to
limit the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters
to be entered, but will limit the user to entering no more than 250
characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Jan 7 '07 #5
I found the following ValidationExpression on the Internet:

[\s\S]{0,250}

This seems to accept all characters, but it counts a newline as two
characters. For example, the text between the following lines:

-------------------------------------------
12345
67890
-------------------------------------------

Would count as 12 characters (5 + 2 + 5). This could end up being confusing
for users, especially if they are entering multiple lines. Also, since most
databases consider a newline one character, it is not the validation most
people want. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I have several TextBoxes with TextMode="MultiLine" in which I want to limit
the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters
to be entered, but will limit the user to entering no more than 250
characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Jan 8 '07 #6
It does not count a newline as 2 characters. It counts a newline as 1
character.

\s matches any white-space character and is equivalent to the Unicode
character classes [\f\n\r\t\v\x85\p{Z}].

\n matches a newline (\u000A)

\r matches a carriage return (\u000D)

Therefore, in your example, it counts one newline and one carriage return
which is absolutely correct because that is what is actually there.
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I found the following ValidationExpression on the Internet:

[\s\S]{0,250}

This seems to accept all characters, but it counts a newline as two
characters. For example, the text between the following lines:

-------------------------------------------
12345
67890
-------------------------------------------

Would count as 12 characters (5 + 2 + 5). This could end up being
confusing for users, especially if they are entering multiple lines. Also,
since most databases consider a newline one character, it is not the
validation most people want. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>I have several TextBoxes with TextMode="MultiLine" in which I want to
limit the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters
to be entered, but will limit the user to entering no more than 250
characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Jan 8 '07 #7
OK, my bad. But regardless of what the two characters are, it is more
characters than we want it to count. What I will be doing with the text
entered by the user is submitting it to an SQL Server database. The field it
is being used in is declared as:

otherinfo VARCHAR(250)

If the validator counts pressing enter as two characters, but the database
only counts it as one character, the user will not be able to enter as many
characters as the database field can actually hold. Is there any way around
this? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Stephany Young" <noone@localhostwrote in message
news:ea**************@TK2MSFTNGP03.phx.gbl...
It does not count a newline as 2 characters. It counts a newline as 1
character.

\s matches any white-space character and is equivalent to the Unicode
character classes [\f\n\r\t\v\x85\p{Z}].

\n matches a newline (\u000A)

\r matches a carriage return (\u000D)

Therefore, in your example, it counts one newline and one carriage return
which is absolutely correct because that is what is actually there.
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>>I found the following ValidationExpression on the Internet:

[\s\S]{0,250}

This seems to accept all characters, but it counts a newline as two
characters. For example, the text between the following lines:

-------------------------------------------
12345
67890
-------------------------------------------

Would count as 12 characters (5 + 2 + 5). This could end up being
confusing for users, especially if they are entering multiple lines.
Also, since most databases consider a newline one character, it is not
the validation most people want. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>>I have several TextBoxes with TextMode="MultiLine" in which I want to
limit the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all characters
to be entered, but will limit the user to entering no more than 250
characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



Jan 8 '07 #8
What makes you think that a varchar column makes any assumptions about what
you put into it apart from the length?
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:Ob*************@TK2MSFTNGP04.phx.gbl...
OK, my bad. But regardless of what the two characters are, it is more
characters than we want it to count. What I will be doing with the text
entered by the user is submitting it to an SQL Server database. The field
it is being used in is declared as:

otherinfo VARCHAR(250)

If the validator counts pressing enter as two characters, but the database
only counts it as one character, the user will not be able to enter as
many characters as the database field can actually hold. Is there any way
around this? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Stephany Young" <noone@localhostwrote in message
news:ea**************@TK2MSFTNGP03.phx.gbl...
>It does not count a newline as 2 characters. It counts a newline as 1
character.

\s matches any white-space character and is equivalent to the Unicode
character classes [\f\n\r\t\v\x85\p{Z}].

\n matches a newline (\u000A)

\r matches a carriage return (\u000D)

Therefore, in your example, it counts one newline and one carriage return
which is absolutely correct because that is what is actually there.
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>>>I found the following ValidationExpression on the Internet:

[\s\S]{0,250}

This seems to accept all characters, but it counts a newline as two
characters. For example, the text between the following lines:

-------------------------------------------
12345
67890
-------------------------------------------

Would count as 12 characters (5 + 2 + 5). This could end up being
confusing for users, especially if they are entering multiple lines.
Also, since most databases consider a newline one character, it is not
the validation most people want. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
I have several TextBoxes with TextMode="MultiLine" in which I want to
limit the number of characters that can be entered using a
RegularExpressionValidator. I have come up with the following
ValidationExpression:

[.]{0,250}

The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation page:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm

I tried using the following ValidationExpression:

[.\n]{0,250}

but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all
characters to be entered, but will limit the user to entering no more
than 250 characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



Jan 8 '07 #9
What I am saying is that if I have a column declared as:

testfield VARCHAR(22)

And the following ValidationExpression:

[\s\S]{0,22}

And the user enters:

first line
second line

The validator will see it as 23 characters, and the database will see it as
22. However, even though it would fit in the database column, the validator
will never let it get submitted. I need a way to make the database and the
validator see the input as the same length. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Stephany Young" <noone@localhostwrote in message
news:OR**************@TK2MSFTNGP02.phx.gbl...
What makes you think that a varchar column makes any assumptions about
what you put into it apart from the length?
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:Ob*************@TK2MSFTNGP04.phx.gbl...
>OK, my bad. But regardless of what the two characters are, it is more
characters than we want it to count. What I will be doing with the text
entered by the user is submitting it to an SQL Server database. The field
it is being used in is declared as:

otherinfo VARCHAR(250)

If the validator counts pressing enter as two characters, but the
database only counts it as one character, the user will not be able to
enter as many characters as the database field can actually hold. Is
there any way around this? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Stephany Young" <noone@localhostwrote in message
news:ea**************@TK2MSFTNGP03.phx.gbl...
>>It does not count a newline as 2 characters. It counts a newline as 1
character.

\s matches any white-space character and is equivalent to the Unicode
character classes [\f\n\r\t\v\x85\p{Z}].

\n matches a newline (\u000A)

\r matches a carriage return (\u000D)

Therefore, in your example, it counts one newline and one carriage
return which is absolutely correct because that is what is actually
there.
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl.. .
I found the following ValidationExpression on the Internet:

[\s\S]{0,250}

This seems to accept all characters, but it counts a newline as two
characters. For example, the text between the following lines:

-------------------------------------------
12345
67890
-------------------------------------------

Would count as 12 characters (5 + 2 + 5). This could end up being
confusing for users, especially if they are entering multiple lines.
Also, since most databases consider a newline one character, it is not
the validation most people want. Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl. ..
>I have several TextBoxes with TextMode="MultiLine" in which I want to
>limit the number of characters that can be entered using a
>RegularExpressionValidator. I have come up with the following
>ValidationExpression:
>
[.]{0,250}
>
The only problem with this ValidationExpression is that it does not
include the \n character as stated on the following documentation
page:
>
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/c82dc689-7e82-4767-a18d-cd24ce5f05e9.htm
>
I tried using the following ValidationExpression:
>
[.\n]{0,250}
>
but it did not work. What can I use as a ValidationExpression for a
RegularExpressionValidator in ASP.NET 2.0 that will allow all
characters to be entered, but will limit the user to entering no more
than 250 characters? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
>




Jan 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Eshtry | last post by:
I have a radio button list, a textbox representing SIN or EIN based on my radio button list selection so I put 2 regularexpressionvalidator to evaluate the value of textbox. EIN must be (for...
0
by: Matt Morris | last post by:
Hello: I have a RegularExpressionValidator control attached to a TextBox. I have the ValidationExpression property set to a very simple validation expression (\w+). The validation always fails...
3
by: tshad | last post by:
I have a RegularExpressionValidator that doesn't seem to work correctly if you don't enter anything. In the following, it works correctly if you have at least 1 character. If you just enter...
4
by: =?Utf-8?B?Y3VyaW91cw==?= | last post by:
I am using a RegularExpressionValidator to validate a TextBox. I use "^?+(\.*)?$" to check for a real number. The control works fine as long as the user enters something in the TextBox; it does not...
0
by: alastair.slater | last post by:
Hi all, Can anyone help please? I'm using the following code to validate a password entered by new users in an <asp:Textbox ... />: <asp:RegularExpressionValidator ID="RegularPassword"...
9
by: Nathan Sokalski | last post by:
I have several TextBoxes with TextMode="MultiLine" in which I want to limit the number of characters that can be entered using a RegularExpressionValidator. I have come up with the following...
4
by: howard.canaway | last post by:
I have always wondered about the specification page in the Access Help files. It reads Microsoft Access database (.mdb) file size 2 gigabytes. However, because your database can include linked...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.