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 7056265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:50:48+00:00 2026-05-28T03:50:48+00:00

I am getting value from a text field. I have one if and several

  • 0

I am getting value from a text field. I have one if and several else if statement.

The problem the last else if doesn’t execute even if the condition is true.

If I change the last else if to if it executes and gives alert. When I change that back to else if the statement doesn’t execute. The else if before that is fine as it’s firing/executing on a particular condition.

function Valcheck()
{

var txtVal = document.getElementById("sometextField").value;



if(txtVal =="%") 
    {

        alert("% is only allowed with other characters.");
        return;
    }       

    else if(txtVal.indexOf("%") != -1) 
    {
        if((txtVal.indexOf('%')) != (txtVal.length-1)) 
        {
            alert(" % is only allowed at the end.");
            return;
        }       
    } 
    else if(txtVal.indexOf(",") != -1) 
    {
        alert("Comma or comma separated values are not allowed.");
        return;

    } 
    else if(( txtVal.length  >0) &&  (txtVal.indexOf("%") == -1)) 
    {
        alert("Please enter % at the end of the value.");
        return;
    }            

    else if( txtVal.length > 11 ) 
    {

        alert(" Value can't be greater than 11 characters.");
        return;

    }

}

Please help. Thanks

  • 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-05-28T03:50:49+00:00Added an answer on May 28, 2026 at 3:50 am

    The problem is that if txtVal.length > 11, then either this is met:

        else if(txtVal.indexOf("%") != -1)
    

    or this is met:

        else if(( txtVal.length  >0) &&  (txtVal.indexOf("%") == -1))
    

    So that it will never reach the else if( txtVal.length > 11 ). You need to change this:

        else if(txtVal.indexOf("%") != -1) 
        {
            if((txtVal.indexOf('%')) != (txtVal.length-1)) 
            {
                alert(" % is only allowed at the end.");
                return;
            }       
        }
    

    to this:

        else if(txtVal.indexOf("%") != -1 && txtVal.indexOf('%') != (txtVal.length-1))
        {
            alert(" % is only allowed at the end.");
            return;
        }
    

    so that it doesn’t “capture” the case where txtVal.indexOf('%') == (txtVal.length-1).

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

Sidebar

Related Questions

Greetings! I'm having a problem getting a text value of a captcha from a
i have one text field and setting its name & values from php, like:
I have a problem with getting value of template field; Gridview is in ContentPlaceHolder1;
I have created code which takes a string from a text field and calls
I have a problem with getting textField value. View : I have Toobar variable
i am working on a project where i am getting date value from date-picker
From the code below I am getting a cell value from the selected row
hi from my db i am getting a value as string type and when
The requirement is following: I have to get the location field from page. var
I am trying to load a value into a field on a view from

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.