Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:10:00+00:00 2026-06-02T07:10:00+00:00

How can I validate only certain attributes on a model? Currently I check if

  • 0

How can I validate only certain attributes on a model? Currently I check if the attribute exists in the object passed into validate:

validate: function(attrs) {

        // Number
        if (attrs.minimum) {
          if (isNaN(attrs.minimum)) {
            return -1;
          }
        }
        if (attrs.maximum) {
          if (isNaN(attrs.maximum)) {
            return -1;
          }
        }
}

but if I want to validate string value then:

if (attrs.mystring) {
 // Do validation
}

would fail and the validation never takes place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:10:03+00:00Added an answer on June 2, 2026 at 7:10 am

    Im a little confused by your wording, but I think you want to check if its not an empty string first? and also work out the possibility that it is undefined..if so then this is what you’ll want to do..

    validate: function(attrs) {
        // Number
        if (attrs.minimum) {
          if (isNaN(attrs.minimum)) {
            return -1;
          }
        }
        if (attrs.maximum) {
          if (isNaN(attrs.maximum)) {
            return -1;
          }
        }
        if (typeof(attrs.mystring) != "undefined"){
          if (!attrs.mystring){
            return -1;
          }
        }
    }
    

    if you want to only validate one of your attributes, you should write your validate function to accommodate the options accordingly

    validate: function(attrs, option) {
      if (!option){
        // Number
        if (attrs.minimum) {
          if (isNaN(attrs.minimum)) {
            return -1;
          }
        }
        if (attrs.maximum) {
          if (isNaN(attrs.maximum)) {
            return -1;
          }
        }
        if (!attrs.mystring){
          return -1;
        }
      }else{
        switch(option){
          case("string"):
            if (!attrs.mystring){
              return -1;
            }
          break;
          case("number"):
            // Number
            if (attrs.minimum) {
              if (isNaN(attrs.minimum)) {
                return -1;
              }
            }
            if (attrs.maximum) {
              if (isNaN(attrs.maximum)) {
                return -1;
              }
            }
          break;
        }
      }
    }
    

    there are many ways to do this, this probably being the least efficient lol but using your example, it will do the job.

    also, this isn’t really a backbone.js problem per say…but general js

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function which can validate a given value (passed as
How can I validate a string using Regular Expressions to only allow alphanumeric characters
How can I validate an XML document against a DTD file which only my
How can I quickly validate if a string is alphabetic only, e.g var str
My current understanding of Castle Windsor registration is that one can only validate registration
I wonder if, with Spring Security, I can validate the user sessions, allowing only
I have methods which parameters can only handle certain types of variable. I have
I need to validate a textbox that it should allow only certain characters and
I want to check a certain argument value against a regex-pattern and only continue
What is a good regular expression that can validate a text string to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.