I'm new to here. So I was just making a simple code just for fun, but then I was confused why the if and else function can't work. Can anybody help me?
var name = prompt("Please enter your name","Your name");
if(name != null) {
alert("Hello, " + name + "!");
}
// Google and Youtube redirect
if (name.onclick == true); {
confirm("If you like to go to google, Click OK. If you like to go to youtube, Click CANCEL")
};
if ( confirm == true ); {
window.location.replace("https://www.google.com/")
} else {
window.location.replace("https://www.youtube.com/")
};
You need to assign whatever confirm returns to a variable, and check that.
let answer = confirm("If you like Apples, click OK. If you like Bananas more, Click CANCEL");
if (answer === true) {
console.log('you like apples');
} else {
console.log('you like bananas');
}
Related
Just not sure why this doesn't work? the prompt work but nothing after.
function btn(){
prompt('do you like banana?');
if("yes"){
alert = "good";
}
else {
alert = "to bad, try again!";
}
};
Just condensing Sabar's answer and changing alert = ... to the method version (which I'm guessing is what you wanted), it could also be written as:
With a prompt(), which would return what the user enters (random is possible)
function btn(){
if(prompt('do you like banana?') == "yes"){
alert("good");
} else {
alert("to bad, try again!");
}
};
Or with a confirm()
function btn(){
if(confirm('Would you like banana?')){
alert("good");
} else {
alert("to bad, try again!");
}
};
You could take it even further if you like with:
function btn(){
confirm('Would you like banana?') ? alert("good") : alert("to bad, try again!");
};
Or even one step more into the realm of harder to read (as suggested by nnnnnn)
function btn(){
alert(confirm('Would you like banana?') ? "good" : "to bad, try again!");
};
prompt is a function which returns what the user typed, so
function btn(){
userAnswer = prompt('do you like banana?');
if(userAnswer === "yes"){
alert("good");
}
else {
alert("to bad, try again!");
}
};
this should work.
Also, alert is a function, and by writing alert = "asdad", you are instead making it just a string. Also, if("yes") just checks if the string "yes" is evaluated to true or false, and it is always evaluated to false. You may want to take a look at some online course for Javascript, for example this
Are you sure what you want is a prompt?
From your code i understand that it's a confirm.
function btn() {
var r = window.confirm("do you like banana?");
if (r == true) {
alert("good");
} else {
alert("to bad, try again!");
}
};
If you want a prompt then use this:
function btn() {
var r = prompt("do you like banana?", "I'm a monkey!!!");
if (r != null) {
alert("good. " + r);//good. I'm a monkey!!!
}
else
alert("to bad, try again!");
};
Prompt returns null if you have cancelled it, and it select value from textbox if you select ok.
function btn(){
var response = prompt('do you like banana?',"yes");
if(response){
alert("good");
}
else {
alert("too bad, try again!");
}
};
You have to save the value of prompt to a variable:
function btn(){
var response = prompt('do you like banana?');
if(response == "yes"){
alert("good");
}
else {
alert("too bad, try again!");
}
};
This is my code, I'm attempting to set up a combo lock for a game that will unlock when the user inputs the correct answer. However, when the user inputs something it will always return "The door is unlocked." What am I missing or doing wrong here?
function comboLock(){
this.comboLock = ["666"];
this.falseInput = /[^1-5, 7-9]/g;
}
comboLock.prototype.unlock = function(){
if (this.comboLock != this.falseInput){
return alert("the door is unlocked");
}
else if (this.comboLock === this.falseInput){
return alert("that is not the correct combonation");
}
}
$(document).ready(function(){
$("#button-unlock").click(function(event) {
event.preventDefault();
if (this.comboLock = "666"){
return alert("the door is unlocked");
}
else if (this.comboLock != "666"){
return alert("that is not the correct combonation");
}
})
})
This prompt was working perfectly well up until I updated some other javascript. I don't know how I messed it up. This function is declared in the body tag to run 'onload'.
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
}
else if(answer == "no") {
alert('That is okay! See our links above and below to learn more!');
}
else if(answer == null || answer == "") {
alert('Please enter an answer.');
funcPrompt();
}
else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
Now suddenly I'm getting this error and the prompt won't appear.
Not certain why you are getting a null but if you are trying to avoid the null, use this:
answer = (answer?answer:'').toLowerCase();
If we click on Cancel, prompt will return null and one can not apply toLowerCase on null(Will cause an exception!)
Add a condition answer===null before all other conditions and return to stops the execution of a function
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
if (answer === null || answer === "") {
alert('Please enter an answer.');
funcPrompt();
return;
}
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else if (answer == "no") {
alert('That is okay! See our links above and below to learn more!');
} else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
funcPrompt();
In your case, better use a confirm instead of a prompt
function funcConfirm() {
var answer = confirm("Are you a photographer?");
if (answer === true) {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else {
alert('That is okay! See our links above and below to learn more!');
}
}
funcConfirm();
I have a textbox that looks like this
<%= Html.TextBoxFor(model => model.ContactAddress, new { autocomplete = "off", maxlength = "75" })%>
in my javascript i have this
$('.save_button_cont').click(function (e) {
var addr1 = $('#ContactAddress').val();
if(addr1.indexOf("box") > -1) {
alert("HEY NO P.O. BOXES ALLOWED ");
}
document.forms[0].submit();
});
i was hoping that this would pop up the alert before posting if the user had 'box' in the textbox. it doesnt work though.
I want to show an alert if the user enters the string 'box' in their address (the textbox).
EDIT: on the submit i am going to use a confirm box to verify that the user wants to continue. thanks for the help
Using
$("#textboxid").blur(function(){
if( $("#textboxid").val() == "box"){
alert("box typed!");
}
});
this will make your life easy!
Try this -
$('.save_button_cont').click(function (e) {
var addr1 = $('#ContactAddress').val();
if(addr1.indexOf("box") > -1) {
alert("blah");
}else {
alert('Bleh');
}
alert("TEST");
$('form').eq(0).submit();
});
You can do this:
$('.save_button_cont').click(function (e) {
var addr1 = $('#ContactAddress').val();
if (addr1.indexOf("box") > -1) alert("blah");
else alert('Bleh');
$('#formID').submit();
});
Instead of:
if(~addr1.indexOf("box")) {...} else {...}
Try
if(addr1 === "box") {...} else {...}
You could use regex to test the string for the occurrence of "box". This will allow you to do a case-insensitive search:
$('input[type="submit"]').click(function(e) {
e.preventDefault();
var input = $('#test').val();
if (/box/i.test(input)) {
alert("bad");
} else {
alert("good");
$("form").submit();
}
});
You should also put the "submit" function in your "else" statement so the form only submits on validation success.
NOTE: this could cause unwanted issues if the user happens to live on "box street".
I am trying to use the jQuery Credit Card Validator to validate credit cards.
The basic usage is given as
$('#cc_number').validateCreditCard(function(result)
{
alert('CC type: ' + result.card_type.name
+ '\nLength validation: ' + result.length_valid
+ '\nLuhn validation: + result.luhn_valid');
});
I looked on the demo JS file included on that site and couldn't make head nor tail.
What I am trying to achieve is onkeyup of input, do something depending on what card type is caught:
//on key up of input
if (card == valid)
{
if (card == visa)
{
//do something
}
else if (card == mastercard)
{
//do something
}
// repeat for rest of card types
}
else
{
//Just print an error
}
I know it's fairly basic stuff, but can anybody help me with how to achieve?
my HTML:
<input type="text" id="cc_number" />
Developer of jQuery Credit Card Validator here.
jCCV binds the keyup event so you don’t need to do it. (actually it’s a little more complicated than that — all you need to know is that every time the value of the field changes, your callback function is executed).
$('#cc_number').validateCreditCard(function(result)
{
// this will execute everytime the value of the `#cc_number` field changes
if (result.length_valid && result.luhn_valid) {
if (result.card_type.name == 'visa') {
// do something
} else if (result.card_type.name == 'mastercard') {
// do something
}
// repeat for rest of card types
} else {
// just print an error
}
});
try something like this:
$("#cc_number").on("keyup", function() {
$(this).validateCreditCard(function(result) {
alert('CC type: ' + result.card_type.name
+ '\nLength validation: ' + result.length_valid
+ '\nLuhn validation: ' + result.luhn_valid);
});
if (result.card_type.name) {
if (result.card_type.name == visa)
{
//do something
}
else if (result.card_type.name == mastercard)
{
//do something
}
// repeat for rest of card types
}
else {
//Just print an error
}
});
Use this one by Stripe: https://github.com/stripe/jquery.payment
Much better.