0

Any ideas on how I can make the html/browser validation work in the below code when also having to call a method. Also I can't use the submit type as a method because it's being used somewhere else. I need an alternative for the ng-click I suppose.

<input type="submit" ng-click="vm.submit1()" class="submitBtn" />

enter image description here

I guess I only want the ng-click to work only if the ng-submit has validated the fields.

0

2 Answers 2

1

Inside submit1 function, could you check form.$valid ?

<input type="button" ng-click="vm.submit1(form)" class="submitBtn" />

function YourController() {

   var self = this;
   self.submit1 = function(form) {
      if (form.$valid) { // OR self.form.$valid
         ...
      }
   }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is exactly what I was after
0

use it on button with the form name it will keep the button disabled until the form data will validated.

ng-disabled="frmLogin.$invalid"

here is the full code for normal login form for your help

<form  novalidate name="frmLogin" id="frmLogin" ng-submit="vm.doLogin()">

            <div class="form-group">
                <label>Email</label>
                <input class="form-control" type="email" name="txtusername" id="txtusername" ng-model="vm.newLogin.username"  required/>
                <span class="text-danger" ng-show="frmLogin.txtusername.$error.required">Email is required.</span>
            </div>
            <div class="form-group">
                <label>Password</label>
                <input class="form-control" type="password" name="txtpassword" id="txtpassword" ng-model="vm.newLogin.password" required/>
                <span class="text-danger" ng-show="frmLogin.txtpassword.$error.required">Password is required.</span>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-default" ng-disabled="frmLogin.$invalid">Login</button>

            </div>
            <div>
                Don't have a Account? <a href="#/Register"><strong>Register Now</strong></a>
            </div>
        </form>

3 Comments

No the thing is that I want to show the browsers validation like the image and not my own validation only. My own is working thank you kind sir :)
then try using required at element tag like <input class="form-control" type="password" name="txtpassword" id="txtpassword" required/>
Yes I also have that and still whenever I click on the button only the method is being thrown and the browsers validation no longer works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.