0
<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
        <title>Javascript Form Testing</title>
</head>

<body>
Name: <input type="text" name="Name" value="Name" id="fname"> <br>
Email: <input type="text" name="Email" value="Email" id="mail" > <br>
Phone: <input type="text" name="Number" value="Phone Number" id="dvr" onchange="validatePhone();">
<span id="phoneval"> ?</span>

    <script type="text/javascript">
    phonenum = document.getElementById("dvr").value.length;
       function validatePhone() {
            if (phonenum == 10){
                document.getElementById('phoneval').innerhtml="<-- Valid";
            }
            else{
                document.getElementById('phoneval').innerhtml="<-- Not Valid";
            }
        }
    </script>
</body>
</html>

Hi, I was getting the error 'Uncaught TypeError: Cannot read property 'value' of null' when the tag was in the head, but since I moved it, it went away. However, now it outputs no error and does NOTHING. Please help?

Sorry for any formatting and things, new :/

2
  • I don't want to submit an answer since I'm actually at work atm. Firstly the big thing is that nothing is calling validatePhone().. if you just want it to run as soon as the dom is loaded (but before assets are loaded) then you could just put the call to validatePhone() right before </script> But you probably want to have all this run during the form submit event Commented Aug 13, 2012 at 4:32
  • Javascript is caseSensitive, it should be innerHTML Commented Aug 13, 2012 at 4:35

2 Answers 2

4

Edit:

Slightly different implementation that works: http://jsfiddle.net/SfTR2/ Make sure the JS is loaded after the HTML, or use window.onload

Original answer:

You need to get the length within the validation function:

   function validatePhone() {
        var phonenum = document.getElementById("dvr").value.length; //MOVE IT HERE
        if (phonenum == 10){
            document.getElementById('phoneval').innerHTML="<-- Valid";
        }
        else{
            document.getElementById('phoneval').innerHTML="<-- Not Valid";
        }
    }

As previously you were caching the empty length.

Also, as @su mentioned, you need to use innerHTML

Sign up to request clarification or add additional context in comments.

7 Comments

what if we assign var phonenum outside of function. i think that way is also okay.
Yeah it is, as long as you get the length (and the value) within the function.
if we define some variable outside the function then its scope would be global and that can be used in any function. see the demo
How to Make sure that the JS is loaded after the HTML ? is there any way. AFAIT JS is always loaded after pageload
Thank you, this works. I think I understand it, so when the input changes, it runs the function?
|
0

add

validatePhone();

to the end of the script (you defined the function but never called it). And change innerhtml to innerHTML

3 Comments

Don't forget that the assignment of phonenum is outside the function.
Alright, that seemed to work to an extent, but it doesn't update on change? (or blur, or whatever)
To make onchange work, you need to move var phonenum = document.getElementById("dvr").value.length; into the validatePhone function (as in Adam Heath's answer), so that it gets the updated value of phone number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.