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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:44:21+00:00 2026-06-15T21:44:21+00:00

I am trying to validate input on an HTML form. I am trying to

  • 0

I am trying to validate input on an HTML form. I am trying to use one script to validate various forms that have several similar elements. All in all there are about 15 different fields but I am only putting 3. Below works on the form that has all of the elements but fails on any form that does not have a specific field.

if (form.name.value == ""){ // Check if blank
    alert( "\nEnter a name." );
    form.name.focus();
    return false; // Stop the form processing.
    } 

else if ( !/^\w{10}$/.test( form.phone.value )) { // Check for phone
    form.name.style.border=""
    alert( "\nEnter a valid phone number!" );
    form.phone.focus();
    return false; // Stop the form processing.
    } 

else{
 return true; // Submit!

Since this only works if all are present what i want to do is validate only if it exists. I have tried nesting the validation under a statement that should check for the element before trying to validate it and if the element doesn’t exist it should move on to the next.

if (document.forms[0].name){ //does field exist?
    if (form.name.value === ""){
        alert( "\nEnter a name." );
        form.name.focus();
        return false; // Stop the form processing.
        }
    else{
        return true; //field ok move on.
        }
    }

else if (document.forms[0].phone){
    if ( !/^\w{10}$/.test( form.phone.value )) {  user
        form.name.style.border=""
        alert( "\nEnter a valid phone number!" );
        form.phone.focus();
        return false; // Stop the form processing.
        } 
    else{
        return true; //field ok move on.
        }
    }
else{
       return true; // Submit!
}

Any help help would be appreciated!

  • 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-15T21:44:22+00:00Added an answer on June 15, 2026 at 9:44 pm

    At present you are checking that (document.forms[0].name) is available and then if this is dead it check if (document.forms[0].phone) is true and then…… and then if none are true it resorts to the ‘else’ and returns are true. The use of if else means that you only bother with anything after if this part has failed.

    You could try.

    if (document.forms[0].name) {
        if ( !/^\w{10}$/.test( form.phone.value )) {  user
            form.name.style.border=""
            alert( "\nEnter a valid phone number!" );
            form.phone.focus();
            return false; // Stop the form processing.
        } 
    }
    if (document.forms[0].blah) {
        do stuff...
        if fail
            return false;
    }
    return true;
    

    As the function is going to return as true anyway, you don’t need to state return true anywhere else (also this will stop the process as it has returned and stopped). The only reason to stop the process if things have gone wrong.. hence return false;

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

Sidebar

Related Questions

I have a form that i'm trying to use the jquery validate plugin. My
I'm trying to use a RegularExpressionValidator to validate a TextBox input length. I have
Right now I have simple form with a single input. I'm trying to use
I'm trying to use a .NET Regex to validate the input format of a
I am trying to validate a form in that if there is no text
I have not really used JavaScript before but I am trying to validate form
I am trying to validate an html form using javascript the code is bellow
I'm trying to create a registration form and validate input fields using javascript, but
I have a form dynamically created using Jquery and i am trying to validate
I'm trying to use jQuery validation plugin to validate a few form fields by

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.