HTML5 pattern not validating - javascript

This is my input element. I want it to accept only letters (No numbers or any other characters).
<input type="text" name="firstName" pattern="[a-zA-Z]" class="form-control" id="firstName" placeholder="First Name" required>
This is my entire partial view.
<form method = "post" action = "" ng-controller = "signUpCtrl as signUp">
<div class="form-group row">
<label for="firstName" class="col-sm-2 form-control-label">First Name</label>
<div class="col-sm-10">
<input type="text" name="firstName" pattern="[a-zA-Z]" class="form-control" id="firstName" placeholder="First Name" required>
</div>
</div>
<div class="form-group row">
<label for="middleName" class="col-sm-2 form-control-label">Middle Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="middleName" placeholder="Middle Name" required>
</div>
</div>
<div class="form-group row">
<label for="lastName" class="col-sm-2 form-control-label">Last Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lastName" placeholder="Last Name" required>
</div>
</div>
<div class="form-group row">
<label for="dob" class="col-sm-2 form-control-label">Date of Birth</label>
<div class="col-sm-10">
<input type="date" class="form-control" id="dob" placeholder="Date of Birth" required>
</div>
</div>
<div class="form-group row">
<label for="userName" class="col-sm-2 form-control-label">User Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="userName" placeholder="User Name" required>
</div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-2 form-control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="Password" required>
</div>
</div>
<div class="form-group row">
<label for="confirmPassword" class="col-sm-2 form-control-label">Confirm Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="confirmPassword" placeholder="Confirm Password" required>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 form-control-label">Email Address</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="Email Address" required>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10 text-center">
<button type="submit" class="btn btn-lg btn-info submitButton">Submit</button>
</div>
</div>
</form>
I am using - Google Chrome 50.0.2661.94 (Official Build) m (32-bit)

Can you please try with the following(use this in the input element):
ng-pattern="/^[A-Za-z]+$/"

Use ^[a-zA-Z][^0-9]+$ or ^\w[^\d]+$ instead of [a-zA-Z].
Edit :
If you need to skip symbols also then use :
^[a-zA-Z][^0-9-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]+$.
or
^[a-zA-Z][^0-9\W]+$ // this will skip all non-word characters.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div class='container'>
<div class="panel panel-default">
<div class="panel-body">
<form method="post" action="" ng-controller="signUpCtrl as signUp">
<div class="form-group row">
<label for="firstName" class="col-sm-2 form-control-label">First Name</label>
<div class="col-sm-10">
<input type="text" name="firstName" pattern="^[a-zA-Z][^0-9]+$" class="form-control" id="firstName" placeholder="First Name" required>
</div>
</div>
<div class="form-group row">
<label for="middleName" class="col-sm-2 form-control-label">Middle Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="middleName" placeholder="Middle Name" required>
</div>
</div>
<div class="form-group row">
<label for="lastName" class="col-sm-2 form-control-label">Last Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lastName" placeholder="Last Name" required>
</div>
</div>
<div class="form-group row">
<label for="dob" class="col-sm-2 form-control-label">Date of Birth</label>
<div class="col-sm-10">
<input type="date" class="form-control" id="dob" placeholder="Date of Birth" required>
</div>
</div>
<div class="form-group row">
<label for="userName" class="col-sm-2 form-control-label">User Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="userName" placeholder="User Name" required>
</div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-2 form-control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" placeholder="Password" required>
</div>
</div>
<div class="form-group row">
<label for="confirmPassword" class="col-sm-2 form-control-label">Confirm Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="confirmPassword" placeholder="Confirm Password" required>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 form-control-label">Email Address</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="Email Address" required>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-lg btn-info submitButton">Submit</button>
</div>
</form>
</div>
</div>
</div>

Related

Validation error An invalid form control with name='' is not focusable

