.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Need a Function to be called from a custom validator to check the number of characters in a textbox

Posted By: Southend Supporter     Posted Date: March 02, 2010    Points:2   Category :ASP.Net
Hi
pretty new at ASP.NET, VB and web stuff in general and learning as I go!

I have a web page that is used to enter in a members details and I use validation controls to validate data is entered and is correct.
One of the fields is a textbox for a description of the business.

I am using a Custom Validation control to try and display a message if the description entered is too long ( I am using 10 as a test):
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Business Description Exceeds allowed characters" ControlToValidate="tbBusDesc2" Text="*" OnServerValidate="ValidateText10"></asp:CustomValidator>

I have the following code in my VB code behine page:
Protected Sub ValidateText10(ByVal sender As Object, ByVal args As ServerValidateEventArgs)

Dim numOfWords As String() = args.Value.Split(New Char() {" "c})
' args.IsValid = args.Value.Length <= 10
' args.IsValid = numofwords.Length <= 10
args.IsValid = args.Value.Length < 11

End Sub
The comments indicate the variations I have tried.
The validations work EXCEPT the length check :(

Any input welcomed.

Responses
Author: Syed Shakeer Hussain          Accepted Answer   
Posted Date: March 02, 2010     Points: 10   

Hi Sothend

The below code validate when user enterd 10 charaters.if the user enterd less than 10 (or) Greater than 10 characters it shows error Message:-

<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Business Description Exceeds allowed characters"
ControlToValidate="tbBusDesc2" Text="*"
ClientValidationFunction="ValidateText10 ">

</asp:CustomValidator>

<script type="text/javascript">
function ValidateText10 (oSrc,args)
{
args.IsValid = (args.Value == 10)
}
</script>


Author: Southend Supporter             
Posted Date: March 03, 2010     Points: 5   

Thanks Syad -- works a treat!

Just to be gredy! Can you tell me how to display the number of characters that have been entered as they are entered :)

____________________
Geoff
Author: Syed Shakeer Hussain             
Posted Date: March 03, 2010     Points: 5   

Hi Southend,

Below is the code to get the length of the string
<head runat="server">
<script type="text/javascript">
function GetTextLength (id)
{
var a=document.getElementById(id).value;
document.write (a.length);
}
</script>
</head>
<form id="form1" runat="server">
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<input type="button" value="Submit" onclick ="GetTextLength ('<%= txt1.ClientID %>')" />


Author: Southend Supporter             
Posted Date: March 03, 2010     Points: 5   

Hi Syed
Thanks for your input on displaying no of characters as they are typed.

BUT.... I Lied in response to your original post regarding simply restricting length!!
Actually, entering any number of charcaters into the textbox (even just 1) causes the error message to display?
I tried changing your script to:
<script type="text/javascript">
function ValidateText10 (oSrc,args)
{
args.IsValid = (args.Value <= 10)
}
</script>
to no avail. (Not a java programmer so this could be totally invaild syntax. Probably is as when I use it I get no display at all when I hit the submit button.!!!)

____________________
Geoff


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend