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

264
Views
How can I submit a form using JavaScript while still running HTML validations (minlength, maxlength, pattern...)

I'm using ReCAPTCHA which means that I have to submit the form from JS, not HTML, but HTMLFormElement.submit() doesn't confirm the form is valid first and I have written some checks that I would like to still be effective. The only solutions I see are for writing all the checks in JS (for people wanting custom checks) and ones that use JQuery. None of the solutions seem to have the browser-styled error messages that show below the field which isn't preferable. Is there a simple way to do this or do I have to do something like iterating over all the fields and validate them manually? Preferably, I would also like to run the checks before ReCAPTCHA even generates a score, but I doubt this is possible because of how limited ReCAPTCHA's API seems to be. It's worth noting that I manually validate the values server-side already, as I'm aware it would be easy for people to get round these checks, but I'd like to avoid unnecessary POST requests and be able to use the browser-styled error messages.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all, you have too many questions/issues in one post. You're normally expected to try and figure things out on your own as much as you can before asking. Here are some issues I picked up:

  1. Form validation process does not conflict with form submission process.
  2. Whether recaptcha goes before or after the validation is not an important issue, but vast majority of forms where it's used, do it after the validation.
  3. Try to avoid using JQuery since it adds an unneeded dependency. Pure JS is just as powerful.
  4. If you want a nicely styled error message - that's a completely different issue that has no relation to recaptcha or form submission. You now talking about DOM editing and styling.
  5. Running something before recaptcha is a question of timing things rather than limitations of Recaptcha's API. It's completely in your power.
  6. Server-side validation and client-side validation are both needed, but they largely play different roles: client is more for user-friendly error reporting while server is a real validation of user input. It's a good practice to keep them in tight sync. So having BE validation doesn't excuse you from having FE validation.
  7. You don't need additional POST requests for FE validation. Additional communication with the back-end during validation is needed for things that the client can't validate.
  8. Finally, you can do your validation before submitting the payload to the server. I suggest looking at the concept of async: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function This is to make your form submission after the response from Recaptcha comes and after you're done with your FE validation. Promises is how it's usually comfortable to work with: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
about 4 years ago · Juan Pablo Isaza 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!