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

296
Views
How to validate select fields in html, laravel and javascript

I'm trying to make sure every field is required but this code only works on "input" tags but not on the select and textarea tags.

Any help will be appreciated. Thanks!

HERE IS MY BLADE

<div class="tab">
    <div class="form-group mb-3">
        <select name="currency" class="choices form-control" id="devise{{ Auth::user()->cred->id }}"
                onchange="deviseSelect({{ Auth::user()->cred->id }})" required="true">
            <option value="" selected disabled>choose</option>
            <option value="USD">USD</option>
            <option value="FC">FC</option>
        </select>
    </div>
</div>

HERE IS MY JAVASCRIPT FOR THE "SELECT" HTML TAG

    <script>
        var currentTab = 0; // Current tab is set to be the first tab (0)
        showTab(currentTab); // Display the current tab
        function validateSelect()
        {
            // This function deals with validation of the form fields
            var x,z, i, valid = true;
            x = document.getElementsByClassName("tab");
            z = x[currentTab].getElementsByTagName("select");

            // A loop that checks every select field in the current tab:
            for (i = 0; i < z.length; i++) {
                var option = z.options[i];
                // If a field is empty...
                if (option.value == "") {
                    // add an "invalid" class to the field:
                    z[i].className += " required";
                    // and set the current valid status to false:
                    valid = false;
                }
            }
            // If the valid status is true, mark the step as finished and valid:
            if (valid) {
                document.getElementsByClassName("step")[currentTab].className += " finish";
            }
            return valid; // return the valid status
        }
    </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I FOUND WHERE I'VE MISTAKEN

I just changed something inside the for loop and everything started working perfectly.

<script>
for (i = 0; i < z.length; i++) {
    // If a field is empty...
    if (z[i].value == "") {
        // add an "invalid" class to the field:
        z[i].className += " is-invalid";
        // and set the current valid status to false:
        valid = false;
    }
}
</script>

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!