Guía de programación para insertar cuadro de texto en

Right now I am creating one registration form. In which I want to include one functionality of limited characters should be taken from user.

As per example lets see if user inputs pin code, that code contains only 6characters so how can I display that *"No_Of_characters characters are left*". If user inputs 2 characters and my text box's max length is 6 char then it must be displayed "4charcters" left ! How can I do this ? Please suggest any java script or Ajax or any type of code !

Gracias de antemano!

One more thing! I am developing registration form so obviously there are multiple textboxes. If I want to give such type of labeled information to every textbox with one function, Is that possible ?

preguntado el 31 de enero de 12 a las 08:01

1 Respuestas

usar javascript

    <SCRIPT LANGUAGE="JavaScript">
// function parameters are: field - the string field, count - the field for remaining characters number and max - the maximum number of characters
function CountLeft(field, count, max) {
// if the length of the string in the input field is greater than the max value, trim it
if (field.value.length > max)
field.value = field.value.substring(0, max);
// calculate the remaining characters
count.value = max - field.value.length;

<form name=sample action="">
<font size="1" face="arial, helvetica, sans-serif"> Only 50 characters allowed!
// at every key up and key down event the upper function is called
<input name="text" type="text" size="40"
<input readonly type="text" name="left" size=3 maxlength=3 value="50">
characters left</font>


otro es

<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" TextMode="multiLine" MaxLength="100" runat="server" onkeyup="javascript:CharCounter(;" onblur="javascript:CharCounter(;"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="100 chars remaining"></asp:Label>
<script language="javascript" type="text/javascript">
function CharCounter(textId)
var totalchar = '<%= TextBox1.MaxLength %>';
var txtbox = document.getElementById(textId);
var lbl = document.getElementById('<% =Label1.ClientID %>');
if(txtbox.value.length > totalchar)
txtbox.value = txtbox.value.substring(0,totalchar);
lbl.innerText = (totalchar - txtbox.value.length) + " chars remaining" ;

for this refer

you can call the same function for all textbox in your form but you have to customize function so that it takes id of the text box to count character and id of the label where the number of count to be displayed.

Respondido el 31 de enero de 12 a las 12:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.