valor validar validacion texto span obtener formularios formulario enviar ejemplos con asignar antes javascript jquery jquery-selectors

validacion - validar formulario javascript html5



jQuery verifica si alguna entrada de texto tiene valor (5)

El problema con la obtención de la propiedad length en filter() es que jQuery evaluará cada elemento de la colección, solo para completar un conteo cuando lo único que nos importa es si el valor es mayor que cero.

Ninguna de las respuestas actuales e incluso el propio .is() , .has() y .filter() hacen uso del cortocircuito tan pronto como se cumplen los criterios.

Puede definir un método de extensión simple llamado .any() como este:

jQuery.fn.any = function(filter){ for (i=0 ; i<this.length ; i++) { if (filter.call(this[i])) return true; } return false; };

Y luego pasar en una función de filtrado como esta:

var someInputsEmpty = $("#reference,#pin,#fName,#mName,#datepicker").any(function() { return this.value == ''''; });

jQuery.fn.any = function(filter){ for (i=0 ; i<this.length ; i++) { if (filter.call(this[i])) return true; } return false; }; $(function() { var gotMatch = $(":input").any(function() { return this.value == ''hi''; }); if (gotMatch) { console.log("Hello to you too!"); } else { console.log("Why don''t you say Hi!"); } })

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" value=""> <input type="text" value=""> <input type="text" value="">

Otras lecturas:

  • Formulario de verificación Jquery para ver si alguna entrada tiene valor
  • ¿Existe una función "existe" para jQuery?
  • Formulario de verificación Jquery para ver si alguna entrada tiene valor

Quiero preguntar si hay una mejor manera en jQuery para seleccionar la entrada de texto múltiple y luego verificar si alguno de ellos tiene un valor. Aquí está mi código:

if ($("#reference").val() != "" || $("#pin").val() != "" || $("#fName").val() != "" || $("#mName").val() != "" || $("#datepicker").val() != "") { /*logic goes here */ }


Prueba jQuery each()

$(''input[type=text]'').each(function(){ var text_value=$(this).val(); if(text_value!='''') { console.log(''Value exist''); } })


Qué tal si:

http://jsfiddle.net/lollero/rr2ss/1/

Otro ejemplo: http://jsfiddle.net/lollero/rr2ss/12/

$(function() { // Check value of each and every input element.... // Which you can of course change to be more specific, like: $(''#myForm input'') $( "input" ).val(function( i, val ) { // Return the console log IF value is not empty // value=" " still counts as "not empty", because technically it isn''t // You could of course replace the console.log(); with anything you''d like val && console.log(''not empty: input-'' + (++i) ); // Note that you don''t have to return val if you don''t want to, it''s just for show in this case. return val }); });


Solo usa esto:

if (!$("#id").val().length == 0))


Usted podría hacer como a continuación:

if ($("#reference,#pin,#fName,#mName,#datepicker").filter(function() { return $(this).val(); }).length > 0) { //.. }

Usar una función común como la siguiente lo haría reutilizable:

function hasValue(elem) { return $(elem).filter(function() { return $(this).val(); }).length > 0; }

Y podrías llamarlo así:

hasValue("#my-input-id");