Проверка ввода типа радио - PullRequest
       8

Проверка ввода типа радио

0 голосов
/ 11 сентября 2018

Я пытаюсь создать форму проверки с помощью jQuery Form Validator (http://www.formvalidator.net/index.html). Я хочу выполнить проверку элементов type="radio". Если вы не выберете какую-либо опцию, появится сообщение Please select a option.Это мой HTML код:

<form id="form">
    <label class="form__box--label" for="radio">What type of prototype do you need?</label> <br>
            <div>
                <input id="basic" class="form__box--radio" type="radio" name="radio"  value="Basic">
                <label class="form__box--option" for="basic">
                  Basic
                </label>
              </div>
              <div>
                <input id="premium" class="form__box--radio" type="radio" name="radio" value="premium">
                <label class="form__box--option" for="premium">
                  Premium
                </label>
              </div>


            <input type="submit" class="sectionform__form--button" value="Get an estimation">
          </form>

Объявление это JS код:

$(document).ready(function(){
  $.validate({
   modules: 'html5',
    {
    rules:    {
      radio:{ required:true }
    },
    messages:
    {
      radio:
      {
        required:"Please select a option<br/>"
      }
    },
    errorPlacement: function(error, element)
    {
        if ( element.is(":radio") )
        {
            error.appendTo( element.parents('.sectionform__form--box') );
        }
        else
        { // This is the default behavior
            error.insertAfter( element );
        }
     }
    }
  });

});

Ответы [ 2 ]

0 голосов
/ 11 сентября 2018

Вы можете использовать, как показано ниже.

<form id="form">
    <label class="form__box--label" for="radio">What type of prototype do you need?</label> <br>
    <div>
        <input id="basic" class="form__box--radio" type="radio" name="radio"  value="Basic" data-validation="required" data-validation-error-msg="Please select a option">
        <label class="form__box--option" for="basic">
            Basic
        </label>
    </div>
    <div>
        <input id="premium" class="form__box--radio" type="radio" name="radio" value="premium" data-validation="required" data-validation-error-msg="Please select a option">
        <label class="form__box--option" for="premium">
            Premium
        </label>
    </div>
    <input type="submit" class="sectionform__form--button" value="Get an estimation">
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>
<script>
    $(document).ready(function () {
        $.validate({
            modules: 'html5',
            rules: {
                radio: {required: true}
            },
            messages:{
                radio:{ required: "Please select a option<br/>" }
            },
            errorPlacement: function (error, element)
            {
                if (element.is(":radio")){
                    error.appendTo(element.parents('.sectionform__form--box'));
                }
                else { // This is the default behavior
                    error.insertAfter(element);
                }
            }
        });
    });
</script>
0 голосов
/ 11 сентября 2018

Замените свой код JS следующим:

$('#form').validate({
   rules: {
     radio:{ required:true }
   },
   messages:
   {
     radio:
     {
       required:"Please select a option<br/>"
     }
   },
   errorPlacement: function(error, element)
   {
       if ( element.hasClass("form__box--radio") )
       {
          error.insertAfter( element.next('label') );
       }
       else
       { // This is the default behavior
          error.insertAfter( element );
       }
    }
   }
});
...