I have a simple HTML Form and i am trying to validate it using Jquery Validation but for some reason the form is not being validated. Please help.
HTML :
<div class="row">
<div class="col-md-8">
<form novalidate="novalidate" action="http://talentcapture.com/Profile/save_profile" id="update-profile-form" method="POST" class="validateForm">
<fieldset>
<legend>Update Your Profile Information</legend>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="first_name">First Name:</label>
</label>
<div class="col-md-8">
<input name="users[first_name]" value="Jhon" data-required="true" id="users-first_name" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="user_name">Last Name:</label>
</label>
<div class="col-md-8">
<input name="users[last_name]" value="Doe" data-required="true" id="users-last_name" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="street_address">Street Address:</label>
</label>
<div class="col-md-8">
<input name="user_profiles[street_address]" value="" data-required="true" id="user_profiles-street_address" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="locality">Locality:</label> </label>
<div class="col-md-8">
<input name="user_profiles[locality]" value="" data-required="true" id="user_profiles-locality" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="city">City:</label>
</label>
<div class="col-md-8">
<input name="user_profiles[city]" value="" data-required="true" id="user_profiles-city" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="state">State:</label> </label>
<div class="col-md-8">
<input name="user_profiles[state]" value="" data-required="true" id="user_profiles-state" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="country">Country:</label> </label>
<div class="col-md-8">
<input name="user_profiles[country]" value="" data-required="true" id="user_profiles-country" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="zipcode">Zipcode:</label> </label>
<div class="col-md-8">
<input name="user_profiles[zipcode]" value="" data-required="true" data-number="true" id="user_profiles-zipcode" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="company_name">Company name:</label>
</label>
<div class="col-md-8">
<input name="user_profiles[company_name]" value="" data-required="true" id="user_profiles-company_name" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="company_desc">Company Description:</label>
</label>
<div class="col-md-8">
<textarea name="user_profiles[company_desc]" cols="40" rows="10" type="textarea" data-required="true" id="user_profiles-company_desc" class="form-control"></textarea>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="linkedin_url">Linkedin url :</label>
</label>
<div class="col-md-8">
<input name="user_profiles[linkedin_url]" value="" data-required="true" id="user_profiles-linkedin_url" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">
<label for="company_website_url">Company Website:</label>
</label>
<div class="col-md-8"><input name="user_profiles[company_website_url]" value="" data-required="true" id="user_profiles-company_website_url" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</fieldset>
</form>
</div>
I have this common method to validate all my forms based on the data- attribute passed to the fields.
JAVASCRIPT
var form = $(".validateForm");
form.each(function(){
var validationRules = {};
var field = '';
$(this).find(':input').each(function(){
field = $(this).attr('id');
if(typeof field !== 'undefined')
validationRules[field]= $(this).data();
});
var error = $('.alert-error', $(this));
var success = $('.alert-success', $(this));
console.log(validationRules);
$(this).validate({
rules: validationRules,
errorClass : 'help-inline error-msg',
errorElement: 'span',
errorPlacement: function (error, element) { // render error placement for each input type
var type = $(element).prop('type');
if(type == 'checkbox' || type == 'radio'){
error.insertAfter(element.closest('.'+type));
error.css('float','none');
} else {
error.insertAfter(element); // for other inputs, just perform default behavoir
}
},
invalidHandler: function (event, validator) { //display error alert on form submit
success.hide();
error.show();
},
highlight: function (element) { // hightlight error inputs
$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
},
unhighlight: function (element) { // revert the change dony by hightlight
$(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set error class to the control group
},
});
});
however the Validation does not work and the form gets submitted on blank fields even though all fields are mandatory.
Related
I want to send a form via Ajax, not via a submit button.
I defined the form as follows in the below code excerpt, and later I defined the jQuery function to trap the CREATE BUTTON action.
When I debug this I get that
console.log($('#customer_form').serialize()); does not throw anything.
Is this the right way of serializing a form?
Do the buttons need to be inside the <form></form> element?
Here is the used code:
HTML:
<form id="customer_form" role="form">
<div class="form-group">
<label class="col-sm-4 control-label" for="name">Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="name" aria-describedby="name_help" placeholder="Name">
<small id="name_help" class="form-text text-muted"></small>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="address_line_1">Address</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="address_line_1" aria-describedby="address_line_1_help" placeholder="Address Line 1">
<small id="address_line_1_help" class="form-text text-muted"></small>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="address_line_2"></label>
<div class="col-sm-8">
<input type="text" class="form-control" id="address_line_2" aria-describedby="address_line_2_help" placeholder="Address Line 2">
<small id="address_line_2_help" class="form-text text-muted"></small>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="town">Town</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="town" aria-describedby="town_help" placeholder="Town">
<small id="town_help" class="form-text text-muted"></small>
</div>
</div>
<button id="create" class="btn btn-sm btn-primary pull-right m-t-n-xs" type="button"><strong>Save</strong></button>
</form>
JavaScript:
$(document).ready(function(){
$('#create').click(function(event) {
console.log('foo');
alert($('#customer_form').serialize());
event.preventDefault();
});
});
You just forgot to add the name attribute to your inputs:
<input name="nameofInput" .... />
-------^
See the below snippet:
$(document).ready(function(){
$('#create').click(function(event) {
console.log('foo');
alert($('#customer_form').serialize());
event.preventDefault();
});
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="customer_form" role="form">
<div class="form-group">
<label class="col-sm-4 control-label" for="name">Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="name" id="name" aria-describedby="name_help" placeholder="Name">
<small id="name_help" class="form-text text-muted"></small>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="address_line_1">Address</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="adress1" id="address_line_1" aria-describedby="address_line_1_help" placeholder="Address Line 1">
<small id="address_line_1_help" class="form-text text-muted"></small>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="address_line_2"></label>
<div class="col-sm-8">
<input type="text" class="form-control" name="adress2" id="address_line_2" aria-describedby="address_line_2_help" placeholder="Address Line 2">
<small id="address_line_2_help" class="form-text text-muted"></small>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="town">Town</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="town" id="town" aria-describedby="town_help" placeholder="Town">
<small id="town_help" class="form-text text-muted"></small>
</div>
</div>
<button id="create" class="btn btn-sm btn-primary pull-right m-t-n-xs" type="button"><strong>Save</strong></button>
</form>
I'm trying to make an appointment management using php and mysql
so a user can register himself by filling a form. I have added a checkbox naming Mon, ) .Tue, Thu ... and gave two fields time_from and time_to (start time and end ti). I will split these time and store it in my db.
The problem is what if user have different timings on different days of the week like someone might work 9 - 5 on Monday to Friday and 11-2 on Saturday.
I want to capture these values when a user registers. Can I add an optional field in my html form or is there any other way I can do this.
My current code
<form class="form-horizontal" role="form" name=fdadd method=post action=dsave.php>
<div class="form-group">
<label for="DoctorName" class="col-sm-3 control-label">Doctor Name</label>
<div class="col-sm-9">
<input type="text" id="firstName" name=name placeholder="Doctor Name" class="form-control" autofocus>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">Gender</label>
<div class="col-sm-6">
<div class="row">
<div class="col-sm-4">
<label class="radio-inline">
<input type="radio" name="radio" id="femaleRadio" value="Female">Female
</label>
</div>
<div class="col-sm-4">
<label class="radio-inline">
<input type="radio" name="radio" id="maleRadio" value="Male">Male
</label>
</div>
<div class="col-sm-4">
<label class="radio-inline">
<input type="radio" name="radio" id="uncknownRadio" value="Unknown">Unknown
</label>
</div>
</div>
</div>
</div> <!-- /.form-group -->
<div class="form-group">
<label for="Specilization" class="col-sm-3 control-label">Specilization</label>
<div class="col-sm-9">
<input type="text" id="spec" name=spec placeholder="Specilization" class="form-control" autofocus>
</div>
</div>
<div class="form-group">
<label for="designation" class="col-sm-3 control-label">Designation</label>
<div class="col-sm-9">
<input type="text" id="designation" name=desig placeholder="Designation" class="form-control" autofocus>
</div>
</div>
<div class="form-group">
<label for="fee" class="col-sm-3 control-label">Fee</label>
<div class="col-sm-9">
<input type="text" id="fee" name=fee placeholder="Fee" class="form-control" autofocus>
</div>
</div>
<div class="form-group">
<label for="checkbox1" class="col-sm-3 control-label">Available On</label>
<div class="col-sm-9">
<input type="checkbox" name="weekday" value="Mon">Mon
<input type="checkbox" name="weekday" value="Tue">Tue
<input type="checkbox" name="weekday" value="Wed">Wed
<input type="checkbox" name="weekday" value="Thu">Thu
<input type="checkbox" name="weekday" value="Fri">Fri
<input type="checkbox" name="weekday" value="Sat">Sat
<input type="checkbox" name="weekday" value="Sun">Sun
</div>
</div>
<div class="form-group">
<label for="timef" class="col-sm-3 control-label">Available from</label>
<div class="col-sm-9">
<input type="time" id="timef" name=timef class="form-control">
</div>
</div>
<div class="form-group">
<label for="timet" class="col-sm-3 control-label">Available upto</label>
<div class="col-sm-9">
<input type="time" id="timet" name=timet class="form-control">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" id="email" name=email placeholder="Email" class="form-control">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-3 control-label">Phone Number</label>
<div class="col-sm-9">
<input type="text" id="phone" name=phone placeholder="Phone Number" class="form-control">
</div>
</div>
<div class="form-group">
<label for="address" class="col-sm-3 control-label">Address</label>
<div class="col-sm-9">
<textarea class="form-control" rows="3" id="address" name=address placeholder="Address"></textarea>
</div>
</div>
<div class="form-group">
<label for="city" class="col-sm-3 control-label">City</label>
<div class="col-sm-9">
<input type="text" id="city" name=city placeholder="City" class="form-control">
</div>
</div>
<div class="form-group">
<label for="state" class="col-sm-3 control-label">State</label>
<div class="col-sm-9">
<input type="text" id="state" name=state placeholder="State" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3">
<button type="submit" value=submit class="btn btn-primary btn-block">Register</button>
</div>
</div>
</form>
I even tried cloning the time fields with
$(function()
{
$(document).on('click', '.btn-add', function(e)
{
e.preventDefault();
var controlForm = $('.controls form:first'),
currentEntry = $(this).parents('.entry:first'),
newEntry = $(currentEntry.clone()).appendTo(controlForm);
newEntry.find('input').val('');
controlForm.find('.entry:not(:last) .btn-add')
.removeClass('btn-add').addClass('btn-remove')
.removeClass('btn-success').addClass('btn-danger')
.html('<span class="glyphicon glyphicon-minus"></span>');
}).on('click', '.btn-remove', function(e)
{
$(this).parents('.entry:first').remove();
e.preventDefault();
return false;
});
});
I have a modal where i put my form inside. Now, my problem is, when i click the button of my onclick="regpatient()" button, the required will pop-up but when i look at it in console, the data was submited by a post because of my onlick function. How can i do this?
Here is my modal:
<div class="modal-body">
<form class="form-horizontal" id="frm_patientreg">
<div class="form-group">
<label class="control-label col-sm-3" for="pfname">First Name:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="pafname" name="pafname" placeholder="First name" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pmname">Middle Name:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="pamname" name="pamname" placeholder="Middle name" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="plname">Last Name:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="palname" name="palname" placeholder="Last name" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="paddress">Address:</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="paaddress" name="paaddress" placeholder="Address" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pcontact">Contact #:</label>
<div class="col-sm-7">
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' class="form-control" id="pacontact" name="pacontact" placeholder="Contact number" maxlength="11" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pbdate">Birthdate:</label>
<div class="col-sm-5">
<div class="input-group date" data-provide="datepicker">
<input type="text" class="form-control" id="pabdate" name="pabdate" placeholder="Birthdate" required>
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="page">Age:</label>
<div class="col-sm-4">
<input type="number" class="form-control" id="paage" name="paage" placeholder="Age" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pheight">Height:</label>
<div class="col-sm-4">
<input type="number" class="form-control" id="paheight" name="paheight" placeholder="Height (cm)" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pweight">Weight:</label>
<div class="col-sm-4">
<input type="number" class="form-control" id="paweight" name="paweight" placeholder="Weight (kg)" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="psex">Sex:</label>
<div class="col-sm-5">
<select class="form-control" id="psex" name="psex">
<option value="0">--- SELECT OPTION ---</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pmartiastat">Civil Status:</label>
<div class="col-sm-5">
<select class="form-control" id="pmartialstat" name="pmartialstat">
<option value="0">--- SELECT OPTION ---</option>
<option value="Single">Single</option>
<option value="Living common law">Living common law</option>
<option value="Married">Married</option>
<option value="Widowed">Widowed</option>
<option value="Separated">Separated</option>
<option value="Divorced">Divorced</option>
</select>
</div>
</div>
</div><!-- modal-body -->
<div class="modal-footer">
<button value="submit" onclick="regpatient()" class="btn btn-primary">Register Patient</button>
</form>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div><!-- modal-footer -->
</div><!-- modal-content -->
and here is my ajax where it fires when the button in footer of my modal is clicked:
function regpatient() {
var a = $('#psex').val();
var b = $('#pmartialstat').val();
if(a == "0" || b == "0") {
alert("Please select option");
}
else {
$.ajax({
url: siteurl+"sec_myclinic/addpatient",
type: "POST",
data: $('#frm_patientreg').serialize(),
dataType: "JSON",
success: function(data) {
alert("Successfully Added");
$('#frm_patientreg')[0].reset();
}
});
}
}
If I understand your problem right you want to stop the button's default submit action and only use your onclick script. Try this:
onclick="regpatient(event)"
and
function regpatient(event) {
event.preventDefault();
...
Edit
The required fields are not getting validated by the onclick function. Some checks like this for the required values should help stop the ajax call.
if (!$('#pafname').val()) {
return alert('Please fill in all required fields.');
}
// ajax code here ...
Use this
<button value="button" onclick="regpatient()" class="btn btn-primary">Register Patient</button>
instead of this
<button value="submit" onclick="regpatient()" class="btn btn-primary">Register Patient</button>
I am new in angularjs, i want to have a filed form from database when the user want to update his profile. I have a webservice that can give all information for the logged user then another webservice to get the information that i need with the id of the user.This is my controller:
controller('myTalentisCtrl', function ($scope,$http) {
this.User_Talent={};
this.T_User={} ;
$http.get("/LoggedUser").success(function(data) {
alert(data);
this.T_User.lnId = data.lnId;
alert(this.T_User.lnId);
});
$http.get("/usertalent",this.T_User.lnId).success(function(data) {
alert(data);
this.User_Talent.user = data.user;
this.User_Talent.talent = data.talent;
});
$scope.modif=function(){
$http.put('updating/' + this.T_User.lnId, $scope.User_Talent).success(function(data) {
$scope.User_Talent = data;
});
};
this is a part of my form:
<div ng-controller="myTalentisCtrl" class="tab-content no-margin">
<!-- Tabs Content -->
<div class="tab-pane with-bg active" id="fwv-1">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label class="control-label" for="full_name">First Name</label>
<input type="text" ng-model="User_Talent.talent.strFirstName" class="form-control" name="first_name" id="first_name" data-validate="required" placeholder="Your first name" />
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<label class="control-label" for="address_line_2">Address</label>
<input ng-model="User_Talent.talent.strAdress" class="form-control" name="address_line_2" id="address_line_2" placeholder="(Optional) your Address" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label" for="full_name">Last Name</label>
<input type="text" ng-model="User_Talent.talent.strLastName" class="form-control" name="last_name" id="last_name" data-validate="required" placeholder="Your last name" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label" for="city">City</label>
<input ng-model="User_Talent.talent.strCity" class="form-control" name="city" id="city" data-validate="required" placeholder="Current city" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label" for="door_no">Phone Number</label>
<input ng-model="User_Talent.talent.lnPhone" class="form-control" name="phone_no" id="phone_no" data-validate="number" placeholder="Numbers only" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label" for="zip">Zip</label>
<input ng-model="User_Talent.talent.lnZipCode" class="form-control" name="zip_no" id="zip_no" data-validate="number" placeholder="Numbers only" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label" for="email">Email Adress</label>
<div class="input-group">
<span class="input-group-addon">
<i class="linecons-mail"></i>
</span>
<input ng-model="User_Talent.talent.strEmail" type="email" class="form-control" placeholder="Enter your email">
</div>
<!-- <label class="control-label" for="city">Email Adress</label>
<input ng-model="User_Talent.talent.strEmail" class="form-control" name="city" id="city" data-validate="required" placeholder="Current city" /> -->
</div>
</div>
Is there a way to initialize my ng-model with the existing values ?
you should perform a digest cycle to reflect the changes in the DOM.
Look into digest
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
I have form, where exist few required fields Example
<div class="form-group">
<label class="col-md-3 control-label" for="mpassword">Password<span class="required"> * </span></label>
<div class="col-md-3">
<input type="password" id="mpassword" name="mpassword" class="form-control" placeholder="Enter password"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="memail">Email <span class="required">* </span></label>
<div class="col-md-3">
<input type="email" id="memail" name="memail" class="form-control" placeholder="Enter email"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="mname">Full name<span class="required">* </span></label>
<div class="col-md-3">
<input type="text" id="mname" name="mname" class="form-control" placeholder="Enter name" value="MyName"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="mname">Nick name</label>
<div class="col-md-3">
<input type="text" id="mname" name="mname" class="form-control" placeholder="Enter nickname"/>
</div>
</div>
Problem, i don't know what is correct way to get all required fields as they don't have required attribute or class. Only labels have required class, which means "this input is required"
Just target the labels, and then get the inputs based on the for attribute
$('label:has(.required)').map(function() {
return $('#' + $(this).attr('for'));
});
Note that two of the form elements have the same name ?
$('.required').next().find('input').each(function() {
// handle elements
});