I had a html form with set of input fields for each of which I am giving required="true" for validation. But when I click on submit after entering the mandatory fields it is give an error:
An invalid form control with name='' is not focusable
I know if we use novalidate to the form I wont get that error, but my validation wont work. How can I make my validation work without error?
<form name="customerForm" class="form-horizontal" id="custForm">
<fieldset>
<div class="form-group">
<label for="customerName" class="col-lg-4 col-lg-offset-1 control-label">Name</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.customerName" placeholder="Enter customer name" type="text" required="true" value = "{{customerName}}">
</div>
</div>
<div class="form-group">
<label for="postCode" class="col-lg-4 col-lg-offset-1 control-label">Post code</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.address.postcode" placeholder="Enter post code" type="text" value = "{{postCode}}">
</div>
<div class="col-lg-2">
<button type="next" class="btn btn-primary" ng-click="$ctrl.findAddress()">Find address</button>
</div>
</div>
<div class="form-group">
<label for="selectAddress" class="col-lg-4 col-lg-offset-1 control-label">Select address</label>
<div class="col-lg-4">
<select class="form-control" id="selectAddress" ng-model="$ctrl.quote.newClient.customerAddress" ng-change="$ctrl.populateAddressFields()">
<option ng-repeat="address in addresses"
value="{{address}}" >{{address}}</option>
</select>
</div>
</div>
<div class="form-group ng-hide">
<label for="textArea" class="col-lg-4 col-lg-offset-1 control-label">Address</label>
<div class="col-lg-4">
<textarea ng-model="$ctrl.quote.newClient.customerAddress" class="form-control" rows="3" required="true"></textarea>
</div>
</div>
<div class="form-group">
<label for="address1" class="col-lg-4 col-lg-offset-1 control-label">Address 1</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.address.addressLine1" placeholder="Enter Address 1" type="text" required="true" value = "{{addressLine1}}">
</div>
</div>
<div class="form-group">
<label for="address2" class="col-lg-4 col-lg-offset-1 control-label">Address 2</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.address.addressLine2" placeholder="Enter Address 2" type="text" required="true" value = "{{addressLine2}}">
</div>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-4 col-lg-offset-1 control-label">Email address</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.eMail" placeholder="Enter customer email" type="email" required="true" value = "{{emailId}}">
</div>
</div>
<div class="form-group">
<label for="customerMobile" class="col-lg-4 col-lg-offset-1 control-label">Mobile number</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.customerMobile" placeholder="Enter customer mobile number" type="text" required="true">
</div>
</div>
<div class="form-group" ng-show="$root.customerDetails.tradeType.description == 'Limited Company'">
<label for="inputCompanyRegistrationNumber" class="col-lg-4 col-lg-offset-1 control-label">Company registration number</label>
<div class="col-lg-4">
<input class="form-control" ng-model="$root.customerDetails.companyRegNo" placeholder="Enter company registration number" type="text" required="true">
</div>
</div>
</fieldset>
</form>
And Iam using $valid to validate the form
<div class="col-lg-1">
<button form="custForm" type="submit" class="btn btn-primary right-button" ng-click="customerForm.$valid && $ctrl.proceedToPaymentDetails()">Next</button>
</div>

data submitted into database if the informations are invalid using jquery,Ajax

