I'm doing something wrong but cant see what. I've followed the angular js docs and the validations are not working:
<form name="callbackForm" ng-submit="requestCallback()">
<div class="col-md-3">
<input name="name" type="text" class="form-control" placeholder="Name..." ng-model="callback.name" ng-minlength="3" required=""/>
</div>
<div class="col-md-3">
<input name="telephone" type="text" class="form-control" placeholder="Telephone..." ng-model="callback.telephone" ng-minlength="11" required=""/>
</div>
<div class="col-md-3">
<input type="submit" class="btn btn-sm btn-block" value="Call Me!">
</div>
<div class="col-md-3">
<p ng-show="callbackForm.name.$error.required" class="help-block">Your name is required.</p>
<p ng-show="callbackForm.telephone.$invalid && !callbackForm.telephone.$pristine" class="help-block">Your telephone number is required.</p>
</div>
</form>
with a controller as follows:
'use strict';
app.controller('footerController', ['$scope', '$http', function ($scope, $http) {
$scope.requestCallback = function () {
alert("form callback");
};
}]);
However, I can't get my error messages to appear. Also the ng-minlength are not restricting the form submission? Am I missing something simple here?
Your code seems to work fine.
Regarding the submission restriction on error - you have to explicitly use angular's valiation by doing something like:
<form name="callbackForm" novalidate ng-submit="callbackForm.$valid && requestCallback()">
Here's a snippet:
function Ctrl($scope) {
$scope.requestCallback = function() {
alert("form callback");
};
}
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="Ctrl">
<form name="callbackForm" novalidate ng-submit="callbackForm.$valid && requestCallback()">
<div class="col-md-3">
<input name="name" type="text" class="form-control" placeholder="Name..." ng-model="callback.name" ng-minlength="3" required="" />
</div>
<div class="col-md-3">
<input name="telephone" type="text" class="form-control" placeholder="Telephone..." ng-model="callback.telephone" ng-minlength="11" required="" />
</div>
<div class="col-md-3">
<input type="submit" class="btn btn-sm btn-block" value="Call Me!">
</div>
<div class="col-md-3">
<p ng-show="callbackForm.name.$error.required" style="font-size: 14px; color:#ff0033" >Your name is required.</p>
<p ng-show="callbackForm.name.$error.minlength" style="font-size: 14px; color:#ff0033" >Your name should have at least 3 letters</p>
<p ng-show="callbackForm.telephone.$error.required" style="font-size: 14px; color:#ff0033" >Your telephone number is required.</p>
<p ng-show="callbackForm.telephone.$error.minlength" style="font-size: 14px; color:#ff0033" >Your telephone number should have at least 11 numbers.</p>
</div>
</form>
</div>
</div>
Your form is validating.. in order to call requestCallback() function, you need to bind your form to controller.
Wrap your form inside controller with ng-controller
<div ng-controller="footerController">
<form name="callbackForm" ng-submit="requestCallback()">
<div class="col-md-3">
<input name="name" type="text" class="form-control" placeholder="Name..." ng-model="callback.name" ng-minlength="3" required=""/>
</div>
<div class="col-md-3">
<input name="telephone" type="text" class="form-control" placeholder="Telephone..." ng-model="callback.telephone" ng-minlength="11" required=""/>
</div>
<div class="col-md-3">
<input type="submit" class="btn btn-sm btn-block" value="Call Me!">
</div>
<div class="col-md-3">
<p ng-show="callbackForm.name.$error.required" || callbackForm.name.$error.minlength class="help-block">Your name is required.</p>
<p ng-show="callbackForm.telephone.$invalid && !callbackForm.telephone.$pristine" class="help-block">Your telephone number is required.</p>
</div>
</form>
</div>
</div>
Related
I'm a novice at Laravel. How can I add new text input fields with the button and add data in each field separately? Add or plus button or whatever.
Below my code, which allows you to enter the sledge, but only in one field, in addition you have to separate the words with a comma.
<div class="card-body">
<form method="post" action="{{route('randomizeTeam.store')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="players">Add player names</label>
<input type="text" class="form-control" name="players">
</div>
<div class="form-group">
<label for="teams">Add team names</label>
<input type="text" class="form-control" name="teams">
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Send</button>
</div>
</form>
</div>
This is how you can do it in a simple way
$(function(){
var more_fields = `
<div class="form-group">
<label for="players">Add player name</label>
<input type="text" class="form-control" name="players[]">
</div>
<div class="form-group">
<label for="teams">Add team name</label>
<input type="text" class="form-control" name="teams[]">
</div>
`;
$('#add-more-field').on('click', (function (e) {
e.preventDefault();
$(".input-fields").append(more_fields);
}));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-body">
<form method="post" action="">
{{ csrf_field() }}
<div class="input-fields">
<div class="form-group">
<label for="players">Add player name</label>
<input type="text" class="form-control" name="players[]">
</div>
<div class="form-group">
<label for="teams">Add team name</label>
<input type="text" class="form-control" name="teams[]">
</div>
</div>
<div class="form-group">
<button id="add-more-field" class="btn btn-secondary btn-sm">add more</button>
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Send</button>
</div>
</form>
</div>
And in your controller, you will do something like this
foreach($request->get('players') as $i => $player) {
YourModel::create([
'player' => $player,
'team' => $request->get('teams')[$i]
]);
}
I'm still learning to programme on AngularJS.
I want to get values from the input box and then click on the button I want to save them in the database.
My database table is called "user". I have columns like id, username, password, name.
I have two checkbox.When I click on first checkbox(Edit) it displaying "div myVar",when i click on second checkbox(Add new user) it displaying "div myVar1".
I want to activate this button "Add" to add input box values to database MySQL. Please help me.
I have this html code:
<label><input type="checkbox" ng-model="myVar">Edit</label>
<label><input type="checkbox" ng-model="myVar1">Add new user</label>
<div ng-show="myVar">
<div class="form">
<form ng-submit="saveItem(userForm.$valid)" name="userForm">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="database_address">User</label>
<input type="text" class="form-control" required ng-model="activeItem.username" placeholder="Потребителско Име..." />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="text" class="form-control" required id="password" ng-model="activeItem.password" />
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="username">Operator</label>
<input type="text" class="form-control" required id="username" ng-model="activeItem.name" />
</div>
</div>
</div>
<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Save</button>
<!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
</form><form ng-submit="createUser(userForm.$valid)" name="userForm1">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="database_address">User</label>
<input type="text" class="form-control1" ng-model="usernamee" placeholder="Потребителско Име..." />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="text" class="form-control1" ng-model="passwordd"required id="password" />
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="username">Operator</label>
<input type="text" class="form-control1" ng-model="namee" required id="username" />
</div>
</div>
</div>
<button class="btn btn-primary" ng-click="createUser();" type="submit">Add user</button>
<!--<button class="btn btn-primary" ng-disabled="userForm.$invalid" type="submit">Добавяне на нов</button>-->
</form>
And Angular code:
$scope.createUser=function()
{
//console.log($scope.activeItem);
//delete $scope.activeItem.hash_method
var objectToSave = {
username: $scope.usernamee,
password: $scope.passwordd,
name: $scope.namee,
id: $scope.id
};
{
defaultAdapter.query('INSERT INTO users(username,password,name,id) VALUES(username = :usernamee, password = :passwordd,name = :namee WHERE id =:id)',
{ replacements: objectToSave, type: Sequelize.QueryTypes.UPDATE }
).then(projects => {
console.log(projects);
$scope.editMode = false;
$scope.activeItem = false;
$scope.refresh();
});
}
}
How to validate a input text box as number,I use ng-pattern for validate number but my input name is array of name like "item[priceFrom]" so how will i validate this field.This validation is working fine if input name is "priceFrom".
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
//$scope.name = "John Doe";
});
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<form name="myForm" class="form-horizontal" method="get" action="">
<div class="input-field">
<p class="heading">Price</p>
<div class="gutter6">
<div class="col-6">
<div class="input-wrapper has-float-label">
<input ng-pattern="/^[0-9]*$/" ng-model="search.priceFrom" type="text" id="price" name="item[priceFrom]" class="form-field">
<label class="form-control-placeholder ">From</label>
</div>
<span ng-show="myForm.item[priceFrom].$error.pattern">Not a valid number!</span>
</div>
<div class="col-6">
<div class="input-wrapper">
<input ng-pattern="/^[0-9]*$/" ng-model="search.priceTo" type="text" id="price" name="item[priceTo]" class="form-field" value="">
<label class="form-control-placeholder">To</label>
</div>
<span ng-show="myForm.item[priceTo].$error.pattern">Not a valid number!</span>
</div>
</div>
</div>
<div class="form-group text-center" style="margin-top: 5%;">
<input class="button submit esein5" value="Apply" type="submit">
<button type="Reset" class="button submit esein5">Reset</button>
</div>
</form>
</body>
Answer courtesy :Aleksey Solovey
https://stackoverflow.com/users/8495123/aleksey-solovey
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
//$scope.name = "John Doe";
});
.error{
color:red;
}
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<form name="myForm" class="form-horizontal" method="get" action="">
<div class="input-field">
<p class="heading">Price</p>
<div class="gutter6">
<div class="col-6">
<div class="input-wrapper has-float-label">
<input ng-pattern="/^[0-9]*$/" ng-model="search.priceFrom" type="text" id="price" name="item[priceFrom]" class="form-field">
<label class="form-control-placeholder ">From</label>
</div>
<span class="error" ng-show="myForm['item[priceFrom]'].$error.pattern">Not a valid number!</span>
</div>
<div class="col-6">
<div class="input-wrapper">
<input ng-pattern="/^[0-9]*$/" ng-model="search.priceTo" type="text" id="price" name="item[priceTo]" class="form-field" value="">
<label class="form-control-placeholder">To</label>
</div>
<span class="error" ng-show="myForm['item[priceTo]'].$error.pattern">Not a valid number!</span>
</div>
</div>
</div>
<div class="form-group text-center" style="margin-top: 5%;">
<input class="button submit esein5" value="Apply" type="submit">
<button type="Reset" class="button submit esein5">Reset</button>
</div>
</form>
</body>
I have the following form. I want to validate the email field .it should only contain for domain name "#somename.com " when the user clicks submit.
The mail should only be validated when the check box is clicked to show the email field.
If the email is invalid i want to prevent submission
<form class="form-horizontal" id="rForm" data-toggle="validator" role="form">
<div class="form-group">
<div class="col-xs-offset-3 col-xs-8">
<label class="checkbox-inline col-xs-10">
<input type="checkbox" ID="chkbx"> Email
</label>
</div>
</div>
<div class="form-group email">
<div class="col-xs-offset-3 col-xs-8">
<div class="col-xs-8">
<input type="text" class="form-control " placeholder="Enter email">
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-8">
<input type="reset" class="btn btn-default" id="rset" value="Reset">
<input type="submit" class="btn btn-primary" id="submit" value="Submit">
</div>
</div>
</form>
JS
$("#submit").on('keypress click', function(e){
e.preventDefault();
$('#chkbx').hide();
$('#chkbx').click(function () {
// This will show / hide your element accordingly
$('.email').toggle();
});
});
Checkout JavaScript RegEx to determine the email's domain (yahoo.com for example). You can use that and augment your example:
$('#chkbx').click(function() {
// This will show / hide your element accordingly
$('.email').toggle();
});
$("#submit").on('click', function(e) {
e.preventDefault();
if ($('#chkbx').is(':checked')) {
// https://stackoverflow.com/questions/3270185/javascript-regex-to-determine-the-emails-domain-yahoo-com-for-example
console.log(/#testdomain.com\s*$/.test($('#email_input').val()));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-horizontal" id="rForm" data-toggle="validator" role="form">
<div class="form-group">
<div class="col-xs-offset-3 col-xs-8">
<label class="checkbox-inline col-xs-10">
<input type="checkbox" ID="chkbx" checked>Email
</label>
</div>
</div>
<div class="form-group email">
<div class="col-xs-offset-3 col-xs-8">
<div class="col-xs-8">
<input id="email_input" type="text" class="form-control " placeholder="Enter email">
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-8">
<input type="reset" class="btn btn-default" id="rset" value="Reset">
<input type="submit" class="btn btn-primary" id="submit" value="Submit">
</div>
</div>
</form>
Use the following function to test for your email.
EDIT:
You first need to add a name= and id= to your email input field.... So what you want to do is:
<input type="text" class="form-control " placeholder="Enter email" name="email" id="email">
and then do:
function testEmail(string) {
var regex = /.+#somename.com$/i;
return regex.test(string);
}
//testEmail("nope#test.com"); // false
//testEmail("what#somename.com.other"); //false
//testEmail("yeah#somename.com"); //true
//testEmail("#somename.com"); // false
$('form').submit(function(e){
var values = $('form').serialize();
if(!testEmail(values.email))
e.preventDefault();
// your submission code goes here.
})
I have two includes in the HTML view listed below. I wait for the view to load before manipulating some elements but it fails because the includes have not yet finished loading. I check the existence of the elements using the console while debugging. In my controller:
wtApp.controller('createAccountController', function ($scope) {
resetNavbar();
$("a[href$='create-account']").css({ color: navbarSelectedColor });
$scope.$on('$viewContentLoaded', function () {
getCountries();
setupAccountsForm();
var form = $("#form-accounts");
if (form.length) {
form.get(0).reset();
}
});
});
<!DOCTYPE html>
<html>
<head>
<title>Writer's Tryst</title>
<link href='css/accounts.css' rel='stylesheet' type='text/css' />
</head>
<body data-ng-app="">
<div>
<form id="form-accounts" class="form-horizontal well center-form-small">
<h1>Create Account</h1>
<div class="capatcha">
<div id="recaptcha-elements" style="display: inline-block" data-sitekey=""></div>
</div>
<div id="oauth-login" class="form-group row text-center">
<p>
<button id="facebook-login" class="btn btn-custom-primary" formnovalidate>Login with Facebook</button>
<button id="google-login" class="btn btn-custom-primary" formnovalidate>Login with Google</button><br/>
</p>
<p class="text-center">
<button class="btn btn-custom-success" formnovalidate>- OR ENTER -</button>
</p>
</div>
<div id="passwords">
<div class="form-group row">
<label for="pwd" class="col-sm-2 form-control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" required id="pwd" name="pwd" placeholder="Password - 6 characters minimum (will be encrypted)" />
<button id="show-pwd" class="btn btn-custom-primary" formnovalidate tabindex="-1">show</button>
</div>
</div>
<div class="form-group row">
<label for="confirm-pwd" class="col-sm-2 form-control-label">Confirm Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" required id="confirm-pwd" placeholder="Confirm password" />
<button id="show-confirm-pwd" class="btn btn-custom-primary" tabindex="-1">show</button>
</div>
</div>
</div>
<div data-ng-include="'pages/snippets/work-type.html'"></div>
<div data-ng-include="'pages/snippets/account-info.html'"></div>
<button type="submit" id="submit" class="btn btn-custom-warrning btn-block">Send Verification Email</button>
<input type="hidden" id="subject" name="subject" />
<input type="hidden" id="msg" name="msg" />
<input type="hidden" id="userid" name="userid"/>
</form>
<form id="form-verify" class="form-horizontal well center-form-small">
<div id="verify">
<div class="form-group">
<input id="ver-email-msg" name="ver-email-msg" readonly="true" />
</div>
<div class="form-group center-div">
<label for="ver-code">Verification Code</label>
<input id="ver-code" name="ver-code" required autofocus="true" placeholder="Paste verification code" />
</div>
<div class="form-group">
<input id="code" name="code" type="hidden" />
<button id="but-register" name="but-register" class="btn btn-custom-success btn-block">Create Account</button>
</div>
</div>
<input id="account-id" name="account-id" type="hidden" />
</form>
</div>
<script src="js/recaptcha.js"></script>
<script src="js/accounts.js"></script>
</body>
</html>
Try using,
$rootScope.$on('$includeContentLoaded',function(event,url){
if (url == 'YourDesiredPath') {
// do something
}
});
From angular docs:
Emitted every time the ngInclude content is requested.