-1

I'm doing some changes on my website and I have made my form with the select box. Before select, I was using input fields but now I changed them to select because this fields are reading values from my database.

So now I'm trying to mark these select fields as required but can't figure out how to do it.

This is my code for the input fields with required flag true, but now how to do the same for the select fields.

jQuery(".next").click(function () {
    var filter = true,
    reded = [];
    jQuery(".visible-path input").filter("[required]").each(function () {
        if (jQuery(this).val() === '') {
            jQuery(this).css("border", "1px solid red");
            reded.push(jQuery(this).index("input"));
            filter = false;
        }
    });

    setTimeout(function () {
        for (var i = 0; i < reded.length; i++) {
            jQuery("input").eq(reded[i]).css("border", "1px solid #e7e7e7");
            jQuery("select").eq(reded[i]).css("border", "1px solid #e7e7e7");
        }
        reded = [];
    }, 800);
ShailyAggarwal
  • 813
  • 9
  • 20
Be Behind
  • 47
  • 10
  • `required` also works for select inputs – Ali Sheikhpour Apr 22 '19 at 07:00
  • No need for jQuery: https://stackoverflow.com/questions/6048710/can-i-apply-the-required-attribute-to-select-fields-in-html5/6048891#6048891 – mplungjan Apr 22 '19 at 07:01
  • just do it like this `` –  Apr 22 '19 at 07:03
  • Your code could be `$(".visible-path input[required]").each(function () { $(this).toggleClass("red",this.value==="") });` – mplungjan Apr 22 '19 at 07:05
  • then later: `setTimeout(function () { $(".red").css("border", "1px solid #e7e7e7"); }, 800);` – mplungjan Apr 22 '19 at 07:07
  • @mplungjan I write this code couple of years ago, but ok you still made it for the input fields, but what is happening with SELECT fields. Do I need to get his attr() or ID, and save him in new var... This code is working for the input fields, but now I added some select fields. – Be Behind Apr 22 '19 at 07:12
  • You can use ":input" - it will take the selects too: https://stackoverflow.com/a/18659780/295783 – mplungjan Apr 22 '19 at 07:14

1 Answers1

-1

No need of JQuery, Just add HTML5 attribute "required" to select. It take take care of validation.

<select required>
....
</select>
  • Please do not make answers from comments. We already had a discussion in the comments under the question – mplungjan Apr 22 '19 at 07:08