JavaScript Backbone.js model.isValid() method not returning false for an invalid model backbone model isvalid,backbone.js model isvalid

so I am trying to get the validate from my model to actually disable the save button but to re-validate when new input is included. Anyone know the best way to attempt this. Thanks! The problem I have with my method is that once it is disabled it doesn't return in state.

Here is the main.js file

(function () {
    window.App = {
        Models: {},
        Collections: {},
        Views: {},
        Templates: {},
        Router: {}

    };

    // MODEL
    App.Models.User = Backbone.Model.extend({
        defaults: {
            firstName: 'first',
            lastName: 'last',
            email: 'Email',
            phone: '222',
            birthday: '07/22/1980'
       },

        validate: function (attrs) {

            if (!attrs.firstName) {
                return 'You must enter a real first name.';
            }
            if (!attrs.lastName) {
                return 'You must enter a real last name.';
            }
            if (attrs.email.length < 5) {
                return 'You must enter a real email.';
            }
            if (attrs.phone.toString().length < 10 ) {
                //&& attrs.phone === int
               return 'You must enter a real phone number, if you did please remove the dash and spaces.';
            }
           // if (attrs.birthday.length < 2) {
             //   return 'You must enter a real city.';
            //}
        },

        initialize: function() {
             this.on('invalid', function (model, invalid) {
                console.log(invalid);
                alert(invalid);
            });
        }

    });

    //var userModel = new App.Models.User();

    //VIEW
    App.Views.User = Backbone.View.extend({
        el: '#user',
        //model: userModel,
        //tagName: 'div',
        //id: 'user',
        //className: 'userProfile',
        //template: _.template($("#userTemplate").html()),
        //editTemplate: _.template($("#userEditTemplate").html()),


        initialize: function (){

        },

        render: function() {
            this.template = Handlebars.compile($("#userTemplate").html());
            this.editTemplate = Handlebars.compile($("#userEditTemplate").html());

            this.$el.html(this.template(this.model.toJSON()));
            return this;
        },

        events: {
            'click button.edit': 'editProfile',
            'click input.save': 'saveEdits',
            'click button.cancel': 'cancelEdits'
        },

        editProfile: function () {
            this.$el.html(this.editTemplate(this.model.toJSON()));

        }, 

        saveEdits: function () {
            var form = $(this.el).find('form#updateUser');
            this.model.set({

                firstName : form.find('.firstName').val(),
                lastName : form.find('.lastName').val(),
                email: form.find('.email').val(),
                phone: form.find('.phone').val(),
                birthday: form.find('.birthday').val()

            }, {validate: true} );

        if(!this.model.isValid()) {
            console.log('run');
            $('.save').attr("disabled", "disabled");

        } else {
            console.log('run2');
            alert('Changes have been made.');
            $('.save').removeAttr("disabled");
            return this.render();
        }


    },


        },

        cancelEdits: function() {
            this.render();
        }

    });
    //start history service
    Backbone.history.start();

    var user = new App.Views.User({model: new App.Models.User()});
    user.render();
})();

And here is the Jade File:

extends layout
block content   
    div.centerContent

        h4 User goes here with equal before it no space
            div#user
                p #{firstName} #{lastName}
                p #{email}
                p #{phone}
                p #{birthday}
                button.edit Edit

        script(id="userTemplate", type ="text/template")
                p {{firstName}} {{lastName}} 1
                p {{email}} 2 
                p {{phone}} 3 
                p {{birthday}} 4
                button.edit Edit

        script(id="userEditTemplate", type ="text/template")
            div
                form(action="#")#updateUser
                    input(type="text", class="firstName", value='{{firstName}}') 
                    input(type="text", class="lastName", value='{{lastName}}')
                    input(type="email", class="email", value='{{email}}')
                    input(type="number", class="phone",, value='{{phone}}')
                    input(type="date", class="birthday", value='{{birthday}}')
                input(type="submit", value="Save").save Save
                button.cancel Cancel
        hr
        script(type="text/javascript", src="/js/main.js")
Answer:1

Guys actually i want to write this code shortly, instead of rewriting the same code for all my 4 elements individually i would like to write a function that will be called for each element and execute....

Guys actually i want to write this code shortly, instead of rewriting the same code for all my 4 elements individually i would like to write a function that will be called for each element and execute....

  1. write function jquery
  2. write jquery function in js file
  3. write jquery function in html
  4. write jquery function in typescript
  5. write jquery function with parameters
  6. write jquery function and call
  7. write jquery function in angularjs controller
  8. write jquery function inside php
  9. creating function jquery
  10. write function in jquery w3schools
  11. write function using jquery
  12. write a jquery function on page load
  13. write on click function jquery
  14. write simple jquery function
  15. how to write jquery function in external js file
  16. how to write jquery function in jsp page
  17. write onclick function in jquery
  18. how to write jquery function in angular 6
  19. how to write jquery function in react js
  20. how to write jquery function in javascript

I just found that I can use $('form').prop(name) to get input with that name within the form. Then I experimented on other tags, and this won't work for div and body. Now I don't have a way to tell if ...

I just found that I can use $('form').prop(name) to get input with that name within the form. Then I experimented on other tags, and this won't work for div and body. Now I don't have a way to tell if ...

  1. form form method property

It's certainly noticeable how delayed a YouTube video page can load when on a slower computer or laptop. Things such as the sidebar, comments, the uploaders Icon and other image elements. My script ...

It's certainly noticeable how delayed a YouTube video page can load when on a slower computer or laptop. Things such as the sidebar, comments, the uploaders Icon and other image elements. My script ...

  1. how to increase youtube page size
  2. how to increase youtube page rpm

Can anyone suggest a regex that will test if the first word of a string is a day of the week. e.g. "Tuesday, July 16" So far I have this: /^((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(...

Can anyone suggest a regex that will test if the first word of a string is a day of the week. e.g. "Tuesday, July 16" So far I have this: /^((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(...

  1. regex first word before space
  2. regex first word in string
  3. regex first word after match
  4. regex first word match
  5. regex first word of each line
  6. regex first word before comma
  7. regex first word
  8. regex first word after space
  9. regex first word before character
  10. regex first word only
  11. regex first word after comma
  12. regex first word javascript
  13. regex first occurrence of word
  14. regex get first word
  15. regex get first word before space
  16. regex remove first word
  17. regex extract first word
  18. regex match first word in line
  19. regex select first word
  20. regex ignore first word