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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:03:49+00:00 2026-05-28T07:03:49+00:00

While developing a web application I want to perform certain validation check and only

  • 0

While developing a web application I want to perform certain validation check and only after sucesssful validation I need to post the form and redirect control to next page.

JavaScript code:

function fnCheckEmptyField()
{
    var strDomain = document.getElementsByName("txtIIDN").value;
    if (strDomain == null)
    {
        document.getElementById("lblValidityStatus").innerHTML = "";
        document.getElementById("lblValidityStatus").innerHTML = "Domain Name Field Can't be Left Blank";
        return false;
    }
    else
    {
        return true; 
    }
}

Relevant HTML code:

<form action="Result.jsp" name="validityCheck" onsubmit="return fnCheckEmptyField()">
    <input type="text" id="txtIIDN"/>
    <input type="submit" id="btnValidityCheck" value="Check Validity" />
</form>

The line onsubmit="return fnCheckEmptyField()" shows an error Cannot return from outside a function or method and after execution of the JavaScript function form is getting submitted regardless the text field is blank or not.

I have placed alerts inside if condition and it is sure that if field is empty function returns false.

I don’t know what’s wrong with my code and why this errors with Cannot return from outside a function or method.

What’s the cause and how can I solve it?

  • 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-28T07:03:49+00:00Added an answer on May 28, 2026 at 7:03 am

    the line onsubmit="return fnCheckEmptyField()" showing an error Cannot return from outside a function or method

    That’s specific to Eclipse. Eclipse is wrong here, that line is perfectly fine. Just ignore the Eclipse error. If you want, you can always disable its JS validation.


    and after execution of the java script function form is getting submitted regardless the text field is blank or not.

    That’s because your JavaScript function is wrong. You’ve 2 mistakes in your JS code.

    1. The getElementsByName() call is incorrect.

      var strDomain = document.getElementsByName("txtIIDN").value;
      

      To retrieve an element by ID, you need getElementById().

      var strDomain = document.getElementById("txtIIDN").value;
      
    2. Empty values are not null, but just empty string.

      if (strDomain == null)
      

      You need to check its length instead.

      if (strDomain.length == 0)
      

      Or just make use of JS boolean magic.

      if (!strDomain)
      

    By the way, the line document.getElementById("lblValidityStatus").innerHTML = ""; is unnecessary in this code.

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

Sidebar

Related Questions

I am developing one web application in that I want to check load time
I am developing an application in Spring Web MVC where i need to execute
I am developing a web application that can support threaded comments. I need the
I'm currently developing a web application that has one feature while allows input from
I'm developing a Web application with PHP 5.3.3. I want it to send confirmation
I've been developing a web application (written in Python/Django) for a while. All my
I am developing a web application in grails which uses lot of ajax.I need
I am developing a web application using netbeans,in that i need to display an
I am developing a web application in ASP.NET (C#) and need to give a
I am developing a web application which revolves around dates. I need to calculate

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.