473,748 Members | 6,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validator control for text length

Is there a asp.net validator control that validates the length of the text
being entered or does everyone just write jscript for this?

--
mo*******@nospa m.com
Nov 18 '05 #1
4 9908
You can make use of the RegularExpressi onValidator for this purpose

Secondly if you are using a TextBox you can set the MaxLength property

Thanx

Nimish.
Nov 18 '05 #2
Hi Moondaddy,

Thanks for posting here. Based on your description, you 're looking for
some approachs to validate the input fields(textbox) 's value's length on
ASP.NET web pages. You're wondering whether need to use a certain validator
control or just some clientside script block, yes?

As for this question, I think the clientside script is enougth if you don't
need very complex function. For example, we can get a textbox's value's
length via such script:
textbox.value.l ength
then, we can validate the value as what we want.
And here is an sample on using the javascript to retrieving text field
value's length and do validation on it:
-------------------------------aspx page---------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Validate Length</title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
<script language=javasc ript>
function ValidateLength( oid, min, max)
{
var txt = document.getEle mentById(oid);
var length = txt.value.lengt h;

alert("input's length is " + length);
if(length<min || length>max)
{
alert("Invalida ted!");
}
else
{
alert("Validate d!");
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><INPUT id="txtContent " type="text"></td>
<td><INPUT id="btnValidate " type="button" value="Validate "
onclick="Valida teLength('txtCo ntent',4,8)" >(Length must
be limited&nbsp;be tween 4----8)</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
------------------------------------------------------------------
NO additional code behind code needed

If you need complex functions on validating, you can also implement them
via javascript if you want to implement them at clientside. In addtion, the
ASP.NET's custom validator control also provide the functions which let you
use Regular expressions to customize your own validation rules. Here is the
related document on it:

#Regular Expression Validator Control Sample
http://msdn.microsoft.com/library/en...larexpressionv
alidatorcontrol sample.asp?fram e=true
Please check out the preceding suggestions to see whether they help. If
you have any further questions, please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Nov 18 '05 #3
Thanks. I tried this sample in a html page and all worked well. then I put
it in my aspx page and called a revised version of the function in an
asp.textbox ontextchaned event and the page errors when it loads saying that
it cant find the function. I put the function in a script file so it can be
used by other pages and here's how I referenced it in the page:
<HEAD>
<title>NewLogin </title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
<script src="Scripts/Main.js" type="text/javascript"></script>
</HEAD>

I also tried writing the function into some script in the body of page with
no success. Is there anyway I can get this to work by using calling a
function from a script file?

--
mo*******@nospa m.com
"Steven Cheng[MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:aO******** *******@cpmsftn gxa06.phx.gbl.. .
Hi Moondaddy,

Thanks for posting here. Based on your description, you 're looking for
some approachs to validate the input fields(textbox) 's value's length on
ASP.NET web pages. You're wondering whether need to use a certain validator control or just some clientside script block, yes?

As for this question, I think the clientside script is enougth if you don't need very complex function. For example, we can get a textbox's value's
length via such script:
textbox.value.l ength
then, we can validate the value as what we want.
And here is an sample on using the javascript to retrieving text field
value's length and do validation on it:
-------------------------------aspx page---------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Validate Length</title>
<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
<script language=javasc ript>
function ValidateLength( oid, min, max)
{
var txt = document.getEle mentById(oid);
var length = txt.value.lengt h;

alert("input's length is " + length);
if(length<min || length>max)
{
alert("Invalida ted!");
}
else
{
alert("Validate d!");
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><INPUT id="txtContent " type="text"></td>
<td><INPUT id="btnValidate " type="button" value="Validate "
onclick="Valida teLength('txtCo ntent',4,8)" >(Length must
be limited&nbsp;be tween 4----8)</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
------------------------------------------------------------------
NO additional code behind code needed

If you need complex functions on validating, you can also implement them
via javascript if you want to implement them at clientside. In addtion, the ASP.NET's custom validator control also provide the functions which let you use Regular expressions to customize your own validation rules. Here is the related document on it:

#Regular Expression Validator Control Sample
http://msdn.microsoft.com/library/en...larexpressionv alidatorcontrol sample.asp?fram e=true
Please check out the preceding suggestions to see whether they help. If
you have any further questions, please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #4
Hi Moddady,
Have you had a chance to try out my suggestions and sample? If you have any
questions, please feel free to post here.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #5

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

Similar topics

0
2233
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These controls and validators are then added to a table for display to the user. This code did seem to work originally, but now does not amd I am stumped as to why. There have been some code changes, but not to the methods below and I have pretty much...
8
2029
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty string (when you don't enter any values) - this is wrong d+ expression does not match, for example "g24" string - this is also wrong www.regexplib.com test validator works fine for both cases, i.e. it is reporting "not match" for the...
0
7949
by: Bryce Budd | last post by:
Hello All, I've been a taker of information from newsgroups for a long time and thought I'd finally make a contribution back to the community whose supported me when I've needed it. After all before commercialization took over that was the beauty of the Internet! I've create a checkboxlist validator control...something MS should have done originally in my opinion, but nonetheless here it is. It's a C# control, sort of IE specific. ...
2
3921
by: Pham Nguyen | last post by:
Has anyone seen an example of a textbox server control that has built-in client-side validation? I'd like to build a server control that extends the System.Web.UI.WebControls.TextBox class to allow javascript checks for things like valid e-mail addresses or phone numbers (without having to add a separate control for validation). One idea I did some work on was having the control implement the IValidator interface and basically recreating...
3
3231
by: RSB | last post by:
Hi Guys.. its me again.. ;o)) now i have a TextBox with properties Textmode = singleline and MaxLength = 100. So this works fine and i cannot enter more then 100 Chars. But as soon as i change the TextMode to MultiLIne the MaxLength gets ignore as it is painted as TEXTAREA. so how do i add a validator control to make sure that user is not entering more than 100 Characters.. Thank you for the help in advance.
10
5507
by: Rigs | last post by:
Hi, I have a textbox with a Custom Validator that utilizes the OnServerValidate method for that textbox. This works fine, however the method only executes when data exists in that textbox after the Submit button is clicked. If I click the submit button and no data exists in the textbox, the OnServerValidate method does not fire. I'd like the OnServerValidate method to either execute every time the Submit button is clicked. I am...
9
5166
by: Alex Shirley | last post by:
Hi there I’m simply trying to check for a blank or empty value in a textbox on my webform. In this instance I do not want to use a requiredfieldvalidator, I want to use a customvalidator (as I have other code within the customvalidator which works). This won’t work (within servervalidate): If txtBox.Text.ToString = "" Then raise exception…..
3
1615
by: Tumurbaatar S. | last post by:
I'm trying to dynamically hide/show a HTML table server control. My form contains one CheckBox control and its AutoPostBack set to true. The Checked value of this box controls whether to show or hide the table. Initially this CheckBox is not checked, so the table and all web control it contains is not rendered to a client. The table contains several TextBox controls and each TextBox has a pair of RequiredFieldValidator and...
3
1389
by: Howard | last post by:
I use a regex validator to check if a textbox is in email format. How do I make sure zero length input is not accepted? Do I need to use another requiredfield validator? or is check zero length built-in to the regex validator?
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.