/    Sign up×
Community /Pin to ProfileBookmark

Changes text colour – Form validation

Hi I am reasonably green when it comes to javascript! I am using javascript to validate a form and if the validaion fails I want to change the colour of the text label to the text box. I am using the following function for the validation

<script language=”Javascript” type=”text/javascript”>
function validateCard()
{
if ((document.card_details.card_number.value.length<15) ||
((document.card_details.card_number.value.lenth>19)))
{

document.field1.style.color=”#FF0000″;

}

return false;

} // end function
</script>

field1 is set as an ID for the table column i.e. <td id=”field1″>Card No.</td>

Any help grately appreciated

Mac

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@CharlesApr 19.2005 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta name="Content-Script-Type" content="text/javascript"&gt;
&lt;meta name="Content-Style-Type" content="text/css"&gt;
&lt;title&gt;Example&lt;/title&gt;

&lt;script type="text/javascript"&gt;
&lt;!--
function check (f) {
var correct = true, e, i = 0;
while (e = f.elements[i++]) {if (e.type == 'text' &amp;&amp; !/S/.test (e.value)) {e.parentNode.className = 'inerror'; e.previousSibling.data = '* ' + e.previousSibling.data; correct = false}}
return correct;
}
// --&gt;
&lt;/script&gt;

&lt;style type="text/css"&gt;
&lt;!--
fieldset {padding:1ex; width:10em}
label {display:block; margin:1em 0}
label.inerror {color:#f00}
input {display:block}
button {display:block; margin:auto}

--&gt;
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form action="some-script.pl" onsubmit="return check (this)"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Giant Says&lt;/legend&gt;
&lt;label&gt;Fee&lt;input type="text"&gt;&lt;/label&gt;
&lt;label&gt;Fie&lt;input type="text"&gt;&lt;/label&gt;
&lt;label&gt;Foe&lt;input type="text"&gt;&lt;/label&gt;
&lt;label&gt;Fum&lt;input type="text"&gt;&lt;/label&gt;
&lt;button type="submit"&gt;Submit&lt;/button&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @macgyver 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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