I would like to use validation to check the length of a textbox entry (character length) when the textbox loses focus. Does anyone have any suggestions on how I can do this?
Use the maxlength like the above post or you can use javascript and a customvalidator control to check the length.
Use a custom validator with ValidateTextLength as its clientvalidatefunction name
function ValidateTextLength(source, args)
{
var length = $get('<%=txtYourTextBox.ClientID %>').value.length;
//this would make it valid unless the length is greater than 10
if (length <= 10)
args.IsValid = true;
else
args.IsValid = false;
}
Ask Me About: Kentico, XHTML, CSS, Javascript, Asp.Net C#, SQL Server.
I am really looking for a way to check the length of the characters in the text box when the user click or tabs to the next text box. Is there a way to do this when the text box loses focus?
The Maxlength property does not seem to capture the actual length of the text in the text box, it will report the value assigned to the property. In the code below I am attempting to request the value of the length of the field, but what is reporte back
is the value of the proporty set for the control....
Protected Sub itemPlotSummaryTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(FormView1.FindControl("itemPlotSummaryTextBox"), TextBox)
Dim tooLong As Integer
tooLong = tb.MaxLength
If tooLong > 100 Then
MsgBox("The Plot Summary is too Long. The field is liimited to 100 characters, the field is currently" & tooLong & " characters in length.")
End If
End Sub
I am going to attempt to set the property back to zero (default) and see if the variable will return the actual number of characters entered in the text box.
Actually, I have everything working at this point with the code below, accept whent the focus is set back on the control, if the user does not make any changes to the text box it is not checked again.....
Protected Sub itemPlotSummaryTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(FormView1.FindControl("itemPlotSummaryTextBox"), TextBox)
Dim tooLong As Integer
tooLong = tb.Text.Length
If tooLong > 100 Then
MsgBox("The Plot Summary is too Long. The field is liimited to 100 characters, the field is currently " & tooLong & " characters in length.")
End If
SetFocus(FormView1.FindControl("itemPlotSummaryTextBox"))
End Sub
I guess the validator is the better option, I try and convert to that in VB though.
I am a bit miffed on the validator though. I'll post my progress shortly.
march11
Contributor
3001 Points
1361 Posts
how can I check the number of characters entered into textbox prior to DB update?
Nov 08, 2010 06:23 PM|LINK
I would like to use validation to check the length of a textbox entry (character length) when the textbox loses focus. Does anyone have any suggestions on how I can do this?
thanks,
MetalAsp.Net
All-Star
112151 Points
18246 Posts
Moderator
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 08, 2010 06:25 PM|LINK
I don't know if this addresses your question, but you can set the MaxLength property to limit the number of characters.
jiveabillion
Contributor
4222 Points
775 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 08, 2010 06:40 PM|LINK
Use the maxlength like the above post or you can use javascript and a customvalidator control to check the length.
Use a custom validator with ValidateTextLength as its clientvalidatefunction name
function ValidateTextLength(source, args) { var length = $get('<%=txtYourTextBox.ClientID %>').value.length; //this would make it valid unless the length is greater than 10 if (length <= 10) args.IsValid = true; else args.IsValid = false; }Learn the LifeCycle
Free Online Games
march11
Contributor
3001 Points
1361 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 10, 2010 01:11 PM|LINK
I am really looking for a way to check the length of the characters in the text box when the user click or tabs to the next text box. Is there a way to do this when the text box loses focus?
march11
Contributor
3001 Points
1361 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 10, 2010 01:33 PM|LINK
The Maxlength property does not seem to capture the actual length of the text in the text box, it will report the value assigned to the property. In the code below I am attempting to request the value of the length of the field, but what is reporte back is the value of the proporty set for the control....
Protected Sub itemPlotSummaryTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Dim tb As TextBox = DirectCast(FormView1.FindControl("itemPlotSummaryTextBox"), TextBox) Dim tooLong As Integer tooLong = tb.MaxLength If tooLong > 100 Then MsgBox("The Plot Summary is too Long. The field is liimited to 100 characters, the field is currently" & tooLong & " characters in length.") End If End SubI am going to attempt to set the property back to zero (default) and see if the variable will return the actual number of characters entered in the text box.
soloom
Member
108 Points
43 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 10, 2010 01:35 PM|LINK
apply regular expression
<asp:TextBox ID="txtName" runat="server"/> <asp:RegularExpressionValidator ID="regexpName" runat="server" ErrorMessage="This expression does not validate." ControlToValidate="txtName" ValidationExpression="^[a-zA-Z'.\s]{1,40}$" />Eslam
manoj0682
Star
8479 Points
1499 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 10, 2010 01:46 PM|LINK
a simple logic would be onblur event of that textbox. onblur event of textbox call an method name CheckTextboxLength().
function CheckTextBoxLength()
{
var len = document.getElementById(urTextboxId).value.length
//here u will get length of textbox in len variable
//do ur task here.
}
Manoj Karkera
march11
Contributor
3001 Points
1361 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 10, 2010 01:51 PM|LINK
Actually, I have everything working at this point with the code below, accept whent the focus is set back on the control, if the user does not make any changes to the text box it is not checked again.....
Protected Sub itemPlotSummaryTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Dim tb As TextBox = DirectCast(FormView1.FindControl("itemPlotSummaryTextBox"), TextBox) Dim tooLong As Integer tooLong = tb.Text.Length If tooLong > 100 Then MsgBox("The Plot Summary is too Long. The field is liimited to 100 characters, the field is currently " & tooLong & " characters in length.") End If SetFocus(FormView1.FindControl("itemPlotSummaryTextBox")) End SubI guess the validator is the better option, I try and convert to that in VB though.
I am a bit miffed on the validator though. I'll post my progress shortly.
march11
Contributor
3001 Points
1361 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 10, 2010 01:52 PM|LINK
soloom,
How can I check length with the ValidationExpression?
soloom
Member
108 Points
43 Posts
Re: how can I check the number of characters entered into textbox prior to DB update?
Nov 10, 2010 01:55 PM|LINK
{1,40}Eslam