Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/07/2008, 14:44
roly0476
 
Fecha de Ingreso: septiembre-2007
Mensajes: 58
Antigüedad: 16 años, 8 meses
Puntos: 0
Respuesta: Contar Palabras en TextBox Multiline con VBSCRIPT

Bueno estuve googleando y encontré una solución aquí les dejo el código completo.

No es igual al que encontre ya que son dos soluciones en una sola, espero le sirva a alguien.

No se olviden comentar si lo usan y si les fue útil.

saludos.


Codigo html: .aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Textbox2.aspx.vb" Inherits="Textbox2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function setMaxLength() {
var x = document.getElementsByTagName('textarea');
var counter = document.createElement('div');
counter.className = 'counter';
for (var i=0;i<x.length;i++) {
if (x[i].getAttribute('maxlength')) {
var counterClone = counter.cloneNode(true);
counterClone.relatedElement = x[i];
counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

x[i].onkeyup = x[i].onchange = checkMaxLength;
x[i].onkeyup();
}
}
}

function checkMaxLength() {
var maxLength = this.getAttribute('maxlength');
var currentLength = this.value.length;
if (currentLength > maxLength)
this.relatedElement.className = 'toomuch';
else
this.relatedElement.className = '';
this.relatedElement.firstChild.nodeValue = currentLength;
// not innerHTML
}
</script>
<script language="javascript" type="text/javascript">
function ValidarCaracteres(TextBox0,maxlength){

if (TextBox0.value.length > maxlength){
TextBox0.value = TextBox0.value.substring(0,maxlength);
alert("Debe ingresar hasta un maximo de "+maxlength+" caracteres");
}
}
</script>
<script language="javascript" type="text/javascript">
function ValidarCaracteres(TextBox1,maxlength){

if (TextBox1.value.length > maxlength){
TextBox1.value = TextBox1.value.substring(0,maxlength);
alert("Debe ingresar hasta un maximo de "+maxlength+" caracteres");
}
}
</script>
</head>
<body runat="server" id="body1">
<form id="form1" runat="server">

<p>
<asp:TextBox ID="TextBox0" runat="server"
Height="88px" TextMode="MultiLine" Width="269px"></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox1" runat="server" Height="88px" TextMode="MultiLine" Width="269px"></asp:TextBox>
</p>
</form>
</body>
</html>
__________________________________________________ _____________

Codigo interno .vb

Partial Class Textbox2
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox0.Attributes.Add("onkeypress", " ValidarCaracteres(this, 10);")
TextBox0.Attributes.Add("onkeyup", " ValidarCaracteres(this, 10);")
TextBox1.Attributes.Add("onkeypress", " ValidarCaracteres(this, 10);")
TextBox1.Attributes.Add("onkeyup", " ValidarCaracteres(this, 10);")
End Sub
Protected Sub TextBox1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox0.Load
TextBox0.Attributes.Add("maxlength", "10")
body1.Attributes.Add("onload", "setMaxLength();")
End Sub
Protected Sub TextBox2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Load
TextBox1.Attributes.Add("maxlength", "10")
body1.Attributes.Add("onload", "setMaxLength();")
End Sub
End Class