Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a fieldset that requires the user to enter some data like name, emai land phone number like this:

        <h2 class="fs-title">Personal data</h2>
        <input type="text" name="firstname" id="firstname" placeholder="First Name" class="textbox" ng-model="firstName"required/>
        <input type="text" name="lastname" id="lastname" placeholder="Last Name" class="textbox" ng-model="lastName" required/>
        <input type="email" name="email" class="textbox" placeholder="Email" ng-model="user.email" required>
        <input type="text" name="contact" placeholder="Phone number" id="contact" ng-model="phoneNumber" required/>
        <input type="button" name="next" class="next action-button" value="Next"/>

The class next has a function associated to it:

var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches


$(".next").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show();

    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50)+"%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({
                'transform': 'scale('+scale+')',
                'position': 'absolute'
            });
            next_fs.css({'left': left, 'opacity': opacity});
        },
        duration: 800,
        complete: function(){
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        // easing: 'easeInOutBack'
        linear: 'easeInOutBack'
    });
});

In its current state, you can click Next even if the form is not properly completed, even if it's full empty and I don't want that. I want when I click next, to also check if the inputs are properly filled and if not, under each field, a message/warning ot be displayed about what the problem is (ie. on email, it has to contain a @)

I tried using the btn btn-primary classes from bootstrap but if I use them (they do validation as I want) I can't use my next class anymore. Any ideas how to make it work? I tried Angular directives, like ng-disable on condition that those fields are empty but for some reason, if I fill the fields, the button still doesn't work so how can I use btn btn-primary and still have my next class work?

share|improve this question

Use form api:

<form ng-submit="saveForm(MyForm.$valid)" name="MyForm">
   <input ng-model="myField" required>
   <button type="submit">Next</button>
</form>

and somewhere in controller:

function saveForm(isValid){
   if(!isValid) showErrors();
   save();
}
share|improve this answer
    
I can't use this because I already have a big form that's split in 3, that's why I have the next button who only passes me to the next step, I just want it to pass me on condition. – Mocktheduck Sep 18 '16 at 11:25
    
Anyway, you should avoid binding click via jquery and trying to use ng-click; Like so : ng-click="MyForm.$valid && handleNextClick()" – Valery Kozlov Sep 18 '16 at 11:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.