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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:07:15+00:00 2026-06-08T02:07:15+00:00

This phone number validator doesn’t work :/. Why? Ideally, if the optional phone field

  • 0

This phone number validator doesn’t work :/. Why?

Ideally, if the optional phone field is not null, it should proceed to validate the form.

The phone field is optional, and isn’t required.

Validating the form:

  • the phone field is optional. this means that it is not required.
  • it should ignore comparing everything except numbers.
  • if the digit count is != 10 numbers, it should display the error.
  • if the count is equal to 10 digits, it should pass onto name.php (see the HTML)

Javascript Code:

function validateForm() {
var x=document.forms["form"]["name"].value;
if (x==null || x=="")
  {
  alert("Name is required.");
  return false;
  }

var y=document.forms["form"]["email"].value;
var atpos=y.indexOf("@");
var dotpos=y.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=y.length)
  {
    alert("Valid email required.");
    return false;
  }

var formValue = document.forms["form"]["number"].value;
var regExpressionValue = /[^\d.]/g;
if (formValue !== null)
{
  if (regExpressionValue.test(formValue) !== true)
  {
    alert("Optional phone number invalid. Example: [1234567890].");
    return false;
  }
}

return true;

}

HTML:

<form class="form" id="form" name="form" method="post" action="name.php" onsubmit="return validateForm()" />
<input type="text" name="name" id="name" />
<input type="text" name="email" id="email" />
<input type="tel" name="number" id="number" />
<button type="submit" value="Send" />Sign Up</button>
<div class="spacer"></div>
</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-06-08T02:07:16+00:00Added an answer on June 8, 2026 at 2:07 am

    You have bad regexp and string presence check for phone number. Check this out:

    function validateForm() {
       var x=document.forms["form"]["name"].value;
       if (x==null || x=="")
       {
         alert("Name is required.");
         return false;
       }
    
      var y=document.forms["form"]["email"].value;
      var atpos=y.indexOf("@");
      var dotpos=y.lastIndexOf(".");
      if (atpos<1 || dotpos<atpos+2 || dotpos+2>=y.length)
       {
         alert("Valid email required.");
         return false;
       }
      var formValue = document.forms["form"]["number"].value;
      if (formValue)
      {
      var regExpressionValue = /^(\d-?){10}$/g;
        if (regExpressionValue.test(formValue) !== true)
        {
          alert("Optional phone number invalid. Example: [1234567890].");
          return false;
       }
     }
    
    return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Telephone Manager give me Null phone number while i have use this code -
I want to validate a input field. The user should type in a phone
I want to validate a United States phone number, this is what I have
I have a sheet A that looks something like this: Name Phone Number Title
I have this code: # Compare phone number phone_pattern = '^\d{3} ?\d{3}-\d{4}$' phoneNumber =
If I have an international phone number such as this: 0541754301 how can I
Let me first say, we validate every field on the server side, so this
I'm thinking about adding some validation/filtering to the phone number field of my Zend
I'm trying to validate a phone number against a RegEx. I was provided with
Hi I am trying to validate a phone number for a cookie via java-script.

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.