String Length Validation

A

A.M

Hi,

How can I use validation controls to check max length of string text boxes?

Thanks,
Alan
 
N

Nicole Calinoiu

Alan,

Use a regular expression validator to test for up to <max length>
repetitions of whatever characters you are willing to permit. For example,
to allow between 1 and 50 word or white-space characters, you might use the
following regular expression: ^[\w\s]{1,50}$

HTH,
Nicole
 
N

Nicole Calinoiu

That will not prevent someone from submitting a longer string value from an
alternate UI. Server-side validation is a much more reliable way to ensure
that the submitted values meet expectations.
 
D

Darren Clark

Stop them from entering it in the first place..

try something like...

<asp:textbox id="SearchDescription" runat="server" CssClass="body_text" Rows="3" TextMode="MultiLine"
Width="173" onkeypress="return FilterMaxLength(event,this,60)" onpaste="return CheckPaste(this,60)"></asp:textbox>

function CheckPaste(o,max){
var maxLength = max;
// Get from clipboard
var data = window.clipboardData.getData("Text");

var CurrentLength = o.value.length + data.length;
if (CurrentLength > maxLength)
return false;
else
return true;
}

function FilterMaxLength(e,o,max){
// we must allow spaces , backspace etc to go through
var iKeyCode, strKey;

if (isIE) {
iKeyCode = e.keyCode;
} else {
iKeyCode = e.which;
}
strKey = String.fromCharCode(iKeyCode);
// Make sure we let special keys go through.
if (isSpecialKey(strKey))
return true;
var maxLength = max;
if(o.value.length > maxLength-1){
return false;
}
return true
}
 
D

Darren Clark

Yes it is more reliable...

BUT pain in the a&*^*&^ for a user having to wait for a page to reload each
time..


3 things to do ..
1) Stop the user from entering it in the first place
2) before submit ensure it is NOT over the max length
3) check on server.

To stop the client side dont rely on the MS validatoin... it doesnt work for
most of your browser base..

try something like this for client side stuff...

<asp:textbox id="SearchDescription" runat="server" CssClass="body_text"
Rows="3" TextMode="MultiLine"
Width="173" onkeypress="return FilterMaxLength(event,this,60)"
onpaste="return CheckPaste(this,60)"></asp:textbox>

function CheckPaste(o,max){
var maxLength = max;
// Get from clipboard
var data = window.clipboardData.getData("Text");

var CurrentLength = o.value.length + data.length;
if (CurrentLength > maxLength)
return false;
else
return true;
}

function FilterMaxLength(e,o,max){
// we must allow spaces , backspace etc to go through
var iKeyCode, strKey;

if (isIE) {
iKeyCode = e.keyCode;
} else {
iKeyCode = e.which;
}
strKey = String.fromCharCode(iKeyCode);
// Make sure we let special keys go through.
if (isSpecialKey(strKey))
return true;
var maxLength = max;
if(o.value.length > maxLength-1){
return false;
}
return true
}
 
S

Saravana [MVP]

Check out this article, this will help you.
http://www.extremeexperts.com/Net/Tools/MultiLineTextBox.aspx

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com



Stop them from entering it in the first place..

try something like...

<asp:textbox id="SearchDescription" runat="server" CssClass="body_text" Rows="3" TextMode="MultiLine"
Width="173" onkeypress="return FilterMaxLength(event,this,60)" onpaste="return CheckPaste(this,60)"></asp:textbox>

function CheckPaste(o,max){
var maxLength = max;
// Get from clipboard
var data = window.clipboardData.getData("Text");

var CurrentLength = o.value.length + data.length;
if (CurrentLength > maxLength)
return false;
else
return true;
}

function FilterMaxLength(e,o,max){
// we must allow spaces , backspace etc to go through
var iKeyCode, strKey;

if (isIE) {
iKeyCode = e.keyCode;
} else {
iKeyCode = e.which;
}
strKey = String.fromCharCode(iKeyCode);
// Make sure we let special keys go through.
if (isSpecialKey(strKey))
return true;
var maxLength = max;
if(o.value.length > maxLength-1){
return false;
}
return true
}
 
J

John Eikanger [MSFT]

Hi, Alan

Have you tried Nicole's suggestion of using Regular Expressions?

If that will not work for you, please expand on your scenario a bit so that
we understand your limitations.

Thank you for choosing the MSDN Managed Newsgroups,

John Eikanger
Microsoft Developer Support

This posting is provided “AS IS” with no warranties, and confers no rights.
--------------------
| From: "A.M" <[email protected]>
| Subject: Re: String Length Validation
| Date: Tue, 18 May 2004 21:43:53 -0400
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
|
| You can't relay on that because of security.
|
| "Swanand Mokashi" <swanandATswanandmokashiDOTcomNOSPAM> wrote in message
| | > use the maxLength property of the textbox?
| >
| > --
| > Swanand Mokashi
| > Microsoft Certified Solution Developer (.NET)
| > Microsoft Certified Application Developer (.NET)
| > http://www.swanandmokashi.com/
| > http://www.swanandmokashi.com/HomePage/WebServices/
| > Home of the Stock Quotes, Quote of the day and Horoscope web services
| > | > > Hi,
| > >
| > > How can I use validation controls to check max length of string text
| > boxes?
| > >
| > > Thanks,
| > > Alan
| > >
| > >
| >
| >
|
|
|
 
A

A.M

Thank you John for follow up.I was aware of regular exressions and I was
hoping that I can find a less sophesticated method for just checking string
length.
I seems that regular exressions is the only choice.

Thanks Again,
Alan
 
Joined
Aug 5, 2009
Messages
1
Reaction score
0
I think better is to use simple ^.{1,255}$ validation expression.
Why use ^[\w\s]{1,50}$ ???
It will give false for example when you enter dot "."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,364
Latest member
Stevanida

Latest Threads

Top