Cannot get HTML5 validation to work - angularjs

I cannot get html5 validation to work when using it with AngularJS.
I simply need a field to be required, but instead of showing me the error, it submits the form.
I would appreciate anyone's input on how to get the html5 validation to work with AngularJS.
I have the following set up in my web.config file:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
I have the following set up in my masterpage:
<form id="userForm" name="userForm">
I have the following input box set up with the required attribute:
<div class="col-sm-8">
<input type="text" class="form-control" placeholder="Enter Check Slip Number" id="txtCDDepositSlipNumber" name="txtCDDepositSlipNumber" ng-model="formCtrl.depositSlip.selectedSlipID" required />
</div>

How do you submit form? You listen ng-submit (and have in the form of input type=submit) or handled ng-click on the button/item?
If the handle ng-submit the form - html5 validation works.

write submit button like:
<input type="submit" Value="Submit" ng-click="submitForm(model)">
ng-click will trigger the function you have written on angular controller.
by giving type="submit" to input tag validation would properly work on click of submit button.

Related

Access to form data that submitted by an unknown source

Scenario:
Another web application get a link from my web app and then POST some data to the link, like this:
<form action="http://www.example.com/mylink">
<input type="hidden" name="data1" value="1" />
<input type="hidden" name="data2" value="2" />
<input type='submit' value="submit" />
</form>
I know there are two parameters data1 and data2 in POST data and nothing else.
Question:
How can I access to this data in Controller?
Because this form submitted in another web application so there is not ng-model directive and or ng-click="submit(customerForm)". I don't know even what is form's name.

AngularJS Disabling Forms When Controllers are Created

AngularJS disables (cannot submit) forms that do not have a specific action set:
<form action="">
when you create a controller. It does not have this issue when creating directive or factories.
You can see this in a plunk here:
http://plnkr.co/edit/gWFRMKGO3FzZtOgs4VmW?p=preview
Form is defined as:
<form action="" method="post">
If you delete the starting on line 6, you will be able to submit the form.
A simple solution is to define the action, but I'd rather not do this, as it is not necessary.
UPDATE
Some details can be found here on trying to get this change in Angular:
https://github.com/angular/angular.js/pull/3776
You can use ng-submit to handle that.
<form ng-submit="submitForm()" method="post">
<input name="test" value="11111111" />
<button type="submit" name="submit" value="1">Send</button>
<input type="submit" name="submit2" value="Send2" />
</form>
That way the form will submit normally and you must actually send the data on your submitForm function (just an example name).
Here is a quick plnkr: http://plnkr.co/edit/lwWVG0CDHSGMtMU0B8Nj?p=preview
Notice that you can submit using the buttons and also by pressing enter on the field. I hope that's what you've been asking for.
Thanks

Angular input validity property without a form

I have inputs in a web page without the form tag (useless to me).
How can I get their validity status inside the HTML ? This
<input name="myInput" type="text" ng-pattern="/^[0-9]{13}$/">
<input type="button" ng-show="myInput.$valid">
doesn't work.
I'm afraid that won't work without wrapping it in a form as you need to access those fields via the form's controller.
<form name="myForm">
<input name="myInput" type="text" ng-pattern="/^[0-9]{13}$/">
<input type="button" ng-show="myForm.myInput.$valid">
</form>
Should work.
If you're unable to use the form tag for any reason, you'll have to wire that up manually.

empty form field is still being submitted when using angular form validation

The validation is being highlighted correctly, but when I click submit button, even with empty form field, the form is still being submitted (and nick value is undefined)
I tried adding novalidate to the form -- but that didn't help.
<form class="nick" ng-submit="joinChat()">
<input type="text" required name="nick" ng-model="nick" ng-minlength="2" ng-maxlength="10">
<button>Join</button>
</form>
I'm trying to follow this guide here:
http://www.ng-newsletter.com/posts/validations.html
The joinChat() function doesn't do any validation itself. As its my understanding this shouldn't be necessary when using Angular form validation.
$scope.joinChat = function(){
socket.emit('chat:join', { nick: $scope.nick });
};
Invalid input does not prevent angular form submission, instead try this:
<form class="nick" novalidate ng-submit="joinChat()" name="myform">
<input type="text" ng-required="true" name="nick" ng-model="nick" ng-minlength="2" ng-maxlength="10">
<button ng-disabled="myform.$invalid">Join</button>
</form>
Fiddle

custom validation in angularJS

I am new to AngularJS.
In the following fiddle, when user clicks on "This submit triggers validation. But I wanted to put this button at the end of the page" button. I could see alert/error but I want to show a custom message, which I am struggling to do it.
Header inputs:
<input type="name" ng-model="sample" required/>
<input type="name" ng-model="sampleX" required/>
<input type="submit" value="This submit triggers validation. But I wanted to put this button at the end of the page"/>
</form>
<hr/>
Some other form here. Think line items
<hr />
<a class="btn" ng-click="triggerSubmit()">Wanted this submit to trigger the validation to the form on which this button doesn't belong, e.g. trigger to header</a>
js fiddle link
http://jsfiddle.net/unWF3/6/
Thanks,
Kalyan Basa
To disable native browser validation add novalidate attribute to form element:
<form novalidate submit-on="myEvent" ng-submit="onSubmitted()">

Resources