|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Form Validation Highlight Field in CSS
Hi Guys,
I've tried several different variations of code, but I'm just not astute enough with JavaScript to figure this out. I have form Validation in javascript that is working great. Now, I'd like to simply do two things: 1. Popup the alert message with the missing fields. (<-- I have this working) 2. Highlight each missing field in red (CSS) (<-- No idea where to begin with this) Validation Script: Code:
<script>
function validateForm() {
with (document.myform) {
var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
if (h.value == "") alertMsg += "\nYour First Name";
if (j.value == "") alertMsg += "\nYour Last Name";
if (SSfirst.value == "") alertMsg += "\nThe first 3 digits of your Social Security";
if (SSsecond.value == "") alertMsg += "\nThe second 2 digits of your Social Security";
if (SSthird.value == "") alertMsg += "\nThe last 4 digits of your Social Security";
if (m.value == "") alertMsg += "\nStreet / P.O. Box";
if (n.value == "") alertMsg += "\nCity";
if (o.value == "") alertMsg += "\nState";
if (p.value == "") alertMsg += "\nZIP";
if (from.value == "") alertMsg += "\nYour e-Mail Address";
if (r.value == "") alertMsg += "\nYour Phone Number";
radioOption = -1;
for (counter=0; counter<s.length; counter++) {
if (s[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nAre you Male or Female?";
if (t.value == "") alertMsg += "\nYour Birth Date";
radioOption = -1;
for (counter=0; counter<v.length; counter++) {
if (v[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nAre you Married, Single or a Single Parent?";
if (w.value == "") alertMsg += "\nNumber in Household.";
if (x.value == "") alertMsg += "\nGross Income.";
if (y.value == "") alertMsg += "\nYour Major.";
for (counter=0; counter<z.length; counter++) {
if (z[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nFirst time student?";
if (f.value == "") alertMsg += "\nYear to enroll.";
for (counter=0; counter<aa.length; counter++) {
if (aa[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nCollege Standing?";
for (counter=0; counter<ac.length; counter++) {
if (ac[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nWill you complete a FAFSA?";
if (ae.value == "") alertMsg += "\nG.P.A.";
for (counter=0; counter<u.length; counter++) {
if (u[counter].checked) radioOption = counter;
}
if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else {
return true;
}
} }
</script>
|
|
#2
|
|||
|
|||
|
k, I'm getting sooooo close... now, I'm just not sure how to control the style property?? Instead of hiding the field, I want to put a border around it or color it somehow...
Any ideas?? Code:
<script>
function validateForm() {
with (document.myform) {
var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
if (h.value == "") alertMsg += "\nYour First Name";
{h.style.visibility='hidden'};
if (j.value == "") alertMsg += "\nYour Last Name";
if (SSfirst.value == "") alertMsg += "\nThe first 3 digits of your Social Security";
if (SSsecond.value == "") alertMsg += "\nThe second 2 digits of your Social Security";
if (SSthird.value == "") alertMsg += "\nThe last 4 digits of your Social Security";
if (m.value == "") alertMsg += "\nStreet / P.O. Box";
if (n.value == "") alertMsg += "\nCity";
if (o.value == "") alertMsg += "\nState";
if (p.value == "") alertMsg += "\nZIP";
if (from.value == "") alertMsg += "\nYour e-Mail Address";
if (r.value == "") alertMsg += "\nYour Phone Number";
radioOption = -1;
for (counter=0; counter<s.length; counter++) {
if (s[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nAre you Male or Female?";
if (t.value == "") alertMsg += "\nYour Birth Date";
radioOption = -1;
for (counter=0; counter<v.length; counter++) {
if (v[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nAre you Married, Single or a Single Parent?";
if (w.value == "") alertMsg += "\nNumber in Household.";
if (x.value == "") alertMsg += "\nGross Income.";
if (y.value == "") alertMsg += "\nYour Major.";
for (counter=0; counter<z.length; counter++) {
if (z[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nFirst time student?";
if (f.value == "") alertMsg += "\nYear to enroll.";
for (counter=0; counter<aa.length; counter++) {
if (aa[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nCollege Standing?";
for (counter=0; counter<ac.length; counter++) {
if (ac[counter].checked) radioOption = counter;
}
if (radioOption == -1) alertMsg += "\nWill you complete a FAFSA?";
if (ae.value == "") alertMsg += "\nG.P.A.";
for (counter=0; counter<u.length; counter++) {
if (u[counter].checked) radioOption = counter;
}
if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else {
return true;
}
} }
</script>
|
|
#3
|
||||
|
||||
|
create proper css class, for example:
.text2{color: red;} and have such javascript: h.class = "text2"; that should change the css class dynamically. |
|
#4
|
|||
|
|||
|
Is there a way to accomplish this with validation controls? Perhaps can a user-defined control inherit the basic required field validator, and add the css work to change the font of the associated text?
|
|
#5
|
||||
|
||||
|
this post is for the .NET forum... anyhow, there is no escape from using custom javascript code. I believe you can put it inside custom Validaor class that you create without any special problem.
|
|
#6
|
|||
|
|||
|
Completed Code
Well,
I got it workin fairly well... Thought I'd post it if anyone else might like to use it: Javascript Field Change Called via: <!-- #include virtual="/includes/validate.js" --> Code:
<!-- ====================== BEGIN Form Validation Replace Class w/ New Color -->
<script language="JavaScript">
function change(id, newClass) {
identity=document.getElementById(id);
identity.className=newClass;
}
</script>
<!-- ====================== END Form Validation Replace Class w/ New Color -->
Style Sheet for the change Called via: <link rel="STYLESHEET" type="text/css" href="/global.css"> Code:
.field {
border: 1px outset #FF0000;
color: #660000;
background-color: #FFCCFF;
font-weight: bold;
}
.field2 {
border: 2px inset #FF0000;
color: #660000;
font-weight: bold;
}
Javascript validation and field change requests: Code:
<script>
function validateForm() {
with (document.myform) {
var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
if (h.value == "")
{alertMsg += "\nYour First Name"
change('changemeh','field') + h.focus()};
if (j.value == "")
{alertMsg += "\nYour Last Name"
change('changemej','field') + j.focus()};
if (SSfirst.value == "")
{alertMsg += "\nThe first 3 digits of your Social Security"
change('changemeSSfirst','field') + SSfirst.focus()};
if (SSsecond.value == "")
{alertMsg += "\nThe second 2 digits of your Social Security"
change('changemeSSsecond','field') + SSsecond.focus()};
if (SSthird.value == "")
{alertMsg += "\nThe last 4 digits of your Social Security"
change('changemeSSthird','field') + SSthird.focus()};
if (m.value == "")
{alertMsg += "\nStreet / P.O. Box"
change('changemem','field') + m.focus()};
if (n.value == "")
{alertMsg += "\nCity"
change('changemen','field') + n.focus()};
if (o.value == "")
{alertMsg += "\nState"
change('changemeo','field') + o.focus()};
if (p.value == "")
{alertMsg += "\nZIP"
change('changemep','field') + p.focus()};
if (from.value == "")
{alertMsg += "\nYour e-Mail Address"
change('changemefrom','field') + from.focus()};
if (r.value == "")
{alertMsg += "\nYour Phone Number"
change('changemer','field') + r.focus()};
if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else
{
return true;
}
} }
</script>
Form: Code:
<form action="page2.asp" name="myform" method="post"> <TABLE width="100%" border="0" cellpadding="3" cellspacing="3"> <TR><TD rowspan="3"><img src="/images/spacer.gif" width="10" height="1" border="0"></TD> </TR> <TR> <TD valign="top"> <!-- Begin BOX --> <P> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td><img src="/images/box_a.gif" width="8" height="8" border="0"></td> <td valign="top"><img src="/images/box_grey_space.gif" width="239" height="1" border="0"></td> <td><img src="/images/box_b.gif" width="8" height="8" border="0"></td> </tr> <tr> <td align="left"><img src="/images/box_grey_space.gif" width="1" height="200" border="0"></td> <td valign="top"> <!-- BOX CONTENT --> <table width="100%" border="0" cellpadding="3" cellspacing="0"> <tr> <td colspan="2"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Full Name</strong><BR><img src="/images/spacer.gif" width="1" height="10" border="0"> </font></td> </tr> <TR> <TD align="right">First:</TD> <TD><input name="h" type="text" size="18" id="changemeh"></TD> </TR> <TR> <TD align="right">Middle:</TD> <TD><input name="i" type="text" size="18"></TD> </TR> <TR> <TD align="right">Last:</TD> <TD><input name="j" type="text" size="18" id="changemej"></TD> </TR> <TR> <TD align="right">Maiden:</TD> <TD><input name="k" type="text" size="18"></TD> </TR> <TR> <TD align="right"><font size="1">Social Security:</font></TD> <TD><input id="changemeSSfirst" type="text" name="SSfirst" size=1 onKeyup="autotab(this, document.myform.SSsecond)" maxlength=3 > - <input type="text" name="SSsecond" size=1 onKeyup="autotab(this, document.myform.SSthird)" maxlength=2 id="changemeSSsecond"> - <input type="text" name="SSthird" size=1 maxlength=4 id="changemeSSthird"></TD> </TR> <TR> <TD><img src="/images/spacer.gif" width="80" height="1" border="0"></TD> <TD><img src="/images/spacer.gif" width="145" height="1" border="0"></TD> </TR> </table> <!-- End BOX CONTENT --> </td> <td align="right"><img src="/images/box_grey_space.gif" width="1" height="200" border="0"></td> </tr> <tr> <td><img src="/images/box_c.gif" width="8" height="8" border="0"></td> <td valign="bottom"><img src="/images/box_grey_space.gif" width="239" height="1" border="0"></td> <td><img src="/images/box_d.gif" width="8" height="8" border="0"></td> </tr> </table> <!-- END BOX --> </TD> <TD align="right" valign="top"> <!-- Begin BOX --> <P> <table width="400" border="0" cellspacing="0" cellpadding="0"> <tr> <td><img src="/images/box_a.gif" width="8" height="8" border="0"></td> <td valign="top"><img src="/images/box_grey_space.gif" width="400" height="1" border="0"></td> <td><img src="/images/box_b.gif" width="8" height="8" border="0"></td> </tr> <tr> <td align="left"><img src="/images/box_grey_space.gif" width="1" height="200" border="0"></td> <td valign="top"> <!-- BOX CONTENT --> <table width="100%" border="0" cellpadding="3" cellspacing="0"> <tr> <td colspan="2"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Contact Information</strong><BR><img src="/images/spacer.gif" width="1" height="10" border="0"> </font></td> </tr> <TR> <TD align="right">Street / P.O. Box:</TD> <TD><input name="m" type="text" size="37" id="changemem"></TD> </TR> <TR> <TD align="right">City:</TD> <TD><input name="n" type="text" size="10" id="changemen"> ST: <input name="o" type="text" size="2" maxlength="2" id="changemeo"> ZIP: <input name="p" type="text" size="3" maxlength="5" id="changemep"></TD> </TR> <TR> <TD align="right">Your e-Mail Address:</TD> <TD><input name="from" type="text" size="37" id="changemefrom"></TD> </TR> <TR> <TD align="right">Your Phone Number:</TD> <TD><input name="r" type="text" size="37" id="changemer"></TD> </TR> <tr> <td align="right" class="form_normal">Country:</td> <td class="form_normal"><input name="country" type="text" value="United States" size="37"></td> </tr> <TR> <TD><img src="/images/spacer.gif" width="80" height="1" border="0"></TD> <TD><img src="/images/spacer.gif" width="135" height="1" border="0"></TD> </TR> </table> <!-- End BOX CONTENT --> </td> <td align="right"><img src="/images/box_grey_space.gif" width="1" height="200" border="0"></td> </tr> <tr> <td><img src="/images/box_c.gif" width="8" height="8" border="0"></td> <td valign="bottom"><img src="/images/box_grey_space.gif" width="400" height="1" border="0"></td> <td><img src="/images/box_d.gif" width="8" height="8" border="0"></td> </tr> </table> <!-- END BOX --> </TD> </TR> </TABLE> |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Form Validation Highlight Field in CSS |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|