If statement in .aspx page - javascript

I am attempting to include an If statement in some Javascript on my .aspx page.
I declare FinishedPacking at the beginning of the page as False. Then when a user clicks the orderSubmit button, the if Statement evaluates if the value is indeed false, if so, display an alert. So far the if statement does not work. If I use just the alert with no if statement it displays the alert:
var FinishedPacking = false;
$("#orderSubmit").click(function (e) {
if (FinishedPacking = false) {
alert("The order is not finished.")
}
ClearScreen();
GetOrder();
}):
As stated if I do not include the if statement, the alert works when I click the order button. Not sure why this simple If statement is not being picked up.

You need the double-equals
if (FinishedPacking = false)
should be
if (FinishedPacking == false)

Try this
var FinishedPacking = false;
$("#orderSubmit").click(function (e) {
if (FinishedPacking == false) {
alert("The order is not finished.")
}
ClearScreen();
GetOrder();
}):

You need 2 ='s signs
if (FinishedPacking == false) {
You are trying to ASSIGN false to the variable FinishedPacking inside your condition (which is wrong) you want to COMPARE the values.

Related

Trying to run javascript with a href for validation

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/

jquery code working but not the first time

I'm trying to build a small login system using jquery (since it is for testing purposes only and the user and password won't change) So i made a form and when you click the button i test whether the details are correct. if so you will get send to the next page. If not i give an alert.
It's working but i have something weird. The first time you visit the site and fill in the details it does nothing. The second time (after submitting) it works like it should.
Does someone know why?
Here is the code:
function controllogin() {
event.preventDefault();
var username = $("#gebruikersnaam").val()
var password = $("#wachtwoord").val()
if (username=="leerkrachten" && password=="leerkrachten") {
alert("welkom leerkrachten");
goToUrl();
}
else if (username=="leerling" && password=="leerling") {
alert("welkom leerling");
}
else {
alert("verkeerde gegevens ingevuld");
}
};
function goToUrl() {
alert("zoeken naar pagina");
window.location = 'leerkrachten/vakken.html';
};
Instead of onclick="controllogin();" try something like this:
$('document').ready(function() {
$('#submit').click(function(event) {
event.preventDefault();
var username = $("#gebruikersnaam").val()
var password = $("#wachtwoord").val()
if (username==="leerkrachten" && password==="leerkrachten") {
alert("welkom leerkrachten");
goToUrl();
}
else if (username==="leerling" && password==="leerling") {
alert("welkom leerling");
}
else {
alert("verkeerde gegevens ingevuld");
}
});
});
Also use === instead of == as operators to compare the strings, it prevents you from some weird results when comparing different types. Maybe that's why it doesn't work the first time, but does the second time. Otherwise I don't know why this happens.
But actually I'd have to say: NEVER do client-side login validation and NEVER do login validation with an unencrypted password (not even server-side)!

If condition is true show button

I'm trying to make a button appear if condition "isauthenticated" is "true".
used this as a reference, but not sure what's wrong with my code.
<button id="authentic" type="hidden">test</button>
<script>
window.onload = function() {
var logedIn = ('{{isauthenticated}}')
if(logedIn == "true") {
document.getElementById('authentic').style.display = 'block';
} else {
document.getElementById('authentic').style.display = 'none';
}
}
</script>
Ok Guys, sorry for the bad question. This is what I was looking for
<script>
var isAuth = ('{{ user.isauthenticated}}');
if (isAuth) true;
document.getElementById("menuProfile").style.display="block";
document.getElementById("showProfile").style.display="none";
</script>
if logedIn is a boolean value you need to remove the "'s from the if(logedIn == "true") { line
or just check:
if(logedIn) {...
Please refer to Boolean Javascript documentatiion:
If the Boolean object has no initial value, or if the passed value is one of the following:
0,-0,null,"",false,undefined,NaN the object is set to false. For any other value it is set to true (even with the string "false")!
see what value does logedIn have before the if!

Incorrect Entry into database

I have a dropdownlist (with values 'CMC' & 'CHF')and two textbox's.
when i select an option from dropdownlist, a text box control appears (visibility is written in javascript).
when i enter a number into this textbox and hit the sumbit/next button it should save this information in database.
The logic works for the one option but its not working for the other!
Both the options have textbox's assoicated with them, which are visible only when the respective option is selected.
the frontend logic works (i.e. the visibility) but when I enter a number for 'txt_HFMN' ( the option for this text box to appear
in the dropdown is 'CHF' and 'CMC' for textbox 'txt_HFNumber')
Here is the code in .cs file:
if (txt_HFNumber != null)
{
strHFNUM = txt_HFNumber.Text;
}
else if (txt_HFMN != null)
{
strHFNUM = txt_HFMN.Text;
}
else
{
strHFNUM = string.Empty;
}
I tried to debug it, to pin point the error. The above condition checks only for the 'txt_HFNumber', it never
checks the 'else if' part. even though I have entered a value in 'txt_HFMN' it checks for 'txt_HFNumber' and since
'txt_HFNumber' doesn't exist in the front end/no value is entered in this textbox , it inserts a 'null' into the
database instead of 'txt_HFMN' entered value!
Advise.
Really appreciate your help.
I think you might want something a bit more like this, you're checking to see if the textbox itself exists:
if (!string.IsNullOrEmpty(txt_HFNumber.Text) )
{ strHFNUM = txt_HFNumber.Text; }
...
if (txt_HFNumber != null) {
strHFNUM = txt_HFNumber.Text;
} else {
strHFNUM = string.Empty;
}
if (txt_HFMN != null) {
strHFNUM = txt_HFMN.Text;
} else {
strHFNUM = string.Empty;
}
If you are setting txt_HFNumber and txt_HFNumber you will never get to the txt_HFNumber in your statement, seperate the 2, but the point is your setting the same string twice, strHFNUM is being over ridden so if you have both set on this occasion only txt_HFMN will be set to strHFNUM
or you could do
if (txt_HFNumber != null && xt_HFMN == null) {
strHFNUM = txt_HFNumber.Text;
} else {
strHFNUM = txt_HFMN.Text; //IF xt_HFMN is NULL it will set to null thus setting the empty string is irrelevent
}

trouble with confirmationboxes when submitting a form

I'm trying to compare some values of select fields.
When submitting my form I want to check whether the values are equal.
Well ... I've got two problems here.
The values aren't compared the first time I submit the form.
Example: I select option3 for id='idx' and id='idy'. In my case option3 is equal to option3. When I submit the form there should be a confirm box because the values are equal. No confirmationbox appears. I call the site again with the set values and submit the form; the confirmationbox appears. I chose option 1 for id='idx'. This would mean that the values of id='idx' and id='idy' are not equal. But now the box appears again.
What I want is that when I submit the form the values should be compared. In case they're equal a confirmationbox appears.
2.Somehow my code only checks for one condition. But it is possible that the two cases in my code can be true. How would I solve it?
This is my code:
var x = $('#idx option:selected').val();
var y = $('#idy option:selected').val();
var z = $('#idz option:selected').val();
$('#myform').submit(function() {
if (x === y ) {
if (confirm('text') == true){
return true;
} else {
return false;
}
}
if (x === z ) {
if (confirm('text') == true){
return true;
} else {
return false;
}
}
});
If there are any questions, please ask.
Thank you for your help.
First, you should put your $('...').val() lines inside the submit function, or else they will get the value of the select boxes when the page loads.
Second, once you return true or false, it will exit out of that submit function, so you should return false when it fails, or else continue until the end of your conditionals, then have one return true at the end.

Categories