Reputation: 48
I am producing a page that takes in a set of card details as a form. The fields need to be validated manually (without HTML 5) using JavaScript. The one problem I am having is with the 16 digit card number field.
I can't figure out why I am getting this inconsistant failier to validate. What is the problem with my code? Thanks.
<form onsubmit="submitDetails()">
<input type="text" id="name" placeholder="Name" style="left:200px;position: absolute;"><br />
<input type="text" id="cardNo" placeholder="Card Number" style="left:200px;position: absolute;"><br />
<input type="text" id="expMonth" placeholder="MM" style="left:200px;position: absolute;">
<input type="text" id="expYear" placeholder="YYYY" style="left:350px;position: absolute;"><br />
<input type="text" id="secCode" placeholder="Security Code" style="left:200px;position: absolute;"><br />
<input type="submit" id="submit" value="Submit Details" style="left:200px;position: absolute;">
</form>
var cardNo = document.getElementById('cardNo').value;
if ((cardNo.toString().length != 16) || (isNaN(parseInt(cardNo)))){
errorMsg += "\n\u2022 Enter a Card Number of 16 digits.\n"
}
Upvotes: 0
Views: 82
Reputation: 1574
From MDN:
If the first character cannot be converted to a number, parseInt returns NaN.
Upvotes: 0
Reputation: 413682
The parseInt()
function parses an integer from the front of the string you give it. When it encounters something that's not part of a number, it stops and returns the value it's seen so far. Only if the string starts with something that cannot be interpreted as numeric will it return NaN
.
Upvotes: 1