Can't return a function or method from the outside?

When developing a web application, I want to perform a certain validation check, and only after a successful check I need to submit a form and redirect the control to the 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; } } 

Corresponding 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 string onsubmit="return fnCheckEmptyField()" indicates an error. It is impossible to return a function or method from the outside, and after the execution of the form of the JavaScript function is sent, regardless of whether the text field is empty or not.

I have placed warnings inside the if condition and am sure that if the field is empty, the function returns false .

I do not know what is wrong with my code and why these errors cannot return a function or method from the outside.

What is the reason and how can I solve it?

+7
source share
5 answers

line onsubmit="return fnCheckEmptyField()" , indicating an error. Cannot return function or method from outside

This is specific to Eclipse. Eclipse is wrong here, this line is excellent. Just ignore the Eclipse error. If you want, you can always disable its JS check.


and after the form form is executed, the java script is sent regardless of whether the text field is empty or not.

This is because your JavaScript function is incorrect. You have 2 errors in your JS code.

  • Invalid call to getElementsByName() .

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

    To get an item by ID, you need getElementById() .

     var strDomain = document.getElementById("txtIIDN").value; 
  • Empty values ​​are not null , but simply an empty string.

     if (strDomain == null) 

    Instead, you need to check its length.

     if (strDomain.length == 0) 

    Or just use JS Boolean magic.

     if (!strDomain) 

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

+13
source

remove the β€œreturn” from the onsubmit attribute your code should look like this:

 <form action="Result.jsp" name="validityCheck" onsubmit="fnCheckEmptyField()"> <input type="text" id="txtIIDN"/> </form> 

hope this solves your problem :-)

+1
source

Besides,

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

it should be

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

The text field has an identifier, not a name

+1
source

I'm not a javascript expert, but I think you should lose the return keyword in your onsubmit parameter in HTML; as below:

 onsubmit="fnCheckEmptyField()" 

edit: sorry, was posted at the same time as the previous answer

0
source

Remove an action from a form tag.

 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"; } else { window.navigate("top.jsp"); } } 
0
source

All Articles