Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:35:32+00:00 2026-05-26T13:35:32+00:00

First: I’m JavaScript newbie. So.. I have basic form with password, repeat password, email

  • 0

First: I’m JavaScript newbie.
So.. I have basic form with password, repeat password, email and repeat email fields. I want to check if password is equal to repeat password. If it’s not, alert message appears and page reloads. Same for email and repeat email.
BUT if pass and repeat password aren’t equal AND email and repeat email aren’t equal, first alert message appears, then the second message (this time for email) appears too fast. I want to show only one alert message when both fields don’t match.

<script type="text/javascript">

           function checkFields() {
            var pass= document.getElementById('password');
            var reppass= document.getElementById('reppass');

            var email= document.getElementById('email');
            var repemail= document.getElementById('repemail');

            if (pass.value != reppass.value) {
               alert('Passwords dont match');
               window.location.reload();
            }

            if (email.value != repemail.value) {
                alert('Emails dont match');
                window.location.reload();
            }                   

            else if (pass.value != reppass.value && email.value != repemail.value) {
                alert('Both fields dont match');
                window.location.reload();
            }

        }   

  </script>

And the form:

<form onSubmit="checkFields()">
<p><label>Password:</label> <input name="password" id="password" required="true" type="password" /></p>
<p><label>Repeat password:</label> <input name="reppass" id="reppass" required="true" type="password" /></p> 
<p><label>Email:</label> <input name="email" id="email" required="true" type="email" /></p>
<p><label>Repeat Email:</label> <input name="repemail" id="repemail" required="true" type="email" /></p>
<p><input type="submit" value="Send"></p> 
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:35:33+00:00Added an answer on May 26, 2026 at 1:35 pm

    You can simply return from the if clauses like this:

    function checkFields() {
        var pass = document.getElementById('password');
        var reppass = document.getElementById('reppass');
    
        var email = document.getElementById('email');
        var repemail = document.getElementById('repemail');
    
        if (pass.value != reppass.value && email.value != repemail.value) {
            alert('Both fields dont match');
            window.location.reload();
        }
    
        if (pass.value != reppass.value) {
            alert('Passwords dont match');
            window.location.reload();
            return;
        }
    
        if (email.value != repemail.value) {
            alert('Emails dont match');
            window.location.reload();
            return;
        }
    
    }
    

    I like this style, because it prevents nesting if clauses. The downside is, that you have multiple return points that can be confusing – this heavily depends on the length of the function.

    EDIT

    Updated order of if blocks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I am using Windows XP. I have multiple hard drives and it
First of all, I know how to build a Java application. But I have
First off: I'm using a rather obscure implementation of javascript embedded as a scripting
First and foremost, I do not know RegEx but am trying to piece something
First of all, this is not schoolwork - just my curiousity as I for
First, I'm not a regex expert, so I'm pretty sure I'm doing something wrong.
First of all: this is not a homework assignment, it's for a hobby project
First off, I'm not really sure how much information is necessary to include because
First of all I am a newbie in C# Programming, and I need to
First, I created a xcode project and copied and pasted the CSS and javascript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.