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>
000
27.3k10 gold badges74 silver badges103 bronze badges
asked Apr 5, 2013 at 3:30

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.

answered Apr 5, 2013 at 3:31
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.