How to display a popup message when a logged-out user tries to vote?

In this article, we will learn how to display a popup message when a logged-out user tries to vote using jQuery and various JavaScript methods. This is a common requirement in web applications where certain actions like voting, commenting, or rating should be restricted to authenticated users only.

To implement this functionality, we will use the jQuery library along with HTML and CSS to create interactive popup messages. When an unauthenticated user attempts to vote, we will show an informative popup that prompts them to log in first.

Syntax

Following is the basic syntax for checking user authentication status and displaying popup messages

// Check authentication status
if (!isLoggedIn) {
    // Display popup message
    $('.popup').show();
}

// jQuery event handler
$('#vote-button').click(function() {
    // Authentication check logic
});

The popup can be displayed using various methods like custom modal dialogs, JavaScript's built-in alert(), confirm(), or prompt() functions.

Using Custom Modal Popup

A custom modal popup provides better user experience with styled content and smooth animations. This approach gives you full control over the appearance and behavior of the popup message.

Example

Following example creates a custom popup modal that appears when an unauthenticated user tries to vote

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Custom Modal Popup for Authentication</title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <style>
      body { font-family: Arial, sans-serif; padding: 20px; }
      .vote-section { margin: 20px 0; }
      #voting-button { 
         background-color: #007bff; 
         color: white; 
         padding: 10px 20px; 
         border: none; 
         border-radius: 5px; 
         cursor: pointer; 
      }
      .overlay {
         display: none;
         position: fixed;
         top: 0;
         left: 0;
         width: 100%;
         height: 100%;
         background-color: rgba(0, 0, 0, 0.5);
         z-index: 999;
      }
      .popup {
         display: none;
         position: fixed;
         top: 50%;
         left: 50%;
         transform: translate(-50%, -50%);
         background-color: white;
         border: 1px solid #ccc;
         border-radius: 8px;
         padding: 30px;
         z-index: 1000;
         box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
         max-width: 400px;
         text-align: center;
      }
      .close {
         position: absolute;
         top: 10px;
         right: 15px;
         font-size: 20px;
         cursor: pointer;
         color: #666;
      }
   </style>
</head>
<body>
   <h2>Vote for Your Favorite Technology</h2>
   <div class="vote-section">
      <p>Which technology do you prefer?</p>
      <button id="voting-button">Vote Now</button>
   </div>
   
   <div class="overlay"></div>
   <div class="popup">
      <span class="close">×</span>
      <h3>Authentication Required</h3>
      <p>You need to be logged in to cast your vote.</p>
      <p>Please log in or create an account to participate.</p>
   </div>
   
   <script>
      const isLoggedIn = false; // Simulate logged-out user
      
      function submitVote() {
         alert('Vote submitted successfully!');
      }

      $(document).ready(function(){
         $('#voting-button').click(function(){
            if(!isLoggedIn){
               $('.overlay').show();
               $('.popup').show();
            } else {
               submitVote();
            }
         });
         
         $('.close, .overlay').click(function(){
            $('.overlay').hide();
            $('.popup').hide();
         });
      });
   </script>
</body>
</html>

When the user clicks "Vote Now", a modal popup appears with an authentication message. The popup can be closed by clicking the X button or clicking outside the modal

Vote for Your Favorite Technology
Which technology do you prefer?
[Vote Now]

(Popup appears: "Authentication Required - You need to be logged in to cast your vote.")

Using JavaScript Built-in Methods

JavaScript provides three built-in methods for displaying popup messages: alert(), confirm(), and prompt(). These are simpler to implement but offer limited styling options.

Example

Following example demonstrates all three JavaScript popup methods for handling unauthenticated voting attempts

<!DOCTYPE html>
<html lang="en">
<head>
   <title>JavaScript Built-in Popup Methods</title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <style>
      body { font-family: Arial, sans-serif; padding: 20px; }
      .button-group { margin: 20px 0; }
      .vote-btn { 
         background-color: #28a745; 
         color: white; 
         padding: 10px 15px; 
         border: none; 
         border-radius: 4px; 
         margin: 5px; 
         cursor: pointer; 
      }
      .vote-btn:hover { background-color: #218838; }
   </style>
</head>
<body>
   <h2>Different Popup Methods for Authentication</h2>
   <p>Try different popup approaches when voting without authentication:</p>
   
   <div class="button-group">
      <button id="alert-btn" class="vote-btn">Vote with Alert</button>
      <button id="confirm-btn" class="vote-btn">Vote with Confirm</button>
      <button id="prompt-btn" class="vote-btn">Vote with Prompt</button>
   </div>
   
   <script>
      const isLoggedIn = false; // Simulate logged-out user
      
      function submitVote() {
         alert('Vote submitted successfully!');
      }

      $(document).ready(function(){
         $('#alert-btn').click(function() {
            showAlert();
         });
         
         $('#confirm-btn').click(function() {
            showConfirmation();
         });
         
         $('#prompt-btn').click(function() {
            showPrompt();
         });
      });
      
      // Method 1: Using alert()
      function showAlert() {
         if (!isLoggedIn) {
            alert('You need to be logged in to vote!');
         } else {
            submitVote();
         }
      }
      
      // Method 2: Using confirm()
      function showConfirmation() {
         if (!isLoggedIn) {
            var result = confirm('You need to be logged in to vote. Do you want to log in now?');
            if (result) {
               alert('Redirecting to login page...');
               // window.location.href = '/login';
            }
         } else {
            submitVote();
         }
      }
      
      // Method 3: Using prompt()
      function showPrompt() {
         if (!isLoggedIn) {
            var username = prompt('Please enter your username to vote:');
            if (username !== null && username.trim() !== '') {
               alert('Validating user: ' + username);
               // Simulate authentication check
               submitVote();
            } else if (username !== null) {
               alert('Username cannot be empty!');
            }
         } else {
            submitVote();
         }
      }
   </script>
</body>
</html>

Each button demonstrates a different popup approach. The confirm() method provides yes/no options, while prompt() accepts user input

Different Popup Methods for Authentication
Try different popup approaches when voting without authentication:

[Vote with Alert] [Vote with Confirm] [Vote with Prompt]

(Alert: "You need to be logged in to vote!")
(Confirm: "You need to be logged in to vote. Do you want to log in now?" [OK] [Cancel])
(Prompt: "Please enter your username to vote:" [text input] [OK] [Cancel])

Comparison of Popup Methods

Following table compares the different approaches for displaying authentication popups

Method Customization User Experience Best Use Case
Custom Modal Full control over styling and content Professional, modern appearance Production applications with branded UI
alert() No customization possible Basic, interrupts user flow Simple notifications or debugging
confirm() No customization, binary choice Quick yes/no decisions Confirmation dialogs with redirect options
prompt() No customization, accepts input Can collect user data immediately Quick data collection or inline authentication
Authentication Flow User clicks Vote Check if user is logged in Logged in? No Yes Submit Vote Show Popup

Best Practices

When implementing authentication popups for voting systems, consider these best practices

  • User Experience Use custom modals for better visual integration with your website's design rather than browser default popups.

  • Clear Messaging Provide clear instructions about what the user needs to do (log in, register, etc.).

  • Action Buttons Include direct links or buttons to login/registration pages in your popups.

  • Accessibility Ensure

Updated on: 2026-03-16T21:38:54+05:30

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements