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?
Nishit jain
source share