Angular validations - Enabling/Disabling input fields changes validity of form - javascript

I have a form. In which I have input fields and I am doing some validation based on input. Also, there is a submit button which enables only if form is valid.
In this form I am enabling/disabling input field in one of the flow.
Initially, when the fields are enable and empty, create button is disabled. After i disable and enable the fields, create button becomes enabled. Although input fields are still empty.
More over button which is enabling/disabling this part is outside of this form.
Here is my code
`
<form method="post" novalidate id="example-widgets-form" name="mdnsCtrl.createSubDomainForm" valdr-type="SubDomain">
<div>
<label>Domain Name</label>
<input required type="text" name="subDomainName" placeholder="Domain Name" ng-model="mdnsCtrl.newDomain.name">
</div>
<div>
<label>Description</label>
<input type="text" name="subDomainDescription" placeholder="Description (Optional)" ng-model="mdnsCtrl.newDomain.description">
</div>
<button type="button" aria-label="Create" ng-click="mdnsCtrl.createDomain();"
ng-disabled="mdnsCtrl.createSubDomainForm.$invalid">
<span class="ng-scope">Create</span>
</button>
</div>
</form>
Tried few things like using $setUntouched() and $setPristine(). But nothing is working. Any help will be appreciated.
Adding a codepen example for this: code
Much Thanks.
`

Its not good practice to mix Angular with jQuery. Please read this great post: “Thinking in AngularJS” if I have a jQuery background?
You can easily achieve requested behavior by using ng-disabled="mdnsCtrl.formDisabled"
JS
var ctrl = this;
ctrl.formDisabled = false;
this.disable = function(){
ctrl.formDisabled = true;
};
this.enable = function(){
ctrl.formDisabled = false;
};
HTML
<div>
<label>Domain Name</label>
<input class="input-field" required type="text" name="subDomainName" placeholder="Domain Name"
ng-disabled="mdnsCtrl.formDisabled"
ng-model="mdnsCtrl.name" >
</div>
<div>
<label>Description</label>
<input class="input-field" type="text" name="subDomainDescription"
ng-disabled="mdnsCtrl.formDisabled"
placeholder="Description (Optional)" ng-model="mdnsCtrl.description">
</div>
Fixed Demo Codepen

I think you missed required attribute for Description input..
<input type="text" name="subDomainDescription" required ng-model="mdnsCtrl.newDomain.description">

Related

how to display error msg using create element in form validation?

