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

  • Home
  • SEARCH
  • 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 8876167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:08:43+00:00 2026-06-14T19:08:43+00:00

I have a form verification that checks for a minimum length, which works fine.

  • 0

I have a form verification that checks for a minimum length, which works fine. However I also need to check for a max length.

I’m using the following code for an textarea field called “msg” to verify minimum lenth, which works fine:

 var msgval    = $("#msg").val();
 var msglen    = msgval.length;

if(msglen < 4) {
$("#msg").addClass("error");
}
else if(msglen >= 4){
$("#msg").removeClass("error");
}

This works fine, but if I add a similar verification for msglen > 250, it ignores the previous minimum length requirement when structured as such:

if(msglen < 4) {
$("#msg").addClass("error");
}
else if(msglen >= 4){
$("#msg").removeClass("error");
}
if(msglen > 250) {
$("#msg").addClass("error");
}
else if(msglen <= 250){
$("#msg").removeClass("error");
}

How can I combine the two arguments?

  • 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-14T19:08:44+00:00Added an answer on June 14, 2026 at 7:08 pm

    Use the logical OR operator to test both conditions.

    if (msglen < 4 || msglen > 250) {
        $("#msg").addClass("error");
    } else {
        $("#msg").removeClass("error");
    }
    

    There’s no need for an else if condition. If it’s not less than 4 and not greater than 250, then you know you can remove the class.


    Or a shorter version could be written like this:

    $("#msg").toggleClass("error", msglen < 4 || msglen > 250);
    

    A truthy value passed as the second argument will add the class. A falsey value will remove it.

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

Sidebar

Related Questions

I'm creating a browser-based form verification script that checks if the input doesn't have
I have a form that uses Ajax for client-side verification. The end of the
I need to have a script read the files coming in and check information
I have a form where users can enter data that ultimately would be used
I have the following snippets that open a single Modal Form: <a class=modalbox id=foo
Hey all, I have an HTML form on my website which currently only uses
I have a custom asp-net control that inherits from another one and its works
We have a form that passes 3 parameters, fname, lname, hiredate to another page
I have django.middleware.csrf.CsrfViewMiddleware in settings.MIDDLEWARE_CLASSES and CSRF verification is working just fine for all
While updating my registration form verification PHP, I came across an error that I

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.