Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlist Required Fields 1

Status
Not open for further replies.

hedidit

Technical User
Joined
Jul 21, 2005
Messages
142
Location
GB
Hi all,

I'm using the following script to ensure required fields are provided before form submission, the script was, i think, provided by someone on here sorry can't remember who...

Anyway as well as showing the user an alert i'd like to highlight the missing required fields in red... i'm rubbish at javascript, can anyone show me how to do this? Guess it's only one line of code?

Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,3)=="req") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
         }
      }
   }
}
if (!pass) {
shortFieldName=tempobj.name.substring(3,30).toUpperCase();
alert("Please make sure the "+shortFieldName+" field was properly completed.");


return false;
}
else
return true;
}
//  End -->
</script>

Cheers
 
After this line:

Code:
var tempobj=which.elements[i];

add this line:

Code:
tempobj.style.border = '1px solid black";

(or whatever default styling you want on form fields)

and then after this line:

Code:
if (!pass) {

add this line:

Code:
tempobj.style.border = '1px solid red";

(or whatever styling you want on the error field)

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
nice one cheers for the help...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top