/    Sign up×
Community /Pin to ProfileBookmark

Why this isn’t working

Hi There,

BTW, you wouldn’t happen to know why the following part of the code isn’t validating. It is not picking up that only letters are allowed in the card name, and is also letting the form be processed with no entry fields for expiry date.

JAVASCRIPT:

for(i=0;i<f.cardname.value.length;i++)
{
if(f.cardname.value.charAt(i).match(“[0-9]”))
{
alert(“Only letters are allowed in your credit card name.”);
return false;
}
}

if (document.detailsform.cardexpfirst.value == “”)
{
alert(“Please enter the expiry date of your credit card.”);
return false;
}

for(i=0;i<f.cardexpfirst.value.length;i++)
{
if(f.cardexpfirst.charAt(i).match(“[a-z|A-Z]”))
{
alert(“Only numerical digits are allowed as an expiry date.”);
return false;
}
}

if (document.detailsform.cardexpsecond.value == “”)
{
alert(“Please enter the expiry date of your credit card.”);
return false;
}

for(i=0;i<f.cardexpsecond.value.length;i++)
{
if(f.cardexpsecond.value.charAt(i).match(“[a-z|A-Z]”))
{
alert(“Only numerical digits are allowed as an expiry date.”);
return false;
}
}

}

</script>

FORM:

<tr>
<td width=8>&nbsp;</td>
<td align=right width=300> <font color=#00549a size=2 face=arial>
<b>Name on Card:</b></font>&nbsp; </td>
<td width=”300″><font color=black size=2 face=arial>
<input type=”text” name=”cardname” size=”30″ >
</font></td>
<td width=”12″>&nbsp;</td>
</tr>
<tr>
<td width=8>&nbsp;</td>
<td align=right width=”300″><font color=#00549a size=2 face=arial>
<b>Expiry Date:</b>&nbsp;</font></td>
<td width=”300″><font color=#00549a size=2 face=arial>
<input type=”text” name=”cardexpfirst” size=”1″

maxlength=”2″>/<input type=”text” name=”cardexpsecond” size=”1″ maxlength=”2″>
<br>
month/year</font></td>
<td width=”12″>&nbsp;</td>
</tr>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 23.2006 — use full regExp to validate. And for number/alpha validate, maybe is better to use onkeyup:

ex - number validadte:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/JavaScript"&gt;
function valid(f) {
if (!/^d*$/.test(f.value)) {
alert("Only integer numbers allowed!");
f.value = f.value.replace(/[^d]/g,"");
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="text" onkeyup="valid(this)"&gt;
&lt;/body&gt;
&lt;/html&gt;

for alpha
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/JavaScript"&gt;
function valid(f) {
var re = /^[A-z]*$/;
if (!re.test(f.value)) {
alert("Only alpha numeric characters allowed!");
f.value = f.value.replace(/[^A-z]/g,"");
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="text" onkeyup="valid(this)"&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @qhen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...