Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Regla de validación de jQuery SOLO AL ENVIAR

Digamos que tengo un formulario que busca ubicaciones de ciudades y estados. si el usuario ingresa una ciudad o estado incorrecto, quiero que el formulario pueda verificar la base de datos con la entrada de los usuarios y ver si la entrada es válida o no. Si no es así, quiero que el usuario sea rápido.

Entonces form Validate es el complemento para mí.

 $("#form").validate({ rules: { citystate: { required: true, myCustomAjaxSyncronousCheckAgainstDBRule: true } submitHandler: function(form) { if ($("#form").valid()) { form.submit(); } } });

Ahora digamos que este formulario debe usarse en 4 lugares diferentes en mi sitio web, por lo que sería difícil refactorizar myCustomAjaxSyncronousCheckAgainsDBRule (nombre falso por cierto) y tener el mismo código 4 veces en mi sitio web. Por eso creé una regla personalizada en el archivo JS. Pero, ¿hay alguna manera de que la regla SOLO PUEDE verificarse en el momento del envío? Dado que si hay una entrada de usuario no válida, la verificará en CADA pulsación de tecla, lo que puede ser bastante engorroso. Especialmente porque tengo jQuery.ui.autocomplete ejecutándose en conjunto.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Todas las validaciones se pueden configurar de forma independiente (onclick, onkeypress,onsubmit,etc.)

Las demostraciones y los detalles están disponibles en el sitio de documentos http://docs.jquery.com/Plugins/Validation/validate

 $("#form").validate({ onkeyup: false, onclick: false })

over 4 years ago · Santiago Trujillo Report

0

Como alternativa, de una manera más sencilla, puede pasar una función a la configuración onfocusout y onkeyup de jquery.validator para decidir si un elemento debe validarse en esos eventos o no, como esto:

 jQuery.validator.defaults.onfocusout = function (element, event) { // detectect if is the element you dont want validate // the element has to have the attribute 'data-control="mycitycontrol"' // you can also identify your element as you please if ($(element).data("control") === "mycitycontrol") return; if (!this.checkable(element) && (element.name in this.submitted || !this.optional(element))) { this.element(element); } } jQuery.validator.defaults.onkeyup = function (element, event) { // detectect if is the element you dont want validate // the element has to have the attribute 'data-control="mycitycontrol"' // you can also identify your element as you please if ($(element).data("control") === "mycitycontrol") return; if (event.which === 9 && this.elementValue(element) === "") { return; } else if (element.name in this.submitted || element === this.lastElement) { this.element(element); } }
over 4 years ago · Santiago Trujillo Report

0

$.extend($("#form-id").validate().settings, { onkeyup: false, onfocusout: false });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!