Validator control for text length

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

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

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

Thanx

Nimish.
 
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.length
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>ValidateLength</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function ValidateLength(oid, min, max)
{
var txt = document.getElementById(oid);
var length = txt.value.length;

alert("input's length is " + length);
if(length<min || length>max)
{
alert("Invalidated!");
}
else
{
alert("Validated!");
}
}
</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="ValidateLength('txtContent',4,8)" >(Length must
be limited&nbsp;between 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-us/cpguide/html/cpconregularexpressionv
alidatorcontrolsample.asp?frame=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
 
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="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<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?
 
Back
Top