Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > RegularExpressionValidator that limits the number of characters that can be entered

Reply
Thread Tools

RegularExpressionValidator that limits the number of characters that can be entered

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      01-07-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Just Me
Guest
Posts: n/a
 
      01-07-2007
Try

[a-zA-Z0-9\n]{0,250}


"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
 
 
 
Mudhead
Guest
Posts: n/a
 
      01-07-2007
This does it just fine:

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

It will recognize the \n char

"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      01-07-2007
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Mudhead" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> This does it just fine:
>
> If TextBox1.Text.Length > 250 Then
> ' Do Whatever
> End If
>
> It will recognize the \n char
>
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>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
>> (E-Mail Removed)
>> http://www.nathansokalski.com/
>>

>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      01-07-2007
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Just Me" <news.microsoft.com> wrote in message
news:%(E-Mail Removed)...
> Try
>
> [a-zA-Z0-9\n]{0,250}
>
>
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>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
>> (E-Mail Removed)
>> http://www.nathansokalski.com/
>>

>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      01-08-2007
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      01-08-2007
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>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
>> (E-Mail Removed)
>> http://www.nathansokalski.com/
>>

>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      01-08-2007
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Stephany Young" <noone@localhost> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>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
>> (E-Mail Removed)
>> http://www.nathansokalski.com/
>>
>> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>>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
>>> (E-Mail Removed)
>>> http://www.nathansokalski.com/
>>>

>>
>>

>
>



 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      01-08-2007
What makes you think that a varchar column makes any assumptions about what
you put into it apart from the length?


"Nathan Sokalski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
> "Stephany Young" <noone@localhost> wrote in message
> news:(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>>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
>>> (E-Mail Removed)
>>> http://www.nathansokalski.com/
>>>
>>> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
>>> news:%(E-Mail Removed)...
>>>>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
>>>> (E-Mail Removed)
>>>> http://www.nathansokalski.com/
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      01-08-2007
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Stephany Young" <noone@localhost> wrote in message
news:(E-Mail Removed)...
> What makes you think that a varchar column makes any assumptions about
> what you put into it apart from the length?
>
>
> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>> (E-Mail Removed)
>> http://www.nathansokalski.com/
>>
>> "Stephany Young" <noone@localhost> wrote in message
>> news:(E-Mail Removed)...
>>> 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" <(E-Mail Removed)> wrote in message
>>> news:%(E-Mail Removed)...
>>>>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
>>>> (E-Mail Removed)
>>>> http://www.nathansokalski.com/
>>>>
>>>> "Nathan Sokalski" <(E-Mail Removed)> wrote in message
>>>> news:%(E-Mail Removed)...
>>>>>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
>>>>> (E-Mail Removed)
>>>>> http://www.nathansokalski.com/
>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
RegularExpressionValidator that limits the number of characters that can be entered Nathan Sokalski ASP .Net 9 01-08-2007 06:36 AM
Feedback The Number of Characters Entered Into Textarea? clintonG ASP .Net 4 10-17-2005 02:47 PM
Date entered from textbox becomes null (1/1/1900) when entered into SQL table. TN Bella ASP .Net 1 07-01-2004 02:53 PM
Interrogating string for number of characters, response.writing identical number of characters on new line Ken Fine ASP General 2 02-05-2004 03:40 AM



Advertisments