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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox change when it get focus - validation

Status
Not open for further replies.

nomu26

Programmer
Sep 10, 2003
22
ZA
I'm trying to write the javascript to check for empty fields and hightlight the field when it empty the javascript check is working but the style I'm not sure how to do that.
Nomvula
 
Why not use the RequiredFieldValidator control - is this an option? That way you don't have to actually do any of your own JavaScripting? (Remember - it's _all_ about productivity these days ;))

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thanks guys
I've done the validations using javascript and it worked my problem is I need to include the style that will change the background of the textbox if there's an error, here's my script:

<script language=&quot;javascript&quot;>
<!--
function isformvalid()
{
//Load Vars from form
txtFirst_Name = document.frmValidate.txtFirst_Name.value;
//alert(txtFirst_Name);
txtSurname = document.frmValidate.txtSurname.value;
txtEmail = document.frmValidate.txtEmail.value;
txtPostal_Code = document.frmValidate.txtPostal_Code.value;

msgHeader = &quot;The form contains the following errors:\n\n&quot;;
errMsg = &quot;&quot;;
numErrors = 0;



//First Name
if(txtFirst_Name==&quot;&quot;){
errMsg += &quot;Your first name is required,\n&quot;;
document.frmValidate.txtFirst_Name.focus();
document.getElementById(&quot;firstNameError&quot;).className = &quot;show&quot;;

numErrors = numErrors + 1;
} else {
document.getElementById(&quot;firstNameError&quot;).className = &quot;hide&quot;;
}

//Surname
if(txtSurname==&quot;&quot;){
//alert(&quot;here&quot;);
errMsg += &quot;Your Surname is required,\n&quot;;
//document.frmValidate.txtSurname.focus();
document.getElementById(&quot;surnameError&quot;).className = &quot;show&quot;;
numErrors = numErrors + 1;
} else {
document.getElementById(&quot;surnameError&quot;).className = &quot;hide&quot;;
}

//Email
if(txtSurname==&quot;&quot;){
//alert(&quot;here&quot;);
errMsg += &quot;Your Email is required,\n&quot;;
//document.frmValidate.txtEmail.focus();
document.getElementById(&quot;EmailError&quot;).className = &quot;show&quot;;
numErrors = numErrors + 1;
} else {
document.getElementById(&quot;EmailError&quot;).className = &quot;hide&quot;;
}

//Postal Code
if(txtPostal_Code==&quot;&quot;){
errMsg += &quot;Enter Postal Code is required,\n&quot;;
//document.frmValidate.txtPostal_Code.focus();
document.getElementById(&quot;CodeError&quot;).className = &quot;show&quot;;
numErrors = numErrors + 1;
} else {
document.getElementById(&quot;CodeError&quot;).className = &quot;hide&quot;;
}

//Check to see if we had any errors
if(numErrors>0){
alert(msgHeader + errMsg);
return false;
}else{
return true;
}
}
-->
</script>
 
I'm not sure if the <input type=&quot;text&quot;> control or CSS allows for highlighting the background of a textbox...but you can certainly get around this by displaying an image or emphasized error message.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top