Javascript returning that a field is null when it is not

M

Marshall Dudley

I have some javascript code which was supplied by a credit card
processing company to check their card numbers for validity and to check
the cvv. It fails on a cvv of 000, which is valid, and complains that
the cvv was left out, when it was not.

Here is the code:

/*This function checks if a field is empty.*/

function emptyField(textObj) {

if (textObj.length == 0) return true;
for (var i=0; i<textObj.length; ++i) {
var ch = textObj.charAt(i);
if (ch != ' ' && ch != '\t') return false;
}
return true;
}

if (emptyField(document.CC.x_Card_Code.value)) {
alert("Error: Your credit card security code is
required. Please correct this error and try again.");
document.CC.x_Card_Code.focus();
return false;
}


I don't know javascript very well, but don't see why it says that it is
blank with the number of 000 entered, when I walk through the code, I
get that emptyField should return a false. Can anyone tell me what
needs changing to make it work, or why it thinks the length is 0, when
it is 3, or thinks a zero is the same as a null or tab?

Thanks,

Marshall
 
D

David Mark

I have some javascript code which was supplied by a credit card
processing company to check their card numbers for validity and to check
the cvv. It fails on a cvv of 000, which is valid, and complains that
the cvv was left out, when it was not.

Here is the code:

/*This function checks if a field is empty.*/

function emptyField(textObj) {

if (textObj.length == 0) return true;
for (var i=0; i<textObj.length; ++i) {
var ch = textObj.charAt(i);
if (ch != ' ' && ch != '\t') return false;
}
return true;

}

if (emptyField(document.CC.x_Card_Code.value)) {
alert("Error: Your credit card security code is
required. Please correct this error and try again.");
document.CC.x_Card_Code.focus();
return false;
}

I don't know javascript very well, but don't see why it says that it is
blank with the number of 000 entered, when I walk through the code, I
get that emptyField should return a false. Can anyone tell me what
needs changing to make it work, or why it thinks the length is 0, when
it is 3, or thinks a zero is the same as a null or tab?

Thanks,

Marshall

You are screwing up the testing as this works:

<html>
<head>
<script type="text/JavaScript">
function emptyField(textObj) {
if (textObj.length == 0) return true;
for (var i=0; i<textObj.length; i++) {
var ch = textObj.charAt(i);
if (ch != ' ' && ch != '\t') return false
}
return true;
}
function validate() {
if
(emptyField(document.forms['CC'].elements['x_Card_Code'].value)) {
alert("Error: Your credit card security code is
required. Please correct this error and try again.");
document.forms['CC'].elements['x_Card_Code'].focus();
return false
}
}
</script>
</head>
<body>
<form name="CC" onsubmit="return validate()"><input name="x_Card_Code"
type="text"></form>
</body>
</html>

And it worked before I changed anything. BTW, your credit card
processing company should not be handing out JS code. This is one of
the most brain-dead validation scripts I have ever seen. Look into
regular expressions.
 
M

Marshall Dudley

David said:
I have some javascript code which was supplied by a credit card
processing company to check their card numbers for validity and to check
the cvv. It fails on a cvv of 000, which is valid, and complains that
the cvv was left out, when it was not.

Here is the code:

/*This function checks if a field is empty.*/

function emptyField(textObj) {

if (textObj.length == 0) return true;
for (var i=0; i<textObj.length; ++i) {
var ch = textObj.charAt(i);
if (ch != ' ' && ch != '\t') return false;
}
return true;

}

if (emptyField(document.CC.x_Card_Code.value)) {
alert("Error: Your credit card security code is
required. Please correct this error and try again.");
document.CC.x_Card_Code.focus();
return false;
}

I don't know javascript very well, but don't see why it says that it is
blank with the number of 000 entered, when I walk through the code, I
get that emptyField should return a false. Can anyone tell me what
needs changing to make it work, or why it thinks the length is 0, when
it is 3, or thinks a zero is the same as a null or tab?

Thanks,

Marshall

You are screwing up the testing as this works:

<html>
<head>
<script type="text/JavaScript">
function emptyField(textObj) {
if (textObj.length == 0) return true;
for (var i=0; i<textObj.length; i++) {
var ch = textObj.charAt(i);
if (ch != ' ' && ch != '\t') return false
}
return true;
}
function validate() {
if
(emptyField(document.forms['CC'].elements['x_Card_Code'].value)) {
alert("Error: Your credit card security code is
required. Please correct this error and try again.");
document.forms['CC'].elements['x_Card_Code'].focus();
return false
}
}
</script>
</head>
<body>
<form name="CC" onsubmit="return validate()"><input name="x_Card_Code"
type="text"></form>
</body>
</html>

And it worked before I changed anything. BTW, your credit card
processing company should not be handing out JS code. This is one of
the most brain-dead validation scripts I have ever seen. Look into
regular expressions.
Opps, you are right, it is this code that is erroring out:

if (document.CC.x_Card_Code.value.length < 3) {
alert("Error: The credit card security code entered does
not have sufficient number of digits. Please correct this
document.CC.x_Card_Code.focus();
return false;
}

I definitely entered 3 zeros, but javascipt is reporting that I entered
2 or less. Even when I enter 4 zeros, the javascript reports that less
than 3 were entered. I did not think it was this part since it seemed
bulletproof (a 1st grader can count to 3) number of characters seems
obvious. However if I enter 001, it counts that as 3 characters. Why
does javascript get confused if the characters are all the same?

Marshall
 
D

David Mark

You are screwing up the testing as this works:
<html>
<head>
<script type="text/JavaScript">
function emptyField(textObj) {
if (textObj.length == 0) return true;
for (var i=0; i<textObj.length; i++) {
var ch = textObj.charAt(i);
if (ch != ' ' && ch != '\t') return false
}
return true;
}
function validate() {
if
(emptyField(document.forms['CC'].elements['x_Card_Code'].value)) {
alert("Error: Your credit card security code is
required. Please correct this error and try again.");
document.forms['CC'].elements['x_Card_Code'].focus();
return false
}
}
</script>
</head>
<body>
<form name="CC" onsubmit="return validate()"><input name="x_Card_Code"
type="text"></form>
</body>
</html>
And it worked before I changed anything. BTW, your credit card
processing company should not be handing out JS code. This is one of
the most brain-dead validation scripts I have ever seen. Look into
regular expressions.

Opps, you are right, it is this code that is erroring out:

if (document.CC.x_Card_Code.value.length < 3) {
alert("Error: The credit card security code entered does
not have sufficient number of digits. Please correct this
document.CC.x_Card_Code.focus();
return false;
}

I definitely entered 3 zeros, but javascipt is reporting that I entered
2 or less. Even when I enter 4 zeros, the javascript reports that less
than 3 were entered. I did not think it was this part since it seemed
bulletproof (a 1st grader can count to 3) number of characters seems
obvious. However if I enter 001, it counts that as 3 characters. Why
does javascript get confused if the characters are all the same?

Marshall- Hide quoted text -

- Show quoted text -

I didn't detect any confusion when I ran it. Post your source in its
entirety.
 
O

One Dumm Hikk

if (document.CC.x_Card_Code.value.length < 3) {

I definitely entered 3 zeros, but javascipt is reporting that I entered
2 or less. Even when I enter 4 zeros, the javascript reports that less
than 3 were entered. I did not think it was this part since it seemed
bulletproof (a 1st grader can count to 3) number of characters seems
obvious. However if I enter 001, it counts that as 3 characters. Why
does javascript get confused if the characters are all the same?

3 digits the same isn't what is confusing it, it isn't even confused
at all. When you type in 000 and JS gets the value property, its value
is 0 and the length of that value is 1 so it properly fails your test.
Try testing it with 111, 222, 333 and so on.
 
D

David Mark

3 digits the same isn't what is confusing it, it isn't even confused
at all. When you type in 000 and JS gets the value property, its value
is 0 and the length of that value is 1 so it properly fails your test.

I don't see that at all. How would the string '000' be coerced to a
number? Adding his additional logic and fixing the error
(unterminated string constant), makes it work just fine:

<html>
<head>
<script type="text/JavaScript">
function emptyField(textObj) {
if (textObj.length == 0) return true;
for (var i=0; i<textObj.length; i++) {
var ch = textObj.charAt(i);
if (ch != ' ' && ch != '\t') return false
}
return true;
}


function validate() {
if
(emptyField(document.forms['CC'].elements['x_Card_Code'].value)) {
alert("Error: Your credit card security code is
required. Please correct this error and try again.");
document.forms['CC'].elements['x_Card_Code'].focus();
return false
}

if (document.forms['CC'].elements['x_Card_Code'].value.length < 3) {
alert("Error: The credit card security code entered
does not have sufficient number of digits. Please correct this.");
document.CC.x_Card_Code.focus();
return false;
}
}
</script>
</head>
<body>
<form name="CC" onsubmit="return validate()"><input
name="x_Card_Code"
type="text"></form>
</body>
</html>
 
O

One Dumm Hikk

I don't see that at all.

And you are seeing right. It is what happens when I post on vacation
and while allowing spirits to influence my thought processes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top