Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
using HTML variables with JavaScript

I'm creating a sign up system and tying to make a code using JavaScript that checks the phone number the user is entering and only allows it to be used if it is 10 characters long.

this is my code so far:

JavaScript:

<script type="text/javascript">
        function phoneIsValid()
        {
            var phoneL = document.getElementById("phone").length;
            if (phoneL != 10)
            {
                document.getElementById("phoneErr").innerHTML = "invalid phone number"
                return false;
            }
            else
            {
                document.getElementById("phoneErr").innerHTML = "";
                return true;
            }

        }
    </script>

HTML:

<input type="text" id="phone" name="phone" />
            <a id="phoneErr"></a>

the problem is that it always replys that the phone number is not 10 cahracters long, even if it is. what am I doing wrong? I dont know much Javascript and I cant find a solution on the internet.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You don't even need JS for this:

<form>
  <input type="text" minlength="10" maxlength="10" required placeholder="Phone number (10 digits)" />
  <button>Send</button>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

function phoneIsValid()
{
    var phoneL = document.getElementById("phone").value.length;
    if (phoneL != 10)
    {
        document.getElementById("phoneErr").innerHTML = "invalid phone number"
        return false;
    }
    else
    {
        document.getElementById("phoneErr").innerHTML = "";
        return true;
    }
}
function checkPhone() {
  if(phoneIsValid()) {
    alert('Phone number is valid');
  } else {
    alert('Phone number is NOT valid');
  }
}
<input type="text" id="phone" name="phone" />
            <a id="phoneErr"></a>
            
<button type="button" onclick="checkPhone()">Check it</button>

As mentioned in comments - you need to validate value of the textbox, not textbox itself.

about 4 years ago · Juan Pablo Isaza Report

0

Try getting the length of the "value".

var phoneL = document.getElementById("phone").value.length;
                                                ˄
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!