I am new to Jquery and ajax.I want to check if mail id is already exist in database. So I use Ajax to check email is exist or not. I check this condition during submitting a form, so I use onSubmit event.
when I enter submit button the above condition is checked and then inserted into database.but If condition fails that email also inserted into database.
what is the error? I cannot find this error please help me!
my html code is:
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<div class="inner">
<h3>Registration</h3>
<form class="form-horizontal" method="post" action = "" onSubmit=" return checkmail();">
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Firstname</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="Firstname" name="fname">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Lastname</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="Lastname"name="lname">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="email" placeholder="Email" name="email" >
<span id="check"></span>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">Password</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="pass" placeholder="Password" name="pass" >
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">re-Password</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="repass" placeholder="re-Password">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Mobile no</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="mob" placeholder="Mobile no" name="phone">
</div>
<h6 id="aaa"></h6>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Address1</label>
<div class="col-sm-9">
<textarea class="form-control" id="inputEmail3" placeholder="Address1" name="addr1"></textarea>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Address2</label>
<div class="col-sm-9">
<textarea class="form-control" id="inputEmail3" placeholder="Address2(optional)"name="addr2"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" >Country</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="Country" name="country">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" >State</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="State" name="state">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" >City</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="City" name="city">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Zipcode</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="zip" placeholder="EX:456322" name="zip" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" name="submit" value="Register">
</div>
</div>
</form>
</div>
</div>
<div class="col-md-3">
</div>
</div>
</div>
my Jquery function is
function checkmail(){
var mail= $('#email').val();
if(mail ==""){
$("#email").focus();
alert("Enter mail id");
return false;
}
else
{
$.ajax({
type:'post',
url :'check.php',
data: {emailid:mail},
success: function(responseText){
$("#check").html(responseText);
if(responseText != 1) { // if the response is 1
$("#check").html("Available!");
return true;
}
else {
// else blank response
$("#email").focus();
$("#check").html("Email are already exist.");
return false;
}
}
});
}
}
</script>
check.php
<?php
$con = new mysqli('localhost','root','','registration'); //set coonection to db
$mail = $_POST['emailid'];
$query = mysqli_query($con, "select email from user where email='".$mail."' ");
if(mysqli_num_rows($query) > 0)
{
echo '1'; // if mail exist
}
?>
thank you in advance.
You should do something like this:
<form class="form-horizontal" method="post" action = "" onSubmit="checkmail()">
And put the script right after body tag starts.
I will recommend you to do this from jquery
$("#form-id").on('submit',function(e){
e.preventDefault();
// Validation for data
});
use below code with few modification.
The HTML
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<div class="inner">
<h3>Registration</h3>
<form id="myForm" class="form-horizontal" method="post" action="">
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Firstname</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="Firstname" name="fname">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Lastname</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="Lastname"name="lname">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="email" placeholder="Email" name="email" >
<span id="check"></span>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">Password</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="pass" placeholder="Password" name="pass" >
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-3 control-label">re-Password</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="repass" placeholder="re-Password">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Mobile no</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="mob" placeholder="Mobile no" name="phone">
</div>
<h6 id="aaa"></h6>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Address1</label>
<div class="col-sm-9">
<textarea class="form-control" id="inputEmail3" placeholder="Address1" name="addr1"></textarea>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Address2</label>
<div class="col-sm-9">
<textarea class="form-control" id="inputEmail3" placeholder="Address2(optional)"name="addr2"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" >Country</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="Country" name="country">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" >State</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="State" name="state">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" >City</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputEmail3" placeholder="City" name="city">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Zipcode</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="zip" placeholder="EX:456322" name="zip" >
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="button" onClick="checkmail();" class="btn btn-default" name="submit" value="Register">
</div>
</div>
</form>
</div>
</div>
<div class="col-md-3">
</div>
</div>
</div>
JQuery Code
function checkmail(){
var mail= $('#email').val();
if(mail ==""){
$("#email").focus();
alert("Enter mail id");
return false;
}
else
{
$.ajax({
type:'post',
url :'check.php',
data: {emailid:mail},
success: function(responseText){
$("#check").html(responseText);
if(responseText != 1) { // if the response is 1
$("#check").html("Available!");
$("#myForm").submit();
}
else {
// else blank response
$("#email").focus();
$("#check").html("Email are already exist.");
return false;
}
}
});
}
}
Change the id of form accordingly, which kept as myForm

How to do the Modification for this requirement? and where to do?

