Validate dynamic radio buttons


Reez0

I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected.

remove.html

{%for laptop in laptops%}
    <form action = "{%url 'laptops:delete'%}" method = "post" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="radio">
  <input type="radio" id = "laptop_{{laptop.id}}" name="laptopname" value = "{{laptop.id}}">
            <label for = "laptop_{{laptop.id}}">{{laptop}}</label>
</div>
{%endfor%}
<input type="submit" value="Submit" id="delete-button"/>
    </form>

views.py

#Gets id value from the 'laptopname' radio button
laptopid = int(request.POST.get('laptopname'))
#Finds matching id value
laptop = Laptop.objects.get(id=laptopid)
#Populates form with matching data
data = {'Name': laptop.Name, 'specification': laptop.specification,'warranty': laptop.warranty,
    'condition':laptop.condition}
form = forms.MakeSale(initial=data)
return render(request, 'laptops/laptop_sale.html', {'form': form})

Is my approach wrong? Should I be using a form with radio buttons for validation?

Gabriel Tufis

(jQuery, but logic should apply)

The way I do this is to create an event where the user clicks a radio button to give me the ID of that particular radio button. Using the ID, you know that the user pressed the witch radio button.

$("input").click(function () {
   alert($(this).attr("id"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
</head>
<body>

<input id="1" type="radio" name="lala">
<input id="2" type="radio" name="lala">
<input id="3" type="radio" name="lala">


<script  type="text/javascript" src="Demo.js"></script>
</body>
</html>

EDIT: If you just want to make sure the user clicks the radio button then just add at the end

<input id="3" type="radio" name="lala" required>

Related


Validate radio buttons AngularJS

Spencer: This seems like it should be easy, but I didn't find the answer. I have a form in which I need to confirm if a selection has been made from a broadcast group. I tried using the 'required' attribute on the radio buttons, but after validating the form i

How to validate Materializecss radio buttons?

(Ranjith varatharajan I've tried implementing validation to display an error message on the radio button, but couldn't succeed. I am using Materializecssframework. Validating/displaying error messages works for textbox elements, not for radio buttons. code: <s

How to validate bootstrap radio buttons

Jimmy I'm trying to validate this part of the form, but option1 returns false no matter which button is selected. How can I make each radio have a different value when they are active/inactive, or get a value from the button group? $(function register(){ $('.e

Validate radio buttons AngularJS

Spencer: This seems like it should be easy, but I didn't find the answer. I have a form in which I need to confirm if a selection has been made from a broadcast group. I tried using the 'required' attribute on the radio buttons, but after validating the form i

Validate an array of radio buttons in Laravel

Coffey I am using Laravel 5.1 and Homestead. My problem is that when validation fails, the radio buttons don't repopulate with the original user entered data. My validation rules from PostRequest.php public function rules() { //validating input tex

Dynamic radio buttons PHP

Chen Enen I am currently creating an online quiz. In order for me to retrieve the information from the input, I think I need to enter the value of the radio button. I want the values to be entered as 1 for the first, 2 for the second, and so on. Since I'm alre

How to validate radio buttons in Android?

Forged Here is my coding. I don't know how to validate radio buttons. When I click the next button, it keeps going even though the radio button is not clicked. Sorry, my English is no good. please help me. Thank you. protected void onCreate(Bundle savedInstanc

Validate an array of radio buttons in Laravel

Coffey I am using Laravel 5.1 and Homestead. My problem is that when validation fails, the radio buttons don't repopulate with the original user entered data. My validation rules from PostRequest.php public function rules() { //validating input tex

How to validate Materializecss radio buttons?

(Ranjith varatharajan I've tried implementing validation to display an error message on the radio button, but couldn't succeed. I am using Materializecssframework. Validating/displaying error messages works for textbox elements, not for radio buttons. code: <s

How to validate bootstrap radio buttons

Jimmy I'm trying to validate this part of the form, but option1 returns false no matter which button is selected. How can I make each radio have a different value when they are active/inactive, or get a value from the button group? $(function register(){ $('.e

How to validate bootstrap radio buttons

Jimmy I'm trying to validate this part of the form, but option1 returns false no matter which button is selected. How can I make each radio have a different value when they are active/inactive, or get a value from the button group? $(function register(){ $('.e

Dynamic handheld with radio buttons

Patra Pedro Hi, you want to get a dynamic (changing number of rows) rhandsontable with the input of the radio button. So I've been working on a shiny that needs to enter data into a table. However, when adding more complexity, the annual option is required. So

Validate dynamic radio buttons

Reez0 I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected. remove.html {%for laptop in laptops%} <form action = "{%url 'laptops:delete'%}" method = "post"

Validate dynamic radio buttons

Reez0 I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected. remove.html {%for laptop in laptops%} <form action = "{%url 'laptops:delete'%}" method = "post"

Validate dynamic radio buttons

Reez0 I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected. remove.html {%for laptop in laptops%} <form action = "{%url 'laptops:delete'%}" method = "post"

How to validate radio buttons?

Dean Roman I am new to coding and I have some radio buttons that need to be validated without using jquery. if(document.getElementById('GCSE').checked){ examLevel = "GCSE"; } if(document.getElementById('AS').checked){ examLevel = "AS"; } if(document.getElement

Validate radio buttons in jQuery

David I'm using the following method to make sure the field is not null before posting in jquery: if(this.field.value == "") { jQuery( ".page-error-message" ).remove(); jQuery(".top").append(jQuery("field required")); jQuery('html, body').animate({ scrol

Validate radio buttons with JavaScript

username I have 2 radio buttons. I need to validate radio buttons using javascript. Please tell me what is wrong with my code. Here is the code. $(function() { $("#XISubmit").click(function(){ var XIGender= document.forms["XIForm"]["XIGender"].val

Validate radio buttons AngularJS

Spencer This seems like it should be easy, but I didn't find the answer. I have a form and need to verify that a selection has been made from a broadcast group. I've tried using the 'required' attribute on the radio buttons, but after validating the form it wi

Validate radio buttons in jQuery

David I'm using the following to make sure the fields are not null before posting in jquery: if(this.field.value == "") { jQuery( ".page-error-message" ).remove(); jQuery(".top").append(jQuery("field required")); jQuery('html, body').animate({ scrollTop:

Validate radio buttons AngularJS

Spencer: This seems like it should be easy, but I didn't find the answer. I have a form in which I need to confirm if a selection has been made from a broadcast group. I tried using the 'required' attribute on the radio buttons, but after validating the form i

Validate dynamic radio buttons

Reez0 I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected. remove.html {%for laptop in laptops%} <form action = "{%url 'laptops:delete'%}" method = "post"

Validate dynamic radio buttons

Reez0 I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected. remove.html {%for laptop in laptops%} <form action = "{%url 'laptops:delete'%}" method = "post"

Validate dynamic radio buttons

Reez0 I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected. remove.html {%for laptop in laptops%} <form action = "{%url 'laptops:delete'%}" method = "post"

Validate dynamic radio buttons

Reez0 I would like to know how to check if a radio button is selected before the user clicks submit. Currently, submissions are allowed if nothing is selected. remove.html {%for laptop in laptops%} <form action = "{%url 'laptops:delete'%}" method = "post"

How to validate radio buttons?

Dean Roman I am new to coding and I have some radio buttons that need to be validated without using jquery. if(document.getElementById('GCSE').checked){ examLevel = "GCSE"; } if(document.getElementById('AS').checked){ examLevel = "AS"; } if(document.getElement

Validate radio buttons with JavaScript

username I have 2 radio buttons. I need to validate radio buttons using javascript. Please tell me what is wrong with my code. Here is the code. $(function() { $("#XISubmit").click(function(){ var XIGender= document.forms["XIForm"]["XIGender"].val

Validate radio buttons in jQuery

David I'm using the following to make sure the fields are not null before posting in jquery: if(this.field.value == "") { jQuery( ".page-error-message" ).remove(); jQuery(".top").append(jQuery("field required")); jQuery('html, body').animate({ scrollTop:

jQuery dynamic radio buttons

Satya I am trying to create radio buttons in a container. value selected from a Combobox in another container to provide the total number of radio buttons, when I try to do this I only get one radio button shown even if the number provided is greater than 1 an