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

  • SEARCH
  • Home
  • 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 6994993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:57:51+00:00 2026-05-27T19:57:51+00:00

I created a simple HTML form which takes input as username of the customer.

  • 0

I created a simple HTML form which takes input as username of the customer. It validates if it is indeed a valid username or not. If yes then it should go to the step2.php page if not then it should display an error and stay on the original page.

Here is my HTML code:

<form method="post" onsubmit="validateUsername();" id="myform" name="myform" action="step2.php">
      Choose username: <input type="text" id="username" name="username" />
      <input type="submit" value="Submit">
</form>

I am validating username by using a javascript function:

function validateUsername()
{
    var x=document.forms["myform"]["username"].value.length;

    if (x < 5)
    {
        alert('Username too short.');
        return false;
    }
    else
    {
        return true;
    }
}

My problem:

If the user enters a short username then it displays an alert message “Username too short”. When I press “OK” button on alert message then it is going to step2.php. Ideally it should send data to step2.php only when username is validated correctly. But this is not happening.

Anyone able to find the bug in my code?

  • 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-27T19:57:51+00:00Added an answer on May 27, 2026 at 7:57 pm

    Change:

    <form method="post" onsubmit="validateUsername();" id="myform" name="myform" action="step2.php">
          Choose username: <input type="text" id="username" name="username" />
          <input type="submit" value="Submit">
    </form>
    

    To:

    <form method="post" onsubmit="return validateUsername();" id="myform" name="myform" action="step2.php">
          Choose username: <input type="text" id="username" name="username" />
          <input type="submit" value="Submit">
    </form>
    

    Note the addition of the keyword return which should cancel the form from submitting if validateUsername returns false.

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

Sidebar

Related Questions

I've created my own theme using HTML, CSS, JS which contains FORM (Emp_ID,Emp_Name, Address
I have a form, which at the moment is a simple HTML based one,
I have created a Django app. I have a registration page(simple HTML form) in
How can I create a simple PHP file, which will retrieve the HTML and
I created a simple detail edit form earlier, and decided to data bind some
I have created a simple html page to test the localstorage and for the
I need to parse a simple HTML page with a simple form in it.
I have a simple ASP.Net MVC View which contains an FCKeditor text box (created
Right now I have simple form with a single input. I'm trying to use
My super simple backbone app is not picking up a form submission and acting

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.