Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Is there a way to use prompt and confirm box using javascript?

I'm new here and new to using javascript, I need help with creating a prompt that asks a user for their name and then displaying the name in a confirm box. If the name is correct it has to display a message that says "Hi" followed by their name. If its not correct it has to loop through and ask for name again. So I understand how to write a prompt and confirm box separately but I am struggling with putting the prompt and confirm together with a loop.

Here is what I have so far:

var name = prompt('What is your name?');
var confirm = confirm('Is your name: ' + name);

function myFunction() {
  var txt;

  if (confirm === true) {
    txt = "You pressed OK!";
  } else {
    txt = "Input your correct name";
  }
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You basically just need to bootstrap myFunction, and also place the prompts inside the myFunction so they get called again if the confirmation is false.

eg.

function myFunction() {
  var name = prompt('What is your name?');
  var conf = confirm('Is your name: ' + name);
  if (conf === true) {
    alert("You pressed OK!");
  } else {
    alert("Input your correct name");
    myFunction();
  }
}

myFunction();

about 4 years ago · Juan Pablo Isaza Report

0

I have encapsulated your code a function and call the function again if the confirmation is no.

Here, I am passing an extra parameter, which confirms if the questions are asked first time. If it not asked first time, then it will show Input your correct name. also.

function askAndConfirm(isFirst){
  var question="";
  if(!isFirst) question = "Input your correct name. ";
  var name = prompt(question + 'What is your name?');
  var isConfirm = confirm('Is your name: ' + name);

  if (isConfirm === true) {
    alert("Hi! "+name);
  } else {
    askAndConfirm(false);
  }
}
askAndConfirm(true);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!