I want to make my text field required from a group of text field so that if any one is filled then the form will submit
<div class="row">
<div class="border border-dark d-flex">
<p class="d-inline ps-1">l.</p>
<p class="d-inline ps-4">Certified copy of OVD or equivalent e-document of OVD or OVD obtained through digital KYC process needs to be submitted (any one of the following OVDs)</p>
</div>
</div>
<div class="row">
<div class="border border-dark col-md-6 ps-4">
<input type="checkbox" name="OVD" class="check d-inline" required>
<h4 class="d-inline">A. Passport Number</h4>
</div>
<div class="border border-dark col-md-6">
<input type="text" class="input form-control border-0" name="ovd">
</div>
</div>
<div class="row">
<div class="border border-dark col-md-6 ps-4">
<input type="checkbox" name="OVD" class="check d-inline" required>
<h4 class="d-inline">B. Voter ID Card</h4>
</div>
<div class="border border-dark col-md-6">
<input type="text" class="input form-control border-0" name="ovd">
</div>
</div>
<div class="row">
<div class="border border-dark col-md-6 ps-4">
<input type="checkbox" name="OVD" class="check d-inline" required>
<h4 class="d-inline">C. Driving Licence</h4>
</div>
<div class="border border-dark col-md-6">
<input type="text" class="input form-control border-0" name="ovd">
</div>
</div>
<input type="submit">
i want my form will submit if any of the field filled
You can remove "required" from your input elements.
Instead of going with type="submit" , you will need to go with
<button type="submit" onClick="check()"> Send </button>
Now, you will need to create some javascript function that checks if any of the input fields are filled, something like this:
<script>
//first you need to take input fields inside variables
var passportNumber = document.getElementsByName("OVD")[0].value; //cos it returns a list
var IDcard = document.getElementsByName("ovd")[0].value; //like this for each field
function check(){
if(passportNumber || IDcard) //if ANY element has a value
return true;
else
return false;
}
</script>
Note: If you are beginner, you can add this code inside of Head HTML element, but better practice is to create an external JS file, and then call it.
Related
Am working on the frontend part of an application whereby I have
some form inputs (wrapped in divs). Each div has a button called
Add Beneficiary. Basically when the user clicks on the button, the div
below it should be displayed (which is hidden by default) and the
sequence continues (each button on a div clicked the div below it
displays).
On the button of each div I have writen some logic whereby on click event
a function called addBeneficiary is triggered. I parse the id of the
button clicked. Next in the function I manipulate the DOM by reaching
out to the parent div and displaying it but the logic does not
work.
~ Kindly assist?
Markup
<!--Person One-->
<div class="container mg-t-30" id="beneficiary1">
<div class="row col-11 mx-auto">
<div class="col-12">
<div class="row">
<div class="col-6">
<label> Category</label>
<input type="email" class="form-control" placeholder="Enter email">
</div>
<div class="col-6">
<label>Family Size</label>
<input type="password" class="form-control" placeholder="Password">
</div>
</div>
<div class="row">
<div class="col-12">
<button class="float-right btn btn-primary" id="btnBen1" onclick="addBeneficiary(this.id)">Add Beneficiary</button>
</div>
</div>
</div>
</div>
</div>
<!--Person Two-->
<div class="container mg-t-30" style="display:none;" id="beneficiary1">
<div class="row col-11 mx-auto">
<div class="col-12">
<div class="row">
<div class="col-6">
<label> Category</label>
<input type="email" class="form-control" placeholder="Enter email">
</div>
<div class="col-6">
<label>Family Size</label>
<input type="password" class="form-control" placeholder="Password">
</div>
</div>
<div class="row">
<div class="col-12">
<button class="float-right btn btn-primary" id="btnBen1" onclick="addBeneficiary(this.id)">Add Beneficiary</button>
</div>
</div>
</div>
</div>
</div>
Logic
function addBeneficiary(beneficiary){
let check = $('#' + beneficiary).parents('div').last().prop('id');
//console.log(check);
$("check").css("display", "block");
}
Its not working because both parents div id is "beneficiary1". And you can't use same id with multiple tag. It should be unique like beneficiary1, beneficiary2 & so on... And also little bit change into your script.
function addBeneficiary(beneficiary){
let check = $('#' + beneficiary).parents('div').next('div');
//console.log(check);
check.css("display", "block");
}
jsfiddle https://jsfiddle.net/y2mva6es/
I am showing the values base on user enters in inputbox So it is in format of array of arrays I mean array inside arrays for example(['str',[]] So I am looping in my component.html file and render data in input box So my issue is if array values are empty So i need to show empty input box which is not happening my case How can i keep a check so that if value is empty also I can show input box
<div class="row">
<span class="col-2 align-self-center firstletter">{{ a[0] }}</span>
<div class="col-4"><label for="startTime">Start Time
<span class="red-star">*</span>
</label></div>
<div class="col-4"><label for="endTime">End Time
<span class="red-star">*</span>
</label></div>
</div>
<div class ="test" *ngIf="a[1].length == 0">
<p>No opening hours</p>
</div>
<div class="row" *ngFor="let m of a[1]; let j = index">
<span class="col-2 align-self-center"></span>
<div class="col-4">
<input type="text" name="startTime"
class="form-control cust-input-class form-control-sm mt-1 col-10 d-inline" [value]="m[0] === undefined ? '': m[0] "
(change)="onStartChangeValue($event.target.value,m, m[0])" />
<!-- [ngModel]="m?.startTime" -->
</div>
<div class="col-4">
<input type="text" name="endTime"
class="form-control cust-input-class form-control-sm mt-1 col-10 d-inline" [value]="m[1] === undefined ? '':m[1]"
(change)="onEndChangeValue($event.target.value,m, m[1])" />
<!-- [ngModel]= "m?.endTime" (ngModelChange)="m.endTime = $event" -->
</div>
<div class="col-2 align-self-center" *ngIf="j == 0">
<span class="add-item-icon" (click)="o2editaddTime(k)"></span>
</div>
<div class="col-2 align-self-center" *ngIf="j > 0">
<span class="remove-item-icon" (click)="o2editremoveTime(k, j)"></span>
</div>
</div>
<!-- </div> -->
</div>
</div>
You can put the two input box with opposite condition
<input type="text" name="hey" *ngIf="array.array">{{ Any thing }}
<input type="text" name="hey" *ngIf="!array.arrayy" >
I have different file inputs I want displayed once the previous one has been selected. At the moment I have
<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="input-group inputMargBtm">
<input type="text" class="form-control" id="fileOneContainer" readonly>
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Browse… <input type="file" id="fileOne" name="fileOne" class="fileGroup" accept=".jpeg,.jpg">
</span>
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="filePadd">
<div class="input-group">
<div id="hideDivTwo" class="hiddenDiv">
<input type="text" class="form-control" id="fileTwoContainer" readonly>
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Browse… <input type="file" id="fileTwo" name="fileTwo" class="fileGroup" accept=".jpeg,.jpg" disabled="true">
</span>
</span>
</div>
</div>
</div>
</div>
</div>
But I also have a lot more hidden rows for up to 10 inputs. So I dont need to create new code for each input within my Javascript, I am trying to display things dynamically. Essentially, if I select a file, it should display the next file input.
At the moment I have this
$(".fileGroup").on('change',function(){
var id = $(this).attr('id');
if (id.length) {
$(this).parents('.row').nextSibling('.row').child('.hiddenDiv').css("display", "table-footer-group");
}
});
So I get the id of the changed fileGroup. I then attempt to get its parents row, then get the next row, and display its hidden div. At the moment this does not seem to work. What would be the best way to display the next input block?
Thanks
Try with this code:
$(".fileGroup").on('change',function(){
var $next_element = $(this).parents('.row').next('.row').find('input[type="file"]');
if ($next_element.length) {
$next_element.prop('disabled', false);
}
});
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="input-group inputMargBtm">
<input type="text" class="form-control" id="fileOneContainer" readonly>
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Browse… <input type="file" id="fileOne" name="fileOne" class="fileGroup" accept=".jpeg,.jpg">
</span>
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="filePadd">
<div class="input-group">
<div id="hideDivTwo" class="hiddenDiv">
<input type="text" class="form-control" id="fileTwoContainer" readonly>
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Browse… <input type="file" id="fileTwo" name="fileTwo" class="fileGroup" accept=".jpeg,.jpg" disabled="true">
</span>
</span>
</div>
</div>
</div>
</div>
</div>
The code is rather simple:
1. grab .row parent
2. look for the next element with .row class
3. find an file input in the next .row element
4. if the input exists, set its disabled attribute to
false
I am using repeater.js to repeat a div with some input fields and button, but the problem is that i want to give each and every div a unique id so that i can extract data from all the fields of that particular div input elements.
this is the HTML Code:
<div class="form-group mt-repeater">
<a href="javascript:;" data-repeater-create class="btn btn-circle purple mt-repeater-add">
<i class="fa fa-edit"></i> Repeat Field
</a>
<div data-repeater-list="group-b">
<div data-repeater-item class="mt-repeater-item">
<div class="mt-repeater-cell">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" id="dosage_form" class="form-control typeahead-findings circle-input" placeholder="Dosage Form">
<label for="dosage_form">e.g. Tablet / Syrup / Capsule etc.</label>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" id="brand_name" class="form-control typeahead-findings circle-input" placeholder="Brand / Generic Name">
<label for="brand_name">e.g. Dytor</label>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input class="form-control circle-input" id="contents" type="text" placeholder="Contents" />
<span class="help-block"> e.g Paracetamol </span>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" id="dose" class="form-control typeahead-findings circle-input" placeholder="Dose">
<label for="dose">e.g. 20 mg / 20 ml</label>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input class="form-control circle-input" id="dose_regimen" type="text" placeholder="Dose Regimen" />
<span class="help-block"> e.g 1-1-1-1 </span>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" id="therapy_duration" class="form-control typeahead-findings circle-input" placeholder="Therapy Duration">
<label for="therapy_duration">e.g. 1 Day / 1 Month etc.</label>
</div>
</div>
</div>
<div class="col-md-1 col-sm-6 col-xs-6">
<i class="fa fa-plus"></i>
</div>
<div class="col-md-1 col-sm-6 col-xs-6">
<i class="fa fa-minus"></i>
</div>
</div>
</div>
</div>
every time i click on the add repater button this div gets replicated but what i also want is that the new div generated should have a unique div id also the curresponding elemnts inside it should have a class or id starting with that id so that data extraction can be easy for that particular div
I was using this Jquery code:
$(".addData").on('click' ,function()
{
alert("hi");
});
This is the repeater i am using: https://pastebin.com/n7g7tdTH
but this code only runs for the very first div on the later generated dynamic divs it doesn't run. I also tried editing the repeater.js but no success because its all dynamic and i am not able to understand where i should edit that code to add a dynamic data-counter value for the div and the corresponding elements.
Can anyone help with this logic?
Another option, to retrieve field values would be to rename all input field id with name (example below) and use repeater.js repeaterVal() to get all the field values .
$(".mt-repeater").repeater();
$("#getVal").click(function () {
$('.mt-repeater').repeaterVal()["group-b"].map(function(fields){
//fields contain the collection of input fields per row
console.log(fields["dosage_form"]);
console.log(fields["brand_name"]);
//....
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.repeater/1.2.1/jquery.repeater.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<div class="form-group mt-repeater">
<a href="javascript:;" data-repeater-create class="btn btn-circle purple mt-repeater-add">
<i class="fa fa-edit"></i> Repeat Field
</a>
<div data-repeater-list="group-b">
<div data-repeater-item class="mt-repeater-item">
<div class="mt-repeater-cell">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" name="dosage_form" class="form-control typeahead-findings circle-input" placeholder="Dosage Form">
<label for="dosage_form">e.g. Tablet / Syrup / Capsule etc.</label>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" name="brand_name" class="form-control typeahead-findings circle-input" placeholder="Brand / Generic Name">
<label for="brand_name">e.g. Dytor</label>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input class="form-control circle-input" nam="contents" type="text" placeholder="Contents" />
<span class="help-block"> e.g Paracetamol </span>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" name="dose" class="form-control typeahead-findings circle-input" placeholder="Dose">
<label for="dose">e.g. 20 mg / 20 ml</label>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input class="form-control circle-input" name="dose_regimen" type="text" placeholder="Dose Regimen" />
<span class="help-block"> e.g 1-1-1-1 </span>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<div class="input-group">
<input type="text" name="therapy_duration" class="form-control typeahead-findings circle-input" placeholder="Therapy Duration">
<label for="therapy_duration">e.g. 1 Day / 1 Month etc.</label>
</div>
</div>
</div>
<div class="col-md-1 col-sm-6 col-xs-6">
<i class="fa fa-plus"></i>
</div>
<div class="col-md-1 col-sm-6 col-xs-6">
<i class="fa fa-minus"></i>
</div>
</div>
</div>
</div>
<button id="getVal">Get Value</button>
For the first problem..
but this code only runs for the very first div on the later generated
dynamic divs it doesn't run
You can convert your jquery events into delegated events.. These allow you to catch events on DOM elements based on a selector, so when newer DOM elements are added these are caught too.
So -> $(".addData").on('click' ,function()
As a delegated event becomes -> $(document.body).on("click",".addData" ,function()
The second problem.
but how to get the values from the input field that are present near that button
This can be achieved by traversing up the DOM tree to a parent that surrounds the elements you want,. eg. .row or maybe mt-repeater-cell, as long as it's a DOM element that contains the elements you want to find. Here you can use jquery's closest selector, that will traverse up the tree until it finds the element your after.
So -> $(this).closest('.row').find('#dose_regimen').val()
The above will traverse up the DOM tree until it finds a .row class, we then find element with id dose_regimen etc,.. #dose_regimen and then finally get it's value with .val()
I got little problem with append error state. I got 7 radio inputs and after submit I got 7 error states under group.
Could some one help me figure out how to modify the code.
<form class="register-form">
<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 margin-top-20">
<h4 class="text-center"> TEST TEST</h4>
<div class="question-box">
<p class="margin-top-20"><span class="red">*</span>1. QUESTION.</p>
<div class="col-lg-6 col-lg-offset-3 form-group text-center question">
<div class="radio-item" >
<input type="radio" id="case1" name="case" value="0-50" required data-step2="1">
<label for="case1">0 - 50</label>
</div>
<div class="radio-item">
<input type="radio" id="case2" name="case" value="50-100" required data-step2="1">
<label for="case2">50 - 100</label>
</div>
<div class="radio-item">
<input type="radio" id="case3" name="case" value="100+" required data-step2="1">
<label for="case3">Over 100</label>
</div>
</div>
</div>
<div class="question-box">
<p class="margin-top-20"><span class="red">*</span>2. QUESTION</p>
<div class="col-lg-6 col-lg-offset-3 form-group text-center question">
<div class="radio-item">
<input type="radio" id="resell1" name="resell" value="1" required data-step2="1">
<label for="resell1">YES</label>
</div>
<div class="radio-item">
<input type="radio" id="resell2" name="resell" value="0" required data-step2="1">
<label for="resell2">NO</label>
</div>
</div>
</div>
<div class="question-box">
<p class="margin-top-20"><span class="red">*</span>3.QUESTION</p>
<div class="col-lg-6 col-lg-offset-3 form-group text-center question">
<div class="radio-item">
<input type="radio" id="export1" name="export" value="1" required data-step2="1">
<label for="export1">YES</label>
</div>
<div class="radio-item">
<input type="radio" id="export2" name="export" value="0" required data-step2="1">
<label for="export2">NO</label>
</div>
</div>
</div>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 margin-top-20 text-center">
<button id="submit-registration" type="submit" class="btn btn-success radius send" >Continue</button>
</div>
</form>
https://jsfiddle.net/13j34o0g/1
thx
I've changed a few things in your code and i think this might be what you are looking for.
Example here
The first code below will loop through you input's why the name attribute. that means it will only run it 3 times
$(".invalid-info").remove(); // Removes the error messages before we run the validation.
var group = {};
$('input[name^="case"], input[name^="resell"], input[name^="export"]').each(function(index) {
var $this = $(this);
var name = this.name;
if (!group[name]) {
group[name] = true;
if (!testRadio($this, options.showValidOnCheck)) validated = false;
}
});
And i changed.
var value = $form.find('input[name="' + name + '"]').is(":checked");
if (!value) {
$('<p class="invalid-info" style="color: red">Please choose corect answer</p>').appendTo($($input).parent().parent())
return false;
}
Let me know if this is what you were looking for.