In the sample below I add some sample file, which is working fine but I need to do some modification on that. Currently while validating, it shows the error like this:
But what I need it to do is to show a tooltip. The tooltip needs to be auto pop and remain visible until the error is cleared. Like this:
$('#myForm').validator()
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://1000hz.github.io/bootstrap-validator/dist/validator.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<form data-toggle="validator" role="form">
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Cina Saffary" required>
</div>
<div class="form-group has-feedback">
<label for="inputTwitter" class="control-label">Twitter</label>
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="text" pattern="^[_A-z0-9]{1,}$" maxlength="15" class="form-control" id="inputTwitter" placeholder="1000hz" required>
</div>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
<span class="help-block with-errors">Hey look, this one has feedback icons!</span>
</div>
<div class="form-group">
<label for="inputEmail" class="control-label">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email" data-error="Bruh, that email address is invalid" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label">Password</label>
<div class="form-group col-sm-6">
<input type="password" data-minlength="6" class="form-control" id="inputPassword" placeholder="Password" required>
<span class="help-block">Minimum of 6 characters</span>
</div>
<div class="form-group col-sm-6">
<input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword" data-match-error="Whoops, these don't match" placeholder="Confirm" required>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group">
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Boxers
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Briefs
</label>
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="terms" data-error="Before you wreck yourself" required>
Check yourself
</label>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Many ways of doing this.
With pure CSS, leaving the DOM structure alone:
span.help-block.with-errors {
position: absolute;
top:45%;
right:36px;
z-index:1000;
}
Or, if you modify the DOM structure (moving the validation text into the .form-group):
<div class="form-group has-feedback">
<label for="inputTwitter" class="control-label">Twitter</label>
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="text" pattern="^[_A-z0-9]{1,}$" maxlength="15" class="form-control" id="inputTwitter" placeholder="1000hz" required>
<span style="position:absolute;top:2px;right:36px;z-index:1000;" class="help-block with-errors">Hey look, this one has feedback icons!</span>
</div>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
</div>
You could inline a bit of CSS to get the position correct. Stack snippet follows.
If you want to do the tooltip-style thing, I'd suggest a jQuery plugin to handle this bit for you. One example is Tooltipster.
$('#myForm').validator()
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://1000hz.github.io/bootstrap-validator/dist/validator.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<form data-toggle="validator" role="form">
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Cina Saffary" required>
</div>
<div class="form-group has-feedback">
<label for="inputTwitter" class="control-label">Twitter</label>
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="text" pattern="^[_A-z0-9]{1,}$" maxlength="15" class="form-control" id="inputTwitter" placeholder="1000hz" required>
<span style="position:absolute;top:2px;right:36px;z-index:1000;" class="help-block with-errors">Hey look, this one has feedback icons!</span>
</div>
<span class="glyphicon form-control-feedback" aria-hidden="true"></span>
</div>
<div class="form-group">
<label for="inputEmail" class="control-label">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email" data-error="Bruh, that email address is invalid" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label">Password</label>
<div class="form-group col-sm-6">
<input type="password" data-minlength="6" class="form-control" id="inputPassword" placeholder="Password" required>
<span class="help-block">Minimum of 6 characters</span>
</div>
<div class="form-group col-sm-6">
<input type="password" class="form-control" id="inputPasswordConfirm" data-match="#inputPassword" data-match-error="Whoops, these don't match" placeholder="Confirm" required>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-group">
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Boxers
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="underwear" required>
Briefs
</label>
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="terms" data-error="Before you wreck yourself" required>
Check yourself
</label>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

Dynamically adding bootstrap fields using clone

