If the problem is that some event handler is still submitting data after showing an alert
informing of a validation error, you’ll probably need to make the event handler function return false
after showing the alert
, in order to prevent further processing of the event.
As pointed out by @Bergi in his comment, and if you’re using JQuery or another framework, you might need to use stopPropagation()
or equivalent. See
- How to stop event bubbling on checkbox click
- event.preventDefault() vs. return false.
UPDATE
Seeing the code posted, it might be important to remark that the return false
is being done inside the IsFieldEmpty
function. That means that false
is the return value for IsFieldEmpty
. Take into account that if you’re submitting data in a similar way to this:
<input type="submit" onclick="IsFieldEmpty()">
IsFieldEmpty
will return false but the event handler won’t. If it’s the case, you should change it to:
<input type="submit" onclick="return IsFieldEmpty()">
and make IsFieldEmpty
return true
when validations are passed.
function IsFieldEmpty() {
var txtAgent_Name = document.getElementById("<%=txtAgent_Name.ClientID %>");
if (txtAgent_Name.value == "") {
alert('Please Enter Agent Name');
txtAgent_Name.focus();
return false;
}
return true;
}
5
solved Javascript error1 [closed]