Im developing Html Form where value will update using jquery as user input the value.
When you enter value in Enter Your Value field it will instantly update its price in Price field
Now i want to add all field sum which will be field 1,field 2,field 3,field 4,field 5 and field 6 and display the total sum in Total Amount but total calculation of the all field are not updating in Total Amount.
Html Code
<form action="" method="post">
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<input type="text" class="form-control" name="fullname" placeholder="FUll Name">
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<input type="email" class="form-control" name="email" placeholder="Your Email">
</div>
</div>
<div class="col-md-6">
<div class="mb-3 menier">
<h4>Iron Bark:</h4>
$210 Per Cubic Metre
<input type="number" id="ironbark" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
Price
<input id="ironbarvalue" type="text" name="ironbark" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6 ">
<div class="mb-3 menier">
<h4>Red Gum:</h4>
$200 Per Cubic Metre
<input type="number" id="redgum" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
<span class="sammy">Price </span>
<input type="text" id="redgumvalue" name="redgum" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6">
<div class="mb-3 menier">
<h4>Mixed Gum:</h4>
$100 Per Cubic Metre
<input type="number" id="mixedgum" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
Price <input id="mixedgumvalue" type="text" name="mixedgum" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6">
<div class="mb-3 menier">
<h4>Brown Bark:</h4>
$110 Per Cubic Metre <input id="brownbark" type="number" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
Price <input id="brownbarkvalue" type="text" name="brownbark" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6 mt-2">
<div class="mb-3">
<input type="checkbox" class="form-check-input" id="stackfee" name="stackingfee" value="25">
<label class="form-check-label" for="exampleCheck1">Stacking Fee: $25 Per m3 </label>
</div>
</div>
<div class="col-md-6 mt-2">
<div class="mb-3">
<input type="checkbox" class="form-check-input" id="delivery" name="deliveryfee" value="50">
<label class="form-check-label" for="exampleCheck1">Delivery Fee: $50 Minimum </label>
</div>
</div>
<div class="col-md-12 mb-3">
<strong class="">
Total Amount: $
</strong>
<input type="number" class="form-control total-amount" id="sum" name="totalsum" disabled>
</div>
<div class="col-md-12">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Jquery code
$(document).ready(function(){
$('#ironbark').change(function(){
var ironbarkrate = parseFloat($('#ironbark').val()) || 0;
$('#ironbarvalue').attr('value', 210 * ironbarkrate);
});
$('#redgum').change(function(){
var redgum = parseFloat($('#redgum').val()) || 0;
$('#redgumvalue').attr('value', 200 * redgum);
});
$('#mixedgum').change(function(){
var mixedgum = parseFloat($('#mixedgum').val()) || 0;
$('#mixedgumvalue').attr('value', 100 * mixedgum);
});
$('#brownbark').change(function(){
var brownbark = parseFloat($('#brownbark').val()) || 0;
$('#brownbarkvalue').attr('value', 110 * brownbark);
});
$('#ironbarvalue, #redgumvalue,#mixedgumvalue,#brownbarkvalue').change(function(){
var value1 = parseFloat($('#ironbarvalue').val()) || 0;
var value2 = parseFloat($('#redgumvalue').val()) || 0;
var value3 = parseFloat($('#mixedgumvalue').val()) || 0;
var value4 = parseFloat($('#brownbarkvalue').val()) || 0;
var value5 = parseFloat($('#stackfee').val()) || 0;
var value6 = parseFloat($('#delivery').val()) || 0;
$('#sum').attr('value',value1 + value2 + value3 + value4 + value5 + value5);
});
});
Im Trying to get All sum on Total Amount… but im notable to get Sum display in Total Amount, isnt because Price field is generate by Jquery
You need to create a common function to calculate the sum and you need to call it in each input change and checkbox click event like below:
Working example:
$(document).ready(function() {
$('#ironbark').change(function() {
$('#ironbarvalue').val(210 * (parseFloat($(this).val()) || 0));
calculateSum();
});
$('#redgum').change(function() {
var redgum = parseFloat($('#redgum').val()) || 0;
$('#redgumvalue').val(200 * (parseFloat($(this).val()) || 0));
calculateSum();
});
$('#mixedgum').change(function() {
$('#mixedgumvalue').val(100 * (parseFloat($(this).val()) || 0));
calculateSum();
});
$('#brownbark').change(function() {
$('#brownbarkvalue').val(110 * (parseFloat($(this).val()) || 0));
calculateSum();
});
$('#stackfee').click(function() {
calculateSum();
});
$('#delivery').click(function() {
calculateSum();
});
});
function calculateSum() {
var value1 = parseFloat($('#ironbarvalue').val()) || 0;
var value2 = parseFloat($('#redgumvalue').val()) || 0;
var value3 = parseFloat($('#mixedgumvalue').val()) || 0;
var value4 = parseFloat($('#brownbarkvalue').val()) || 0;
var value5 = ($('#stackfee').is(":checked")) ? parseFloat($('#stackfee').val()) : 0;
var value6 = ($('#delivery').is(":checked")) ? parseFloat($('#delivery').val()) : 0;
$('#sum').val(value1 + value2 + value3 + value4 + value5 + value6);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="post">
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<input type="text" class="form-control" name="fullname" placeholder="FUll Name">
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<input type="email" class="form-control" name="email" placeholder="Your Email">
</div>
</div>
<div class="col-md-6">
<div class="mb-3 menier">
<h4>Iron Bark:</h4>
$210 Per Cubic Metre
<input type="number" id="ironbark" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
Price
<input id="ironbarvalue" type="text" name="ironbark" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6 ">
<div class="mb-3 menier">
<h4>Red Gum:</h4>
$200 Per Cubic Metre
<input type="number" id="redgum" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
<span class="sammy">Price </span>
<input type="text" id="redgumvalue" name="redgum" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6">
<div class="mb-3 menier">
<h4>Mixed Gum:</h4>
$100 Per Cubic Metre
<input type="number" id="mixedgum" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
Price <input id="mixedgumvalue" type="text" name="mixedgum" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6">
<div class="mb-3 menier">
<h4>Brown Bark:</h4>
$110 Per Cubic Metre <input id="brownbark" type="number" class="form-control" placeholder="Enter Your Value">
<div class="mt-3">
Price <input id="brownbarkvalue" type="text" name="brownbark" class="total-amount form-control" readonly>
</div>
</div>
</div>
<div class="col-md-6 mt-2">
<div class="mb-3">
<input type="checkbox" class="form-check-input" id="stackfee" name="stackingfee" value="25">
<label class="form-check-label" for="exampleCheck1">Stacking Fee: $25 Per m3 </label>
</div>
</div>
<div class="col-md-6 mt-2">
<div class="mb-3">
<input type="checkbox" class="form-check-input" id="delivery" name="deliveryfee" value="50">
<label class="form-check-label" for="exampleCheck1">Delivery Fee: $50 Minimum </label>
</div>
</div>
<div class="col-md-12 mb-3">
<strong class="">
Total Amount: $
</strong>
<input type="number" class="form-control total-amount" id="sum" name="totalsum" disabled>
</div>
<div class="col-md-12">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
getting too many id is problematic, use $(document) to select all and use $('#sum').val() to enter the total value.
$(document).change(function(){
var value1 = parseFloat($('#ironbarvalue').val()) || 0;
var value2 = parseFloat($('#redgumvalue').val()) || 0;
var value3 = parseFloat($('#mixedgumvalue').val()) || 0;
var value4 = parseFloat($('#brownbarkvalue').val()) || 0;
var value5 = parseFloat($('#stackfee').val()) || 0;
var value6 = parseFloat($('#delivery').val()) || 0;
$('#sum').val(value1 + value2 + value3 + value4 + value5 + value5);
});
Related
I am building an ecommerce site. so, i created a billing form, and in that form, i want to collect stuffs like email, address etc and pass it into a json format so i'll pass it to the payment gateway.
I tried creating it in a variable and passing it, but on the payment gateway redirectedmodal form, it is saying invalid email input
Code
const publicKey = "{{ key }}";
var email = document.getElementById('email').value;
var fullname = document.getElementById('fullName').value;
var address1 = document.getElementById('custAdd').value;
var address2 = document.getElementById('custAdd2').value;
var country = document.getElementById('country').value;
var state = document.getElementById('state').value;
var address1 = document.getElementById('postCode').value;
function payWithRave() {
var x = getpaidSetup({
PBFPubKey: "xxxxxx",
email: email,
amount: '{{cart.get_total_price}}',
customer_phone: "234099940409",
currency: "USD",
address: 'address1',
address2: 'address2',
country: 'country',
state: 'state',
postcode: 'postCode',
})
<div class="col-sm-7">
<label for="firstName" class="form-label">Full Name</label>
<input type="text" class="form-control" id="fullName" placeholder="" required>
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
<div class="col-12">
<label for="email" class="form-label">Email <span class="text-muted">(Optional)</span></label>
<input type="email" class="form-control" id="email" placeholder="you#example.com">
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
<div class="col-12">
<label for="address" class="form-label">Address</label>
<input type="text" class="form-control" id="custAdd" placeholder="1234 Main St" required>
<div class="invalid-feedback">
Please enter your shipping address.
</div>
</div>
<div class="col-12">
<label for="address2" class="form-label">Address 2 <span
class="text-muted">(Optional)</span></label>
<input type="text" class="form-control" id="custAdd2" placeholder="Apartment or suite">
</div>
<div class="col-md-5">
<label for="country" class="form-label">Country</label>
<select class="form-select" id="country" required>
<option value="">Choose...</option>
<option>United States</option>
</select>
<div class="invalid-feedback">
Please select a valid country.
</div>
</div>
<div class="col-md-4">
<label for="state" class="form-label">State</label>
<select class="form-select" id="state" required>
<option value="">Choose...</option>
<option>California</option>
</select>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3">
<label for="Postcode" class="form-label">Postcode</label>
<input type="text" class="form-control" id="postCode" placeholder="" required>
<div class="invalid-feedback">
Zip code required.
</div>
</div>
</div>
<hr class="my-4">
<button type="button" class="btn btn-primary w-100 fw-bold" onClick="payWithRave()">Pay ${{cart.get_total_price}}</button>
const publicKey = "{{ key }}";
const payWithRave = (ev) => {
ev.preventDefault();
let x = {
PBFPubKey: "xxxxxx",
fullname: document.getElementById('fullName').value,
email: document.getElementById('email').value,
amount: '{{cart.get_total_price}}',
customer_phone: "234099940409",
currency: "USD",
address: document.getElementById('custAdd').value,
address2: document.getElementById('custAdd2').value,
country: document.getElementById('country').value,
state: document.getElementById('state').value,
postcode: document.getElementById('postCode').value,
}
console.log(x);
}
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('btn').addEventListener('click', payWithRave);
})
<!DOCTYPE html>
<html>
<body>
<div class="col-sm-7">
<label for="firstName" class="form-label">Full Name</label>
<input type="text" class="form-control" id="fullName" placeholder="" required>
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
<div class="col-12">
<label for="email" class="form-label">Email <span class="text-muted">(Optional)</span></label>
<input type="email" class="form-control" id="email" placeholder="you#example.com">
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
<div class="col-12">
<label for="address" class="form-label">Address</label>
<input type="text" class="form-control" id="custAdd" placeholder="1234 Main St" required>
<div class="invalid-feedback">
Please enter your shipping address.
</div>
</div>
<div class="col-12">
<label for="address2" class="form-label">Address 2 <span
class="text-muted">(Optional)</span></label>
<input type="text" class="form-control" id="custAdd2" placeholder="Apartment or suite">
</div>
<div class="col-md-5">
<label for="country" class="form-label">Country</label>
<select class="form-select" id="country" required>
<option value="">Choose...</option>
<option>United States</option>
</select>
<div class="invalid-feedback">
Please select a valid country.
</div>
</div>
<div class="col-md-4">
<label for="state" class="form-label">State</label>
<select class="form-select" id="state" required>
<option value="">Choose...</option>
<option>California</option>
</select>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-3">
<label for="Postcode" class="form-label">Postcode</label>
<input type="text" class="form-control" id="postCode" placeholder="required">
<div class="invalid-feedback">
Zip code required.
</div>
</div>
<hr class="my-4">
<button type="button" id="btn" class="btn btn-primary w-100 fw-bold">Pay ${{cart.get_total_price}}</button>
</body>
</html>
strong text
I have this form:
<div class="modal-body">
<form action="repository/add_new_task.php">
<div class="form-group">
<label for="street" class="col-form-label">Street</label>
<input type="text" class="form-control" id="street">
</div>
<div class="form-group">
<label for="phone" class="col-form-label">Phone</label>
<input type="text" class="form-control" id="phone">
</div>
<div class="form-group">
<label for="date" class="col-form-label">Date</label>
<div class="input-group date" id="datetimepicker" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" id="date" data-target="#datetimepicker">
<div class="input-group-append" data-target="#datetimepicker" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
<div class="form-group">
<label for="price" class="col-form-label">Price</label>
<input type="text" class="form-control" id="price">
</div>
<div class="form-group">
<label for="comment" class="col-form-label">Comment</label>
<textarea type="text" class="form-control" rows="3" id="comment"></textarea>
</div>
<div class="form-group">
<label for="job-type" class="col-form-label">Comment</label>
<select class="form-control" id="job-type">
<option value="0">Repair</option>
<option value="1">Multistory Pull In</option>
<option value="2">Multistory Weld</option>
<option value="3">Private Residence</option>
</select>
</div>
<button type="submit" class="btn btn-primary btn-block" name="new-task-submit">Create task</button>
</form>
</div>
All I need to do is to make button disabled until all the fields (including datepicker and textarea) have values in them.
I tried following the answer in this post and while it worked I struggles to make it work for my datepicker and textarea. Can someone please help me figure this out
With just css you can use pointer events and make the button unclickable.
form:invalid [type="submit"] {
pointer-events: none;
border: 1px solid #999999;
background-color: #CCCCCC;
color: #666666;
}
<form>
<input type="text" required />
<input type="text" required />
<input type="text" required />
<input type="submit" />
You can use oninput and .disabled like so:
var field1 = document.getElementById("date");
var field2 = document.getElementById("phone");
var field3 = document.getElementById("price");
var field4 = document.getElementById("comment");
var field5 = document.getElementById("job-type");
var button = document.getElementById("btn");
function validate(){
if(!isEmpty(field1)&&!isEmpty(field2)&&!isEmpty(field3)&&!isEmpty(field4)&&!isEmpty(field5)){
btn.disabled=false;
}else{
btn.disabled=true;
}
}
function isEmpty(element){
if(element.value.length==0){
return true;
}else{
return false;
}
}
<div class="modal-body">
<form action="repository/add_new_task.php">
<div class="form-group">
<label for="street" class="col-form-label">Street</label>
<input type="text" class="form-control" id="street" oninput="validate()">
</div>
<div class="form-group">
<label for="phone" class="col-form-label">Phone</label>
<input type="text" class="form-control" id="phone" oninput="validate()">
</div>
<div class="form-group">
<label for="date" class="col-form-label">Date</label>
<div class="input-group date" id="datetimepicker" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" id="date" data-target="#datetimepicker" oninput="validate()">
<div class="input-group-append" data-target="#datetimepicker" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
<div class="form-group">
<label for="price" class="col-form-label">Price</label>
<input type="text" class="form-control" id="price" oninput="validate()">
</div>
<div class="form-group">
<label for="comment" class="col-form-label">Comment</label>
<textarea type="text" class="form-control" rows="3" id="comment" oninput="validate()"></textarea>
</div>
<div class="form-group">
<label for="job-type" class="col-form-label">Comment</label>
<select class="form-control" id="job-type" onchange="validate()">
<option value="0">Repair</option>
<option value="1">Multistory Pull In</option>
<option value="2">Multistory Weld</option>
<option value="3">Private Residence</option>
</select>
</div>
<button type="submit" id="btn" class="btn btn-primary btn-block" name="new-task-submit" disabled>Create task</button>
</form>
</div>
If the user has not entered any text into any of the fields, the button will be disabled automatically.
I'm making a sign up form for a web application, and I'm trying to make certain fields required, and one of the radio buttons pre-checked, but neither the "required" nor the "checked" input tags seem to have any effect. I am using Firefox as my browser and bootstrap css classes.
When I take out all the Angular related code (the ngForm, ngModel and *ngIf error messages) and just open it in the browser like pure html/css, the radio button becomes checked and the required field do act as they should. So I must have made some logical mistake with my Angular code, which i am fairly new to.
<div class="container">
<div class="row">
<div class="col-12">
<h1>Registration</h1>
</div>
</div>
<form #regForm="ngForm" method="POST" (ngSubmit)= "registrate()">
<div class="row">
<div class="col-sm-4">
<label for="firstName">*First name:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="firstName"
class="form-control"
name = "firstName"
#firstName = "ngModel"
[(ngModel)] = "user.firstName"
required/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="lastName">*Last name:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="lastName"
class="form-control"
name = "lastName"
#lastName = "ngModel"
[(ngModel)] = "user.lastName"
required/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="email">*E-mail:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="email"
class="form-control"
name = "email"
#email = "ngModel"
[(ngModel)] = "user.email"
required/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="occupation">Occupation:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="occupation"
class="form-control"
name = "occupation"
[(ngModel)] = "user.occupation"/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="username">*Username:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="username"
class="form-control"
name = "username"
#username = "ngModel"
[(ngModel)] = "user.username"
required/>
</div>
</div>
<div class="row" *ngIf="regForm.submitted && nameExists">
<div class="col-sm-6" style="margin: auto">
<p style="color: red; text-align: center">The desired username already exists</p>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="password1">*Password:</label>
</div>
<div class="col-sm-8">
<input
type="password"
id="password1"
class="form-control"
name = "password1"
pattern="^(?=.{8,12}$)(?!.*(\S)\1{2})(?=.*[A-Z])(?=.*[a-z]{3})(?=.*\d)(?=.*[^a-zA-Z0-9])([a-zA-Z]\S*)$"
#password1 = "ngModel"
[(ngModel)] = "user.password1"
required/>
</div>
</div>
<div class="row" *ngIf="regForm.submitted && password1?.errors.pattern">
<div class="col-sm-6" style="margin: auto">
<p style="color: red; text-align: center">Bad password!</p>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="password2">*Re-enter your password:</label>
</div>
<div class="col-sm-8">
<input
type="password"
id="password2"
class="form-control"
name = "password2"
#password2 = "ngModel"
[(ngModel)] = "checkPassword"
required/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label>Gender:</label>
</div>
<div class="col-sm-4">
<label for="male">male:</label>
<input
type="radio"
name="gender"
id="male"
value="male"
checked
#gender1 = "ngModel"
[(ngModel)] = "user.gender"/>
</div>
<div class="col-sm-4">
<label for="female">женски:</label>
<input
type="radio"
name="gender"
id="female"
value="female"
#gender2 = "ngModel"
[(ngModel)] = "user.gender"/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="idNumber">*ID number:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="idNumber"
class="form-control"
name = "idNumber"
#idNumber = "ngModel"
[(ngModel)] = "user.idNumber"
required/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="question">*Security question:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="question"
class="form-control"
name = "question"
#question = "ngModel"
[(ngModel)] = "user.question"
required/>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<label for="answer">*Answer:</label>
</div>
<div class="col-sm-8">
<input
type="text"
id="answer"
class="form-control"
name = "answer"
#answer = "ngModel"
[(ngModel)] = "user.answer"
required/>
</div>
</div>
<div class="row" *ngIf="regForm.submitted && (firstName.invalid || lastName.invalid || email.invalid || username.invalid || password1.invalid || password2.invalid || idNumber.invalid || question.invalid || answer.invalid)">
<div class="col-sm-6" style="margin: auto">
<p style="color: red; text-align: center">Input fields marked with * must be filled!</p>
</div>
</div>
<div class="row">
<div class="col-12" style="text-align: center">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
Note that my .ts class has these fields:
user is an object with all the fields used here,
checkPassword is a string used to match passwords,
nameExists is a boolean that is set to false,
registrate() is a function that does nothing for now.
These attributes (required and checked) in angular should be used as shown in below examples..
<input type="text" id="test2" name="test2" [(ngModel)]="test" [required]="true">
and
<input type="checkbox" [checked]="true" />
Solved the checked tag by setting the starting value of user.gender to "male". It was an empty string by default and checked tag seems to have been canceled out by it.
required tag seems to be working from Angular's perspective (ngModels are invalid), just does not have the "normal" html behavior - the inputs don't get a red border, and the usual "Please fill out this field" message doesn't show. Is this expected?
<script>
$(document).ready(function() {
$("#frm_details").on("submit", function(event) {
event.preventDefault();
var data = $(this).serialize()
$.ajax({
url : "/saveUser",
type : "post",
contentType : "application/json",
data: JSON.stringify({ user: user, employee: employee, address: address }),
dataType : 'json',
success : function(d) {
location.replace("index.html");
}
});
});
});
This is HTML Registration form
<html lang="en"> <head> <title>Registration Form</title> </head>
<body> <div class="container"> <h1 class="well">Registration
Form</h1> <div class="col-lg-12 well"> <div class="row">
<form name="frm_details" id="frm_details" method="post">
<div class="row">
<div class="col-sm-6 form-group">
<label>First Name*</label> <input type="text" name="employeeName"
id="employeeName" placeholder="Enter First Name Here.."
class="form-control" required>
</div>
<div class="col-sm-6 form-group">
<label>Last Name*</label> <input type="text"
name="employeeSurName" id="employeeSurName"
placeholder="Enter Last Name Here.." class="form-control"
required>
</div>
</div>
<div class="row">
<div class="col-sm-6 form-group">
<label>Birth Date*</label>
<div class="input-append date">
<input type="text" class="form-control datepicker"
name="birthDay" id="birthDay"
placeholder="Enter Birth Date Here.." required>
</div>
</div>
<div class="col-sm-6 form-group">
<label>Mobile Number*</label> <input type="text"
placeholder="Enter Mobile Number Name .." class="form-control"
name="mobile" id="mobile" required>
</div>
</div>
<div class="row">
<div class="col-sm-4 form-group">
<label>Email Address*</label> <input type="email"
placeholder="Enter Email Address Here.." class="form-control"
name="userName" id="userName"
data-error="Please enter valid email address" required>
</div>
<div class="col-sm-4 form-group">
<label>Password*</label> <input type="password" id="password"
placeholder="Password" class="form-control" name="password"
data-minlength="6" required>
<div class="help-block">Minimum of 6 characters</div>
</div>
<div class="col-sm-4 form-group">
<label>Re-Password*</label> <input type="password"
data-match="#password" id="re_password" name="re_password"
data-match-error="Whoops,these don't match"
placeholder="Confirm" required class="form-control" required>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-sm-4 form-group">
<label>House No*</label> <input type="text"
placeholder="Enter House Number Here.." class="form-control"
name="house_no" id="house_no">
</div>
<div class="col-sm-4 form-group">
<label>STREET NO</label> <input type="text"
placeholder="Street Number" class="form-control"
data-error="Street Number required" name="street_Name"
id="street_Name" required>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-sm-4 form-group">
<label>State*</label> <select id="state" class="form-control"
data-error="State required" name="state" id="state" required>
<option value="" selected="selected">-- Select State--</option>
</select>
<div class="help-block with-errors"></div>
</div>
<div class="col-sm-4 form-group">
<label>City*</label> <select class="form-control" id="city"
name="city" data-error="City required" required>
<option value="" selected="selected">-- Select City--</option>
</select>
<div class="help-block with-errors"></div>
</div>
<div class="col-sm-4 form-group">
<label> Pin Code*</label> <input type="text" id="zip" name="zip"
placeholder="Enter Pin Code Number Here.."
data-error="Zip required" class="form-control" required>
</div>
<div class="help-block with-errors"></div>
</div>
<button type="submit" class="btn btn-lg btn-info">Submit</button>
<button type="reset" class="btn btn-lg btn-info">RESET</button>
</form> </div> </div> </div> <script type="text/javascript"> $('input.datepicker').each(function() {
var datepicker = $(this);
datepicker.bootstrapDatePicker(datepicker.data()); }); </script>
This controller class
#RequestMapping("/saveUser")
public String saveUser(#RequestBody String json) {
try {
if (!StringUtils.isEmpty(json))
return "1";
else
return "0";
} catch (Exception ex) {
return "1";
}
}
Iam getting this type result in json :
employeeName=Rajesh+XYZ&employeeSurName=xyz&birthDay=&mobile=213333123&userName=rajesh_xyz%40gmail.com&password=ssss&re_password=www&house_no=www&street_Name=Flat+No%3AA-27&state=Maharashtra&city=Latur&zip=411019
I have already three pojo object employee ,user,address.How could I pass this three object from ajax to cotroller
You should probably create some sort of request wrapper or command object for this.
public class RegistrationCommand {
private String employeeName;
private String password;
private String address;
//etc...
}
Then you can bind that request like this:
#RequestMapping("/saveUser")
public String saveUser(#RequestBody RegistrationCommand request) {
}
If you want to keep you existing json structure then I'd probably do something like:
public class RegistrationCommand {
private User user;
private Employee employee;
private Address address;
//etc...
private static class User {
private String name;
private String password;
//etc...
}
// Employee & Address classes
}
I need to select at least one checkbox and one radio button with remaining fields.
Here with out selecting checkbox it is allowing to submit and if I select one checkbox then also it not allowing.
Please me to validate this form with least one checkbox and radio button with remaining fields are required
JSFiddle
<form name="addUserForm" novalidate>
<div class="modal-body">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="email">Email:</label>
<input class="form-control" type="email" id="email" name="email" ng-model="addUserFormData.email" ng-pattern="^\S+#(([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6})$" required placeholder="xxx#xxx.com">
<span class="error_text" ng-show="addUserForm.email.$error.required && addUserForm.email.$touched">Required</span>
<span class="error_text" ng-show="!addUserForm.email.$error.required && addUserForm.email.$error.email && addUserForm.email.$dirty">Invalid Email</span>
</div>
<div class="form-group">
<label for="username">Customer Name:</label>
<input class="form-control" type="text" id="username" name="username" ng-model="addUserFormData.username" ng-pattern="/^[\w -]*$/" ng-minlength="2" ng-maxlength="20" maxlength="20" required placeholder="Customer Name">
<span class="error_text" ng-show="addUserForm.username.$error.required && addUserForm.username.$touched">required</span>
<span class="error_text" ng-show="!addUserForm.username.$error.minLength && !addUserForm.username.$error.maxLength && addUserForm.username.$error.pattern && addUserForm.username.$dirty">Name must contain letters and space only.</span>
<span class="error_text" ng-show="!addUserForm.username.$error.required && (addUserForm.username.$error.minlength || addUserForm.username.$error.maxlength) && addUserForm.username.$dirty">Name must be between 2 and 20 characters.</span>
</div>
<div class="form-group">
<label>Role:</label><br />
<label class="radio-inline"><input type="radio" name="role" ng-model="addUserFormData.roleAdmin" ng-required="addUserFormData.roleAdmin || addUserFormData.roleUser">Admin</label>
<label class="radio-inline"><input type="radio" name="role" ng-model="addUserFormData.roleUser" ng-required="addUserFormData.roleAdmin || addUserFormData.roleUser">User</label>
</div>
<div class="form-group">
<label>Accessable To:</label>
<div class="clearfix"><!-- donot delete this -- clear both property --></div>
<div class="access_checkbox">
<label class="checkbox-inline">
<input type="checkbox" ng-model="addUserFormData.cStoreAnalytics" ng-required="addUserFormData.cStoreAnalytics || addUserFormData.cameraAnalytics || addUserFormData.wifiAnalytics">C Store Analytics
</label>
<input class="form-control mar_bot_5" type="text" ng-disabled="!addUserFormData.cStoreAnalytics" placeholder="Username">
<input class="form-control" type="password" ng-disabled="!addUserFormData.cStoreAnalytics" placeholder="Password">
</div>
<div class="access_checkbox">
<label class="checkbox-inline">
<input type="checkbox" ng-model="addUserFormData.cameraAnalytics" ng-required="addUserFormData.cStoreAnalytics || addUserFormData.cameraAnalytics || addUserFormData.wifiAnalytics" >Camera Analytics
</label>
<input class="form-control mar_bot_5" type="text" ng-disabled="!addUserFormData.cameraAnalytics" placeholder="Token Id">
</div>
<div class="access_checkbox">
<label class="checkbox-inline">
<input type="checkbox" ng-model="addUserFormData.wifiAnalytics" ng-required="addUserFormData.cStoreAnalytics || addUserFormData.cameraAnalytics || addUserFormData.wifiAnalytics" >Wifi Analytics
</label>
<input class="form-control mar_bot_5" type="text" ng-disabled="!addUserFormData.wifiAnalytics" placeholder="Username">
<input class="form-control" type="password" ng-disabled="!addUserFormData.wifiAnalytics" placeholder="Password">
</div>
<div class="clearfix"><!-- donot delete this -- clear both property --></div>
<p class="note_text">Make sure you have entered correct username and password</p>
</div>
<div class="form-group">
<label>Password:</label>
<input class="form-control" type="password" placeholder="Password">
</div>
<div class="form-group">
<label>Confirm password:</label>
<input class="form-control" type="password" placeholder="Confirm password">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" ng-disabled="addUserForm.$invalid" ng-click="addUser()" class="btn redcolor_btn"> <!--data-dismiss="modal"-->Add</button>
</div>
</form>
I think changing ng-required slightly will solve your problem.
So, instead of;
<input type="checkbox"
ng-model="addUserFormData.cStoreAnalytics"
ng-required="addUserFormData.cStoreAnalytics
|| addUserFormData.cameraAnalytics
|| addUserFormData.wifiAnalytics">
use this (apply same logic for all checkboxes):
<input type="checkbox"
ng-model="addUserFormData.cStoreAnalytics"
ng-required="!(addUserFormData.cStoreAnalytics
|| addUserFormData.cameraAnalytics
|| addUserFormData.wifiAnalytics)">