I am trying to create a form where users can click a plus button to add another row of fields and a remove button. This is what I have so far:
$(".add").click(function() {
$(".frm > div:first-child").clone(true).insertBefore(".frm > div:last-child");
return false;
});
$(".remove").click(function() {
$(this).parent().remove();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<form class="frm">
<div>
<div class="form-group col-md-1">
<br/>
<h4 style="text-align:right">1.</h4>
</div>
<div class="form-group col-md-1">
<label for="title" class="control-label">Title</label>
<input type="text" value='' class="form-control" id="title" placeholder="Title"/>
</div>
<div class="form-group col-md-1">
<label for="fname" class="control-label">First Name</label>
<input type="text" value='' class="form-control" id="fname" placeholder="First Name"/>
</div>
<div class="form-group col-md-2">
<label for="sname" class="control-label">Surname</label>
<input type="text" value='' class="form-control" id="sname" placeholder="Surname"/>
</div>
<div class="form-group col-md-2">
<label for="job" class="control-label">Job</label>
<input type="text" value='' class="form-control" id="job" placeholder="Job"/>
</div>
<div class="form-group col-md-2">
<label for="class" class="control-label">Class</label>
<input type="text" value='' class="form-control" id="class" placeholder="Class"/>
</div>
<div class="form-group col-md-2 col-md-inset-1">
<label for="emailadd" class="control-label">Email Address</label>
<input type="email" value='' class="form-control" id="emailadd" placeholder="Email Address"/>
</div>
<span class="remove">Remove</span>
</div>
<div>
<span class="add">Add fields</span>
</div>
</form>
With the code above I can add the rows but for some reason I cannot remove them. Also when I do add them the layout gets ruined, instead of the rows appearing underneath each other they append to the end and then break off to another line. what could be causing the remove to not work?
COUNTER JS
$(document).on('click', '.add', function() {
$('.counter').html(function(i, val) { return val*1+1 });
});
$(document).on('click', '.remove', function() {
$('.counter').html(function(i, val) { return val*1-1 });
});
If you want to add fields even when there are no fields, then one option is a hidden field with the full code.
jsfiddle fullscreen demo (code)
$(document).ready(function() {
$(".add").click(function() {
$(".cloneDefault").clone(true).insertBefore(".frm > div:last-child");
$(".frm > .cloneDefault").removeClass("cloneDefault");
return false;
.cloneDefault{
display: none;
}
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<div class="row cloneDefault">
<div class="form-group col-md-1">
<br/>
<h4 style="text-align:right">1.</h4>
</div>
<div class="form-group col-md-1">
<label for="title" class="control-label">Title</label>
<input type="text" value='' class="form-control" id="title" placeholder="Title"/>
</div>
<div class="form-group col-md-1">
<label for="fname" class="control-label">First Name</label>
<input type="text" value='' class="form-control" id="fname" placeholder="First Name"/>
</div>
<div class="form-group col-md-2">
<label for="sname" class="control-label">Surname</label>
<input type="text" value='' class="form-control" id="sname" placeholder="Surname"/>
</div>
<div class="form-group col-md-2">
<label for="job" class="control-label">Job</label>
<input type="text" value='' class="form-control" id="job" placeholder="Job"/>
</div>
<div class="form-group col-md-2">
<label for="class" class="control-label">Class</label>
<input type="text" value='' class="form-control" id="class" placeholder="Class"/>
</div>
<div class="form-group col-md-2 col-md-inset-1">
<label for="emailadd" class="control-label">Email Address</label>
<input type="email" value='' class="form-control" id="emailadd" placeholder="Email Address"/>
</div>
<span class="remove">Remove</span>
</div>
<form class="frm">
<div class="row">
<div class="form-group col-md-1">
<br/>
<h4 style="text-align:right">1.</h4>
</div>
<div class="form-group col-md-1">
<label for="title" class="control-label">Title</label>
<input type="text" value='' class="form-control" id="title" placeholder="Title"/>
</div>
<div class="form-group col-md-1">
<label for="fname" class="control-label">First Name</label>
<input type="text" value='' class="form-control" id="fname" placeholder="First Name"/>
</div>
<div class="form-group col-md-2">
<label for="sname" class="control-label">Surname</label>
<input type="text" value='' class="form-control" id="sname" placeholder="Surname"/>
</div>
<div class="form-group col-md-2">
<label for="job" class="control-label">Job</label>
<input type="text" value='' class="form-control" id="job" placeholder="Job"/>
</div>
<div class="form-group col-md-2">
<label for="class" class="control-label">Class</label>
<input type="text" value='' class="form-control" id="class" placeholder="Class"/>
</div>
<div class="form-group col-md-2 col-md-inset-1">
<label for="emailadd" class="control-label">Email Address</label>
<input type="email" value='' class="form-control" id="emailadd" placeholder="Email Address"/>
</div>
<span class="remove">Remove</span>
</div>
<div>
<span class="add">Add fields</span>
</div>
</form>

Jquery Validation plugin not validating entire form

I am trying to validate a form with the jQuery validation plugin. However, I am only getting the validation to work for 1 of the inputs. Any ideas?
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"> </script>
<form id="driver-form" class="form-horizontal driver" method="post">
<div class="form-group">
<label for="inputName1" class="col-md-2 col-md-offset-2 control-label">First Name</label>
<div class="col-md-4">
<input type="text" class="form-control" id="driverFirst" placeholder="First Name" required/>
</div>
</div>
<div class="form-group">
<label for="driverLast" class="col-md-2 col-md-offset-2 control-label">Last Name</label>
<div class="col-md-4">
<input type="text" class="form-control" id="driverLast" placeholder="Last Name" required/>
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-md-2 col-md-offset-2 control-label">Email</label>
<div class="col-md-4">
<input type="email" class="form-control" id="driverEmail" placeholder="Email" required/>
</div>
</div>
<div class="form-group">
<label for="inputPhone1" class="col-md-2 col-md-offset-2 control-label">Phone Number</label>
<div class="col-md-4">
<input type="text" class="form-control" id="driverPhone" placeholder="Phone Number" required/>
</div>
</div>
</form>
javascript:
$("#driver-form").validate();
jsfiddle: http://jsfiddle.net/rynslmns/533Yt/
The validation framework used name attribute, you have not specified it
<input type="text" class="form-control" id="driverFirst" name="driverFirst" placeholder="First Name" required/>
Demo: Fiddle

Categories

Resources