I have 3 javascript functions:
validateClk(), validateAmPm() and getClks()
And on two occurring events, they get executed as follows:
OnChange - executes validateClk() and validateAmPm()
OnClick - executes getClks() (getClks() returns a boolean value)
All 3 functions run correctly, but the problem is, after getClks() has finished execution and returns a boolean, the next function postClocks() doesn't run. I'm very sure that the code for postClocks() is correct as well. If I don't use the return statement for getClks() then the getClks() function doesn't work as expected.
Please help :(
<script type='text/javascript'>
function validateClk() {
....
var clks = clocks.value;
if (clks == "") {
alert('Enter time');
}
else { ... }
}
</script>
<script type='...'>
function validateAMPM() {
...
var ampm= ap.value;
if (ampm=="") {
alert('Enter am or pm');
}
}
</script>
<script type='text/...'>
function getClks() {
var clks= clock.value;
var ampm= ap.value;
if (clks==" && ampm="") {
alert('Enter time and am/pm');
return false;
}
else { ... }
return true;
}
</script>
<... onChange="validateClk(); validateAmPm();" />
<... button label="Submit" onClick="return getClks(); postClocks(); return false;" />
It's because you explicitly coded a return in there.
return getClks();
postClocks();
return false;
That code will always just exit after that first return statement. I suggest removing it.
have all of your custom functions return boolean then change the onclick event to this:
onClick="if(!getClks() || !postClocks()) return false;"
assuming you don't want to continue if invalid
You wrote
onClick="return getClks(); postClocks(); return false;"
You have to remove the first "return".
Related
At the bottom of my webpage, I have a submit button. I've used this code:
<a onClick="myFunction(); return false;" href="testpage.html">Submit</a>
What I'm trying to do is when my function is called, I'm checking for validation. If it's false, my function raises an alert and the user doesn't leave the current page...which is what I want. When its true though...nothing changes. When it's true I want them to go to the next link.
function myFunction() {
if (localStorage.length != 3) {
alert("Missing Values");
} else {
break;
}
}
It goes to the next link when I put in break, but now the alert doesn't get called even if it's requirements are met. Moreover, whey does the break in the else block get called even when the if block requirements are met?
Well return false cancels the action. So if you do not want to stop the link, you need to remove that.
<a onclick="return myFunction();" href="testpage.html">Submit</a>
Now return true or false from myFunction
function myFunction() {
if (localStorage.length != 3) {
alert("Missing Values");
return false;
}
return true;
}
Rewrite your html to <a onClick="myFunction()" href="testpage.html">Submit</a> and function to:
function myFunction() {
if (localStorage.length != 3) {
event.preventDefault();
}
}
See Fiddle: https://jsfiddle.net/ermakovnikolay/L0q7ocgg/
<script>
function someFunc() {
if (1==2) {
return true;
} else {
alert("Not submitting");
return false;
}
}
</script>
This is from stackoverflow here
prevent form submission (javascript)
I want to know what the purpose of the obvious false statement 1==2 returns true is for?
In general, to stop a form from submitting you would need to do two things:
Place an "onsubmit" event on the form tag
define the event handler in javascript and return false in that function
So, basically, in HTML you would need:
<form ... onsubmit="return someFunc()">
And in the javascript code you would need:
function someFunc() {
return false;
}
The part with 1 == 2 its for developing purposes only i would imagine, just a mechanism of making the forms to never submit.
When moving on from the developing environment this should be done in a more clever configurable way.
You might as well have a function like this:
function someFunc() {
if (1 > 0) {
return true;
} else {
alert("Not submitting");
return false;
}
}
That validation is only meant to always stop the from from submitting.
Or, a function like this:
function someFunc() {
alert("Not submitting");
return false;
}
<a href="#" type="image" class="topopup" onclick="ShowDIV3();" >Click Here</a>
In this code I need to execute another one function like validation if that function is true only the next function should run otherwise it won't be run. Can any one help me?
Try this code :
Click Here
Try like
<a href="#" type="image" class="topopup" onclick="ShowDIV3();" >Click Here</a>
function ShowDIV3() {
if(true) //Here can use condition for validation
another_fun();
else
return false;
}
call your another function when the condition is true at your showDIV3 function
Try this way
onclick=" if (ValidateFunction()) return ShowDIV3();"
in jquery you could do it like this, this will also allow multiple handlers and better event normalization and improved separation of concerns.
$(".topopup").on("click", function(){
if (ShowDIV3()){
OtherStuff();
}
});
In your showDIV3() function at the end have something like this,
function showDIV3()
{
// Your process, and then at the end,
if(result)
{
// Call the other function
}
else
{
// Return false
}
}
Why returning there and executing ?? why not simply ??
function ShowDIV3(){
-----
if(resultBoolean){
proceedToanotherFunc();
}
}
Imho This would be more readable,other that writing logic there in html
Do your validation in ShowDIV3() function, and depending on the validation call your next function from inside the ShowDIV3() function
Just create more statement in your javascript like
function ValidateStatments() {
var val = document.getElementById("Value 1").value;
var val2 = document.getElementById("Value 2").value;
if(val != "Value 1"){
return false;
}
else if (val2 != "Value2){
return false;
}
return true;
}
and on your link add your javascript so if its true, it will allow the user to do the action onclick="return ValidateStatments();"
I have this simple javascript function which calls other 2 functions:
<script type="text/javascript" language="javascript">
function mainfunction() {
function1()
function2()
}
function function1() {
if (//some test) {
alert('test function1');
return false;
}
}
function function2() {
if (//some test) {
alert('test function2');
return false;
}
}
</script>
i call mainfunction() like this:
<form id="form1" runat="server" onSubmit="return mainfunction()">
or like this:
<asp:Button ID="btntest" runat="server" Text="test button" OnClientClick="return mainfunction()" />
btntest is a button that just calls some class which redirects to another page.
The problem
if i directly call any of the 2 functions function1() or function2() then the button will not get executed, which is what i want. but...:
When i call mainfunction() then alert in each function works but return false; doesn't seem to work because the button unfortunately gets executed.
why is that? how can i call the 2 functions and let their return false; work?
Your mainfunction is not returning anything. Try this:
function mainfunction() {
return function1() && function2();
}
As mentioned in the comments, to have your mainfunction working properly, you have to alter your other functions to always return either true or false:
function function1() {
if (//some test) {
alert('test function1');
return false;
}
return true;
}
EDIT
A funny hack to have both functions being executed and still return true only when both functions are true:
function mainfunction() {
return (function1() + function2()) === 2;
}
mainfunction doesn't return anything, you aren't using the return values from either function1 or function2
To prevent a form from submitting, you need the handler itself to return false, for example:
function mainfunction() {
function1();
function2();
return false; // prevent form submission
}
Returning undefined (what you're doing right now in the example in the question) is not sufficient.
Try this:
<asp:Button ID="btntest" runat="server" Text="test button" OnClientClick="mainfunction(); return false;" />
This will works as well:
<asp:Button ID="btntest" runat="server" Text="test button" OnClientClick="return mainfunction();" />
but you will need to change mainfunction:
function mainfunction() {
function1();
function2();
return false;
}
Why is this not reading through both if statements even though they are both true?
HTML
<textarea name="test">
Focus out to test prompts
</textarea>
jQuery
var disableA = 1;
var disableB = 1;
$('textarea[name="test"]').focusout(function() {
if (disableA == 1) {
disableX();
}
if (disableB == 1) {
disableY();
}
});
function disableX() {
alert('A is disabled');
}
function disabledY() {
alert('B is disabled');
}
Right now it will call for disableX(); but not disableY()
jsFiddle: http://jsfiddle.net/nCQQm/
In your second function you have called it disabledY, whereas you are calling back disableY()?
You spelled disableY wrong you need to rename it to disabledY()
There is no problem with the if statements.
Running your code gives the error:
ReferenceError: disableY is not defined
You have named the second function disabledY, and then you try to call disableY.