Cita:
espero y me puedan ayudar!!!! if(document.frmcontactanos['txtnombre'].value == txtnombre[1]) { document.frmcontactanos['txtnombre'].focus(); document.write('el campo "nombre" es obligatorio.'); }
| |||
:::document write::: Hola a todos soy nuevo en este foro y pues necesito de su ayuda... Tengo un formulario para un contactanos pero ahora lo que necesito es que cuando el usuario no escriba nada en las cajas de texto mande un error por ejemplo asi cuando creamos una cuenta y si ese correo ya existe nos manda una advertencia o cuando queremos usar el nick de un usuario que ya existe el codigo hasta ahorita que tengo es el siguiente: Cita: espero y me puedan ayudar!!!! if(document.frmcontactanos['txtnombre'].value == txtnombre[1]) { document.frmcontactanos['txtnombre'].focus(); document.write('el campo "nombre" es obligatorio.'); } |
| ||||
Respuesta: :::document write::: Puedes usar un alert("El campo 'nombre' es obligatorio"); para mostrar una ventana con el mensaje o tener ese texto en un elemento oculto y mostrarlo con style.display="none"/"block"
__________________ Por favor, antes de preguntar, revisa la Guía para realizar preguntas. |
| |||
Respuesta: :::document write::: mmm si ya use el alert pero no quiero que aparesca la ventanita, lo que yo deseaba era que mostrara el mensaje a un lado de las cajas de texto asi como cuando creamos una cuenta en hotmail o aqui mismo en el foro.... |
| ||||
Respuesta: :::document write::: Como te comenté, en ese caso puedes colocar ese mensaje en un elemento oculto (display: none) y luego mostrarlo cambiando el estilo:
Código Javascript:
Ver original
__________________ Por favor, antes de preguntar, revisa la Guía para realizar preguntas. |
| |||
Respuesta: :::document write::: A continuacion voy a poner el codigo que me esta causando conflicto espenrando contar con su ayuda!!!! Última edición por infinitum88; 09/10/2009 a las 00:07 Razón: DUPLICIDAD DE MENSAJES |
| |||
Respuesta: :::document write::: Hola este es el codigo que tengo hasta ahorita: Cita: y en mis input text tengo lo siguiente: <script type="text/javascript"> validateform=function() { var txtnombre = new array(document.getelementbyid('txtnombre'), document.frmcontactanos['txtnombre'].value); var txtdireccion = new array(document.getelementbyid('txtdireccion'), document.frmcontactanos['txtdireccion'].value); var txtemail = new array(document.getelementbyid('txtemail'), document.frmcontactanos['txtemail'].value); var txttelefono = new array(document.getelementbyid('txttelefono'), document.frmcontactanos['txttelefono'].value); var txtpoblacion = new array(document.getelementbyid('txtpoblacion'), document.frmcontactanos['txtpoblacion'].value); var txtasunto = new array(document.getelementbyid('txtasunto'), document.frmcontactanos['txtasunto'].value); var txtcomentario = new array(document.getelementbyid('txtcomentario'), document.frmcontactanos['txtcomentario'].value); var submit = document.getelementbyid('btnenviar'); /* eventos */ txtnombre[0].onfocus = onfocus; txtnombre[0].onblur = onblur; txtdireccion[0].onfocus = onfocus; txtdireccion[0].onblur = onblur; txtemail[0].onfocus = onfocus; txtemail[0].onblur = onblur; txttelefono[0].onfocus = onfocus; txttelefono[0].onblur = onblur; txtpoblacion[0].onfocus = onfocus; txtpoblacion[0].onblur = onblur; txtasunto[0].onfocus = onfocus; txtasunto[0].onblur = onblur; txtcomentario[0].onfocus = onfocus; txtcomentario[0].onblur = onblur; submit.onclick = validatefields; /* funciones */ function onfocus() { if(document.frmcontactanos[this.name].value==eval(this.name)[1]) document.frmcontactanos[this.name].value='' } function onblur() { var value = document.frmcontactanos[this.name].value; value = value.replace(/^\s*/, ''); value = value.replace(/\s*$/, ''); if(!value) document.frmcontactanos[this.name].value = eval(this.name)[1] } function validatefields() { if(document.frmcontactanos['txtnombre'].value == txtnombre[1]) { document.frmcontactanos['txtnombre'].focus(); document.write("el campo 'nombre' es obligatorio."); } else if(document.frmcontactanos['txtdireccion'].value == txtdireccion[1]) { document.frmcontactanos['txtdireccion'].focus(); document.write('el campo "dirección" es obligatorio.'); } else if(document.frmcontactanos['txtemail'].value == txtemail[1]) { document.frmcontactanos['txtemail'].focus(); document.write('el campo "email" es obligatorio.'); } else if(document.frmcontactanos['txttelefono'].value == txttelefono[1]) { document.frmcontactanos['txttelefono'].focus(); document.write('el campo "teléfono" es obligatorio.'); } else if(document.frmcontactanos['txtpoblacion'].value == txtpoblacion[1]) { document.frmcontactanos['txtpoblacion'].focus(); document.write('el campo "población" es obligatorio.'); } else if(document.frmcontactanos['txtasunto'].value == txtasunto[1]) { document.frmcontactanos['txtasunto'].focus(); document.write('el campo "asunto" es obligatorio.'); } else if(document.frmcontactanos['txtcomentario'].value == txtcomentario[1]) { document.frmcontactanos['txtcomentario'].focus(); document.write('el campo "comentario" es obligatorio.'); } else { return true; } return false; } } if(document.all&&window.attachevent) { window.attachevent("onload",validateform); } // ie-win else if(window.addeventlistener) { window.addeventlistener("load",validateform,false) ; } function solo_numerico(variable) { numer=parseint(variable); if (isnan(numer)) { return ""; } return numer; } function valnumero(control) { control.value=solo_numerico(control.value); } claseerror="requerido"; function validoemail(email) { var s = email; if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(s)) return true; else return false; } function valida_envia() { var txtnombre = document.frmcontactanos.txtnombre.value; var txtdireccion = document.frmcontactanos.txtdireccion.value; var txttelefono = document.frmcontactanos.txttelefono.value; var txtemail = document.frmcontactanos.txtemail.value; var txtpoblacion = document.frmcontactanos.txtpoblacion.value; var txtasunto = document.frmcontactanos.txtasunto.value; var txtcomentario = document.frmcontactanos.txtcomentario.value; if (txtnombre.length==0 || txtdireccion.length==0 || txttelefono.length==0 || txtemail.length==0 || txtpoblacion.length==0 || txtasunto.length==0 || txtcomentario.length==0) { document.getelementbyid("error").style.display="bl ock"; if (txtnombre.length==0) document.frmcontactanos.txtnombre.classname=clasee rror; else document.frmcontactanos.txtnombre.classname=""; if (txtdireccion.length==0) document.frmcontactanos.txtdireccion.classname=cla seerror; else document.frmcontactanos.txtdireccion.classname=""; if (txttelefono.length==0) document.frmcontactanos.txttelefono.classname=clas eerror; else document.frmcontactanos.txttelefono.classname=""; if (txtemail.length==0) document.frmcontactanos.txtemail.classname=claseer ror; else document.frmcontactanos.txtemail.classname=""; if (txtpoblacion.length==0) document.frmcontactanos.txtpoblacion.classname=cla seerror; else document.frmcontactanos.txtpoblacion.classname=""; if (txtasunto.length==0) document.frmcontactanos.txtasunto.classname=clasee rror; else document.frmcontactanos.txtasunto.classname=""; if (txtcomentario.length==0) document.frmcontactanos.txtcomentario.classname=cl aseerror; else document.frmcontactanos.txtcomentario.classname="" ; return 0; } else { document.frmcontactanos.submit(); } } function revalida(cual) { var ese = document.getelementbyid(cual).value; if (ese.length>0){document.getelementbyid(cual).class name="";} var txtnombre = document.frmcontactanos.txtnombre.value; var txtdireccion = document.frmcontactanos.txtdireccion.value; var txttelefono = document.frmcontactanos.txttelefono.value; var txtemail = document.frmcontactanos.txtemail.value; var txtpoblacion = document.frmcontactanos.txtpoblacion.value; var txtasunto = document.frmcontactanos.txtasunto.value; var txtcomentario = document.frmcontactanos.txtcomentario.value; if (txtnombre.length>0 && txtdireccion.length>0 && txttelefono.length>0 && txtemail.length>0 && txtpoblacion.length>0&& txtasunto.length>0 && txtcomentario.length>0){document.getelementbyid("e rror").style.display="none";} if (txtemail.length>0){ if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) { document.frmcontactanos.txtemail.classname=""; document.getelementbyid("error2").style.display="n one"; } else { document.getelementbyid("error2").style.display="b lock"; document.frmcontactanos.txtemail.classname=claseer ror; } } } </script> Cita: <input name="txtnombre" type="text" id="txtnombre" tabindex="1" class="input-text" title="introduce tu nombre" value="---nombre---" accesskey="n" onkeypress="javascript:revalida(this.id);"> <p id="error" class='error' style='display:none'>introduce el nombre es obligatorio</p> |
| ||||
Respuesta: :::document write::: Así sin mirar detalladamente el código, el nombre de la función es getElementById, no getelementbyid, que el nombre es sensible a mayúsculas.
__________________ Por favor, antes de preguntar, revisa la Guía para realizar preguntas. |
| |||
Respuesta: :::document write::: HOLA A TODOS ESTOY TRATANDO DE AGREGAR UNA FUNCION A MI codigo de javascript para que me valide el email pero no me funciona o en que estare fallando: mi codigo es el siguiente: Cita: y en el form tengo lo siguiente: <script type="text/javascript"> validateForm=function() { var txtNombre = new Array(document.getElementById('txtNombre'), document.frmContactanos['txtNombre'].value); var txtDireccion = new Array(document.getElementById('txtDireccion'), document.frmContactanos['txtDireccion'].value); var txtEmail = new Array(document.getElementById('txtEmail'), document.frmContactanos['txtEmail'].value); var txtTelefono = new Array(document.getElementById('txtTelefono'), document.frmContactanos['txtTelefono'].value); var txtPoblacion = new Array(document.getElementById('txtPoblacion'), document.frmContactanos['txtPoblacion'].value); var txtAsunto = new Array(document.getElementById('txtAsunto'), document.frmContactanos['txtAsunto'].value); var txtComentario = new Array(document.getElementById('txtComentario'), document.frmContactanos['txtComentario'].value); var submit = document.getElementById('btnEnviar'); /* Eventos */ txtNombre[0].onfocus = onFocus; txtNombre[0].onblur = onBlur; txtDireccion[0].onfocus = onFocus; txtDireccion[0].onblur = onBlur; txtEmail[0].onfocus = onFocus; txtEmail[0].onblur = onBlur; txtTelefono[0].onfocus = onFocus; txtTelefono[0].onblur = onBlur; txtPoblacion[0].onfocus = onFocus; txtPoblacion[0].onblur = onBlur; txtAsunto[0].onfocus = onFocus; txtAsunto[0].onblur = onBlur; txtComentario[0].onfocus = onFocus; txtComentario[0].onblur = onBlur; submit.onclick = validateFields; /* Funciones */ function onFocus() { if(document.frmContactanos[this.name].value==eval(this.name)[1]) document.frmContactanos[this.name].value='' } function onBlur() { var value = document.frmContactanos[this.name].value; value = value.replace(/^\s*/, ''); value = value.replace(/\s*$/, ''); if(!value) document.frmContactanos[this.name].value = eval(this.name)[1] } function validateFields() { if(document.frmContactanos['txtNombre'].value == txtNombre[1]) { document.frmContactanos['txtNombre'].focus(); alert("El campo 'Nombre' es obligatorio."); } else if(document.frmContactanos['txtDireccion'].value == txtDireccion[1]) { document.frmContactanos['txtDireccion'].focus(); alert('El campo "Dirección" es obligatorio.'); } else if(document.frmContactanos['txtTelefono'].value == txtTelefono[1]) { document.frmContactanos['txtTelefono'].focus(); alert('El campo "Teléfono" es obligatorio.'); } else if(document.frmContactanos['txtEmail'].value == txtEmail[1] && if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txtEmail[1])) { document.frmContactanos['txtEmail'].focus(); alert('El campo "Email" es obligatorio.'); } else if(document.frmContactanos['txtPoblacion'].value == txtPoblacion[1]) { document.frmContactanos['txtPoblacion'].focus(); alert('El campo "Población" es obligatorio.'); } else if(document.frmContactanos['txtAsunto'].value == txtAsunto[1]) { document.frmContactanos['txtAsunto'].focus(); alert('El campo "Asunto" es obligatorio.'); } else if(document.frmContactanos['txtComentario'].value == txtComentario[1]) { document.frmContactanos['txtComentario'].focus(); alert('El campo "Comentario" es obligatorio.'); } else { return true; } return false; } } if(document.all&&window.attachEvent) { window.attachEvent("onload",validateForm); } // IE-Win else if(window.addEventListener) { window.addEventListener("load",validateForm,false) ; } </script> Cita: tambien quiero agregar una funcion que me valide el campo telefono..... Ayudaaaaaa <form name="frmContactanos" method="post" action="#" enctype="multipart/form-data"> <input name="txtNombre" type="text" id="txtNombre" tabindex="1" class="input-text" title="Introduce tu nombre" value="---Nombre---" accesskey="n"> <input name="txtDireccion" type="text" id="txtDireccion" tabindex="2" class="input-text" title="Introduce tu dirección" value="---Dirección---" accesskey="d"> <input name="txtTelefono" type="text" id="txtTelefono" tabindex="3" class="input-text" title="Introduce tu teléfono" value="---Teléfono---" accesskey="t"> <input name="txtEmail" type="text" id="txtEmail" tabindex="4" class="input-text" title="Introduce tu email" value="---Email---" accesskey="e"> <input name="txtPoblacion" type="text" id="txtPoblacion" tabindex="5" class="input-text" title="Introduce tu población" value="---Población---" accesskey="p"> |