I am new to angular js. I am trying to change the border of the fields to red when empty form is submitted.
I have written controller where it processes the input when valid form is submitted and just makes a field(invalidFormSubmitted) to true when invalid form is submitted.
I tried below css but it is not working
input.invalidFormSubmitted{
border-bottom: 0.125rem solid #e42105;
}
I also tried with ng-submitted, but no luck.
How do i make the field border red when this variable is set to true?
Thanks in advance.
you're looking for ng-class
angular.module('app',[]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<style>
input.invalidFormSubmitted{
border:1px solid red;
}
</style>
<div ng-app="app">
<form name="myForm">
<input type="text" required ng-model="foo" name="sample" ng-class="{'invalidFormSubmitted':myForm.sample.$invalid}" />
</form>
</div>
As you can see the class is applied as long there is no value in the input here
try this.
var app = angular.module("app",[]);
app.controller("MyCtrl" , function($scope){
});
.invalidFormSubmitted{
border-bottom: 0.125rem solid #e42105;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
<form name="form">
<input name="name" ng-model="name" ng-required="true" ng-class="{'invalidFormSubmitted': form.name.$invalid && check}">
<button type="button" name="button" ng-click="check = true">ENVIAR</button>
</form>
</div>
Related
I am trying to show a DIV if two inputs have the same value, as already asked here: How to compare two inputs and show a div
I can run the code snippet, but when I try it myself it doesn't seem to work. The code is very simple, yet I don't see why the DIV isn't showing when the two values are matching:
$("#some1, #some2").on("keyup change", function(){
let firstEl = $("#some1"),
secondEl = $("#some2"),
conditionalEl = $("#showhide");
if (firstEl.val() == secondEl.val() ) {
conditionalEl.show();
} else {
conditionalEl.hide();
}
});
#showhide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="number" name="some1" id="some1">
<input type="number" name="some2" id="some2">
<div id="showhide">The inputs are the same</div>
<input type="submit">
</form>
</html>
Seems your key up function is not working, Try this
<html>
<head>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function myFunction(vaule){
let conditionalEl = $('#showhide');
if ($("#some1").val() == $("#some2").val() ) {
conditionalEl.show();
} else {
conditionalEl.hide();
}
}
</script>
<form>
<input type="number" name="some1" id="some1" onchange="myFunction()">
<input type="number" name="some2" id="some2" onchange="myFunction()">
<div id="showhide" style="display:none;">The inputs are the same</div>
<input type="submit">
</form>
</body>
</html>
How to change the background color of an input field when disabled is true.How to use ng-class for disabled input field.
//html
<div>
<input type="file" ng-disabled="false" id="id1">
</div>
//Controller
document.getElementById("id1").disabled = true;
Just have scope variable for ng-disabled and use it with ng-class
<input type="file" ng-disabled="shouldDisable" ng-class="{'disabledCssClassName' : shouldDisable}" id="id1">
Then in controller manage disabled state of your controler by setting shouldDisable = true/false and define css class in your stylesheet
You don't need ng-class while you can use ng-style:
<input type="file" ng-disabled="false" id="id1" ng-style="{'background-color': disabled? 'red' : 'green'}"
Here is an example using the ng-class
angular
.module('app', [])
.controller('ctrl', function($scope) {
$scope.disabled = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<style>
.ng-disabled {
background: #E9B96E;
}
</style>
<span ng-app="app" ng-controller="ctrl">
Disabled? <input type="checkbox" ng-model="disabled"/>
<br/>
<!-- sets `ng-disabled` as class when `disabled` var is true! -->
<input ng-class="{'ng-disabled': disabled}" ng-disabled="disabled" ng-value="disabled? 'Disabled': 'Enabled'"/>
</span>
And here you can see how to achieve the same goal using the CSS :disabled selector (see below snippet). This requires less code and can be applied to all inputs in an easier way.
angular
.module('app', [])
.controller('ctrl', function($scope) {
$scope.disabled = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<style>
input:disabled {
background: #E9B96E;
}
</style>
<span ng-app="app" ng-controller="ctrl">
Disabled? <input type="checkbox" ng-model="disabled"/>
<br/>
<input ng-disabled="disabled" ng-value="disabled? 'Disabled': 'Enabled'"/>
</span>
I`m using Angualr for my web.
I have this part in html code:
<div class="form-group">
<div class="text-form" style="float: left;">Companion URL</div>
<input type="text" placeholder="Companion URL" class="companion-url-box" ng-model="newCreative.companionUrl">
</div>
I want to add method (in the Controller) that when the text change, it will add a new button down.
How can I do it? I can`t understand how ng-change works.
thanks
Simply use ng-if to check if newCreative.companionUrl exists in scope :
<div class="form-group">
<div class="text-form" style="float: left;">Companion URL</div>
<input type="text" placeholder="Companion URL" class="companion-url-box" ng-model="newCreative.companionUrl">
<input type="button" value="Click me" ng-if="newCreative.companionUrl">
</div>
You can also use a function to validate button visibility :
index.html
<!doctype html>
<html lang="en" ng-app="app">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<script src="scripts.js"></script>
</head>
<body ng-controller="SampleController as ctrl">
<div class="form-group">
<div class="text-form" style="float: left;">Companion URL</div>
<input type="text" placeholder="Companion URL" class="companion-url-box" ng-model="newCreative.companionUrl">
<input type="button" value="Click me" ng-if="ctrl.isButtonAvailable()">
</div>
</body>
</html>
script.js
angular.module('app', []);
angular.module('app').controller('SampleController', function ($scope) {
var ctrl = this;
ctrl.isButtonAvailable = function() {
if(!$scope.newCreative) {
return false;
}
// Ensure companion url starts with https:// using a regular expression
return /^https:\/\//.test($scope.newCreative.companionUrl);
}
});
Working example
Check out this Plunker : https://plnkr.co/edit/n8VtJrenePGf9hCbmzWJ
You can use the directives ng-change on input and ng-if or ng-show on button, plus a little code on controller.
Check this:
http://codepen.io/mQuixaba/pen/oZqXWG?editors=1111
HTML:
<div ng-app="myApp" ng-controller="MyController">
<label for="input">Text:</label>
<input type="text" id="input" ng-model="text" ng-change="showButton=true"/>
<button ng-if="showButton" ng-click="hideButton()">My Button</button>
</div>
JS:
angular
.module('myApp', [])
.controller('MyController', function($scope){
$scope.showButton = false;
$scope.text = "My text";
$scope.hideButton = function() {
$scope.showButton = false;
}
});
I would like to know if there is a way to change the color of an input field after you clicked on a submit button, like change it's color to red if the input is invalid. My current solution marks every input red at the beginning, because of my current css code.
input:invalid {
background: #FF3300;
}
I want the input fields to remain white until you hit submit button, then the correct inputs should turn green while the incorrect ones should turn red.
Thanks in advance.
Small example.
HTML:
<html><!DOCTYPE html>
<head>
<title>Example</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>
<form action="">
<input type="text" id="name" class="input" maxlength="50" pattern="[A-Za-z\\s]*" placeholder="Name" required>
<input type="submit" value="Submit" id="Submit">
</form>
</body>
</html>
CSS:
input:invalid {
background: #FF3300;
}
input:valid {
background: lightgreen;
}
#Submit{
background: lightgrey;
}
Try this:
<!DOCTYPE html>
<html>
<head>
<title>Say I'm a duck</title>
</head>
<body>
<script>
function verify(){
var value = document.forms[0].item.value; // or replace document.forms[0].item by document.getElementById('name')
var objective = "I'm a duck";
if(objective != value){
document.forms[0].item.style.color="#FF3300";
return false;
} else {
document.forms[0].item.style.color="lightgreen";
}
return true;
}
</script>
<form action="" method="" onSubmit="verify();">
<input type="text" name="item" id="name" class="input" maxlength="50" pattern="[A-Za-z\\s]*" placeholder="Name" required>
<input type="submit" value="Submit" id="Submit">
</form>
</body>
</html>
The onSubmit property will call the js verif() function.
In javascript, the call to DOM Object.style.property = value; will change the object style.
W3School js change css
Admin XVII
You may have incorrectly set parameters in the tag input. That is: type, required, pattern. Read more on the Internet about HTML5 form validation. CSS selector you are correct. Please note that this method works only in modern browsers supporting HTML5, in older browsers, you will have to use JS libraries to validate the form or write a small script in PHP.
What you are asking for is one of angularJS features.
But to answer your question... First of all I asume you are using ajax, or the reload won't show the input any more. If that is so, when you get an error from the call you can add an .invalid class, so instead you'd need this:
input.invalid {
background: #FF3300;
}
And on ajax error:
document.querySelector(".targetInput").classList.add("invalid");
Have you considered using JQuery validation plugin?
$("form").validate({
errorClass: "my-error-class",
validClass: "my-valid-class",
rules: {
test: {
required: true,
minlength: 12
}
}
});
http://jsfiddle.net/8vQFd/
I have a iframe containing div to be shown later that is initially hidden using a class. When I remove the class from the container div, everything inside it is shown. but textbox inside iframe is not shown.
parent.htm
<style>
.hide
{
display: none;
}
</style>
<script>
function showSearchWindow(show) {
if (show) {
$('div.overlay').removeClass('hide');
}
else {
$('div.overlay').addClass('hide');
}
}
</script>
<form id="form1">
<div class='overlay hide'>
<input type="text" id='txt1' value='test1' />
<iframe id="frame" src="frame.htm"></iframe>
</div>
<input type="button" id='btnShow' value='Show' onclick='showSearchWindow(true)' />
<input type="button" id='btnHide' value='Hide' onclick='showSearchWindow(false)' />
</form>
frame.htm
//Reference to jQuery 1.4.1 js file
<form id="form1">
<input type="text" id='txt2' value='test'/>
</form>
when I click 'btnShow', 'txt1' is shown but 'txt2' is not shown.
I did not work in IE 7,8 and 9. in other major browsers it works fine.
Are you using jQuery?
Calling $('div.overlay').removeClass('hide') is not pure javascript.
A solution with pure javascript could look like this:
<html>
<head>
<style>
.hide { display: none; }
</style>
<script>
function showSearchWindow(show) {
document.getElementById("mydiv").className = (show ? "" : "hide");
}
</script>
</head>
<body>
<form id="form1">
<div class='hide' id='mydiv'>
<input type="text" id='txt1' value='test1' />
<iframe id="frame" src="frame.htm"></iframe>
</div>
<input type="button" id='btnShow' value='Show' onclick='showSearchWindow(true)' />
<input type="button" id='btnHide' value='Hide' onclick='showSearchWindow(false)' />
</form>
</body>
</html>
The changes I've made is using document.getElementById("mydiv").className to set the class name. I also access the div using id=mydiv instead of class=overlay.
This works in FF9. Hope it helps!
Reloading the page after removing the class is the best solution I found so far.
function showSearchWindow(show) {
if (show) {
$('div.overlay').removeClass('hide');
$('#frame').attr('src', 'frame.htm');
}
else {
$('div.overlay').addClass('hide');
$('#frame').attr('src', 'about:blank');
}
}
<iframe id="frame" src="about:blank"></iframe