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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:08:00+00:00 2026-06-10T01:08:00+00:00

I’ve been developing my first website, and I’m having issues with a javascript validation

  • 0

I’ve been developing my first website, and I’m having issues with a javascript validation script. My code:

<html>
    <head>
      <title>Territory</title>
    </head>
    <body>

      <script type="text/javascript"> //Main Javascript function giving me issues
    function validate(form)
    {
      fail = validateTerrNum(document.checkOut.numberOut.value);
      fail += validateFirstName(document.checkOut.fName.value);
      fail += validateLastName(document.checkOut.lName.value);
      if (fail == "") 
    return true;
      else 
      {
        alert(fail);
        return false;
      }
    }
        </script>
        <script type="text/javascript>
        function validateTerrNumber(field)
        {
     if (field == "") return "No territory number was entered. \n";
     else if (field.length > 3)
    return "Territory numbers must be less than four characters. \n";
    else if (/[^0-9]/.test(field))
        return "Only 0-9 allowed in territory number. \n";
    return "";
        }

        //Note: Does not sanitize string
        function validateFirstName(field)
        {
     if (field == "") return "No first name was entered. \n";
     return "";
        }

        //Again, note that this function does not sanitize string
        function validateLastName(field)
        {
     if (field == "") return "Not last name was entered. \n";
     return "";
        }
        </script>

        <table border="0" cellpadding="2" cellspacing="5">
     <th colspan="2" align="center">Check Out</th>
      <form name="checkOut" method="post" onSubmit="validate(this)"> 
    <tr><td>Territory Number</td><td><input type="text" name="numberOut"     tabindex="1" maxlength="3" size="3" /></td>
                    </tr><tr><td>First Name of Holder</td><td><input type="text" name="fName" tabindex="2" maxlength="15"/></td>
                    </tr><tr><td>Last Name of Holder</td><td><input type="text" name="lName" tabindex="3" maxlength="15" /></td>
                    </tr><tr><td><input type ="checkbox" name="specialC" tabindex="4" value="Yes"/> Special Campaign</td>
                    </tr><tr><td></td><td><input type="submit" value="Check Out" /></td>
                </form>
            </table>
</body>
</html>

It is obviously poorly formatted – result of copy and paste. To be more specific, this issue is with the javascript validation. When I hit “Check Out,” the validation does not prevent it when I put in bad values. The script is quite simple, but for some reason I can’t figure out what is going on with it (I’m fairly new to javascript).

I know the issue is not my browser as I have many javascripts running throughout it. The only thing I can think of is that the form does not have a method. It will be another validation, but written in php – the javascript is really just a prevalidation for the client.

Any help would be appreciated.

-Mlagma

  • 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-10T01:08:01+00:00Added an answer on June 10, 2026 at 1:08 am

    To stop the submit going ahead after validation fails you need:

    onSubmit="return validate(this)"
    

    instead of your current code:

    onSubmit="validate(this)"
    

    Your validate() function already returns true or false, but those values were not being returned from the event handler itself. If you think of the code between the quotes of onSubmit="" as the body of a function this makes sense.

    And, asTeemu pointed out, your validate() function tries to invoke a function called validateTerrNum when actually you’ve declared that function as validateTerrNumber.

    With these problems corrected you can see it working here: http://jsfiddle.net/nnnnnn/Wvcy3/

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.