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 8946539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:32:49+00:00 2026-06-15T12:32:49+00:00

I am trying to use JavaScript to validate forms but if the form doesn’t

  • 0

I am trying to use JavaScript to validate forms but if the form doesn’t validate, I don’t want the form to be sent to the “action” page.

The validator:

<script>
    function formSubmit()
    {
    document.getElementById("signup_form").submit();
    var x=document.forms["signup_form"]["tname"].value;
    if (x==null || x=="")
      {
      alert("First name must be filled out");
      return false;
      }
    }
</script>

The form itself:

<form action="some_file.php" method="post" id="signup_form" name="signup_form" onsubmit="return formSubmit()">

But all this does is if the tname field empty, it will return an alert but as soon as the user hits ok, the form then redirects to some_file.php. What have I missed here?

The submit button:

<a href="" onclick="return formSubmit(); return false" class="purplebutton">Signup</a>

So what have I missed? How do I avoid this in the future?

  • 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-06-15T12:32:51+00:00Added an answer on June 15, 2026 at 12:32 pm

    Try this:


    function formSubmit()
    {
        var x=document.forms["signup_form"]["tname"].value;
        if (x=="")
        {
             alert("First name must be filled out");
             return false;
        }
        return true;
    }
    

    *Changes made to your code: *
    – There is no need for formSubmit to try to submit the form! Just perform the validation checks and return true if you want the submission to proceed or false otherwise.

    • Also you do not need to check for the x == null case. If the textbox is empty, its value will be “”.

    • What might be needed though is a check that x does not just contain spaces. If you do not care about supporting older browsers you can use the new Javascript trim function. If you care about backwards compatibility, either try one of the many javascript libraries that offer trim or the following:


    function formSubmit()
    {
        var x=document.forms["signup_form"]["tname"].value.replace(/^\s+|\s+$/g, '');
        if (x=="")
        {
             alert("First name must be filled out");
             return false;
        }
        return true;
    }
    

    If I just wanted to avoid submitting spaces I would have left the var x= bit as it was and added the replace in the check making it if(x.replace(/\s/g, '') == ""). However, I also want to trim the input so that if the user enters ” user1″ or “user1 ” the form will send “user1”.

    You can see the three different versions versions working here:
    http://jsfiddle.net/9LPfb/2/
    http://jsfiddle.net/9LPfb/3/
    http://jsfiddle.net/9LPfb/6/

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

Sidebar

Related Questions

I have not really used JavaScript before but I am trying to validate form
I'm trying to use Javascript's built-in replace() method, but for some reason it won't
I am trying to use an HTML form and javascript (i mention this, because
I am trying to validate my form using jquery but I cannot get it
I am trying to use a textarea form entry which I have some javascript
I'm trying to use jQuery validation plugin to validate a few form fields by
I have a form that i'm trying to use the jquery validate plugin. My
I'm trying to use the validate plugin but I guess it's not setup correctly.
I have this chunk of javascript I am trying to use to validate my
Possible Duplicate: Validate email address in Javascript? I am trying to use this 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.