1

I have googled a lot and there seems to be a lot of talk regarding jQuery and the problem I'm having, however I'm using purely JavaScript.

I am just having a problem with not being able to validate the input because it is undefined.

**If my HTML is needed I will post, but i think this can be solved without it.

function fnValidation()
    {
        var validator = /^[a-zA-Z]*$/; //regex
        var input = document.getElementById("fn").innerHTML; 

        if (!(input.match(validator)))
        {
            alert("Please enter only alphabetical characters!");
        }
        printReceipt();
    }

I dug this old code up from a pervious problem and decided to salvage the username validation but am now having the problem described above

3
  • 4
    inputs don't have innerHTML. They have value Commented Nov 13, 2015 at 21:44
  • ohhh! is it just my variable name being a keywork in javascript? Commented Nov 13, 2015 at 21:44
  • 2
    try document.getElementById("fn").value instead. Commented Nov 13, 2015 at 21:44

3 Answers 3

1

Input element doesn't have innerHTML, because all it has its value inside value attribute on html & value property Inside DOM.

You should use

document.getElementById("fn").value
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for clearing this up. I was under the impression .innerHTML would return the HTML inside the element
@SpencerWieczorek Thanks for heads up..I editted that code from mobile which accendently made starting letter in caps..
0

should be var input = document.getElementById("fn").value and not innerHTML

Comments

0

.innerHTML will return the string inside that id tag example here: jsfiddle

<h1 id="fn">Hello World!</h1> the id is 'fn' the innerHTML is Hello Wold!

Comments

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.