I want to do a validation registration form. How to display the error of the input empty in a span at the bottom of the input?
I want this span to not already exist and be created to display the message.
enter code here
<form action="" name="reg-form" method="post">
<div id="name-box" onblur="validateFullname()" class="form-holder">
<input type="text" name="name" placeholder="enter name" id="name" class="form-control">
</div>
i want create a span element and display message after input or append to div.
The solution will look something like this:
Identify where the "Error Message" span will appear. It needs to be inside another element, even if that element is the body. My example contains: <div id="ErrMsg"></div>
You need an event that triggers both the validation, and the creation of the error span. Usually, that event is a button press. So let's go with that in this example:
$('#mybutt').click(function(){
var tmpL = $('#login').val();
var tmpP = $('#pword').val();
if( tmpL=='' || tmpP=='' ){
$('#ErrMsg').html('<span>Please fill-out all fields</span>');
}else{
alert('Logging in now');
}
});
$('input').on('keyup', function(){
if ( $('#ErrMsg span').length ) $('#ErrMsg span').remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div id="LoginDIV">
<div id="LName">
Login: <input id="login" type="text" />
</div>
<div id="LPass">
Pword: <input id="pword" type="password" />
</div>
<div id="ErrMsg"></div>
<div id="btnDIV"><button id="mybutt">Login</button></div>
</div>
Note: the <script> tag that references the jQuery library is all that you need in order to use jQuery. jQuery just makes javascript easier, more consistent, and less-typing-required.
Your best bet is probably to use JQuery Validation. It's built-in methods don't technically use a span, as you specified, but you could easily tack on your own function to un-hide the custom span you create. Or, better yet, just style their error-message <label> in the way you prefer.
Here's an example using just JQuery Validation:
<form action="/" method="post" id="commentForm">
<div class="form-holder">
<input type="text" name="username" placeholder="username" class="form-control">
</div>
<div class="form-holder col-12">
<input type="email" name="email" placeholder="email" class="form-control">
</div>
</form>
<script>
var validator = $("#commentForm").validate();
validator.showErrors({
"email": "Please enter a valid email address"
});
</script>
(The example leaves out the includes for JQuery and JQuery Validation.)

Clear input field without reloading of the page

I have a input field where a user enters in a name at the end of a quiz and i have tried various methods of clearing the field. The page cannot re-load as the user is able to restart a quiz.
Here is my HTML:
<div class="js-quiz-submit">
<input type="text" class="js-name" placeholder="Please Enter Your Name"/>
<input type="button" class="js-submit" value="Submit"/>
</div>
If your input field is in a form say #quiz-form, then you can clear the fields of form as follows:
var form = document.querySelector('#quiz-form');
form.reset();
I guess the better way will be :
document.querySelector(".js-name").value = "";
Html:
<div class="js-quiz-submit">
<input type="text" id="js-name" class="js-name" placeholder="Please Enter Your Name"/>
<input type="button" class="js-submit" value="Submit"/>
</div>
Javascript:
document.getElementById('js-name').value='';

REQUIRED || REQUIRED HTML5 Forms

I have arrived to a situation that if only one of the two required fields in the form are filled then it should submit the form. Unfortunately, if i put required on both the fields it would wait for both of them to be filled in. Can this work without using JS? I know it is nearly impossible while not knowing any of the hidden features regarding it?
Can this work without using JS?
No, you need JavaScript to do that validation client-side.
The solution requires JS, but is easy - simply toggle the .required property of the elements based on whether either of them has a value:
var t1 = document.getElementById('test1');
var t2 = document.getElementById('test2');
function toggleRequired() {
t2.required = (t1.value.trim() === '');
t1.required = (t2.value.trim() === '');
}
t1.addEventListener('change', toggleRequired, false);
t2.addEventListener('change', toggleRequired, false);
<form>
<input name="test1" id="test1" required />
<br/>
<input name="test2" id="test2" required />
<br/>
<input type="submit" />
</form>
You could also put it into one div. It did worked for me.
<form>
<div class=question>
<input id="field1_1" name="field1" required>
<input id="field1_2" name="field1" required>
<input type="submit" />
</div>
</form>

jQuery.validationEngine v2.6.1 Only Validates Sometimes?

The code appears to be hooked up (like this)
jQuery("#contactForm").validationEngine();
because it will validate and raise an error bubble if:
you tab out of required field without any input
you type at least one character into a field that requires more and then click the submit button
But it will not validate and raise an error bubble if you do nothing at all except click the submit button. In that case, it just submits. Once you click in the field or enter anything at all, it seems to work.
What can I be looking for that I've mis-configured?
The HTML:
<form class = "contactform" id = "contactForm">
<fieldset>
<div class="contactform-name contactform-field">
<label class="contactform-label" for="contactform-name">Name:
<br>
</label>
<input class="validate[required,minSize[8]] contactform-input" type="text" id="contactform-name" name="name" />
</div>
<div class="contactform-email contactform-field">
<label class="contactform-label" for="contactform-email">Email Address:<br></label>
<input value class="validate[required,custom[email]] contactform-input" type="email" id="contactform-email" name="contactform-email" />
</div>
<div class="contactform-text contactform-field">
<label class="contactform-label" for="contactform-text">Message:
<br>
</label>
<textarea class="validate[required,minSize[12]]contactform-input" name="text" id="contactform-text" > </textarea>
</div>
<input class="contactform-button" type="submit" name="submit" value="Send" />
</fieldset>
</form>
The JavaScript (it's running in Meteor):
Template.Contact.rendered = function () {
jQuery("#contactForm").validationEngine();
}
I've never used this engine, but from the docs I found that 'attach' will attach the validator to form.submit. Can it be as simple as that?
https://github.com/posabsolute/jQuery-Validation-Engine#attach
EDIT:
You can also do stuff to the submit-event (if the tip above won't help).
Something like this (not tested, but should put you in the correct path):
Template.templateName.events({
'submit': function(event) {
// Prevent the submit with preventDefault()
event.preventDefault();
// Do something to check the submit etc.
}
});

Disabling submit button until all fields have values on multi-part form

I have created a multi-part form and need to validate completed fields in visible fieldsets on the form. If all required fields are completed, the the next step button will be enabled.
So Far I have played with a few options none of which are 100% effective
HTML:
<div id="set1">
<fieldset>
<div>
<label>field 1</label>
<input name="f1" type="text" /><br />
<span class="error"></span>
</div>
<div>
<label>field 2</label>
<input name="f2" type="text" /><br />
</div>
</fieldset>
</div>
<div id="set2">
<fieldset>
<div>
<label>field 3</label>
<input name="f3" type="text" /><br />
<span class="error"></span>
</div>
.
.
.
</fieldset>
</div>
jQuery:
$input = $('fieldset:visible div:has(span[class="error"]) input');
$next = $('fieldset:visible .button');
$input.keyup(function() {
$input.each(function() {
var trigger = false;
$input.each(function() {
if (!$(this).val()) {
trigger = true;
}
});
trigger ? $next.attr('class', 'disable') : $next.removeAttr('class');
});
});
Could someone help me understand what I am doing wrong? It appears the keyup event is not firing.
You should use jQuery validator. It will make your life a lot easier:
jQuery Validation
First thing, the keyup event will be fired, don't worry. Second thing, there is a heavy error at line if (!$(this).val()) { This spelling caused by wrong thinking in standard javascript. Translated to it you wanted to test if the value exists or not. The truth is, the value does always exists. It is just "" or a real value.
Look at my example. After some corrections it works as you wanted, hopefully.

Categories