2

i'm trying to write a credit card validation script using the luhn algorithm, but i can't even manipulate the string input right to even get myself started. just trying to take out hyphens and spaces from the string but it keeps saying in the debugger that my function has no replace method? im not a programmer, just trying to get through this class.....

here's my code, there might be a line or 2 in there for testing purposes that i forgot to remove.

<script type="text/javascript">

  function fixString(){

    //get credit card number
    var ccNumber = document.getElementById("ccNumber");

    //remove hyphens and spaces
    var ccNumber = ccNumber.replace(/-/g, "");
        //.replace(/\n/g, "");



    show.innerHTML = ccNumber.value;

  }

</script>

 <body>
<form action="#">
  <p><label>Enter credit card number here:<input id="ccNumber" type="text">
    </label> <input value="Validate" onclick="fixString()" type="button"> </p>
</form>
<p id="show"></p>

</body>

1 Answer 1

10

You definitely have the right ideas and are on the right track!

var ccNumber = document.getElementById("ccNumber");

This just gets the element. It doesn't get the value.

You want this.

var ccNumber = document.getElementById("ccNumber").value;

Then here, show.innerHTML = ccNumber.value;, remove .value since ccNumber is the string.

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

1 Comment

ok, i'm getting pretty close, but i've hit anotehr bump... after i reversed it, i don't know how to multiply the values correctly... i keep getting NaN in my for loop. what's wrong?

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.