...

View Full Version : form validation-check for empty fields



dennylim
08-06-2002, 09:28 AM
hi..i have already written a code but it is not working, the form is not submitted when clicked. pardon me, i'm very new to javascript. my codes are:


<!--BEGIN FORM VALIDATION SCRIPT-->
<script language="JavaScript1.2">

var error;
var error_ad;
var error_co;
var error_pr;
var error_ch;
var error_ti;
var error_en;
var error_cm;
var errormsg;

//--------------------------Customise-------------------------------
var isADVERTISEMENT_ID=true; // True if ADVERTISEMENT_ID field required else False
var isCOMPANY_ID=true; // True if COMAPNY_ID field required else False
var isPRODUCT_ID=false; // True if PRODUCT_ID required else False
var isCHANNEL=true; // True if CHANNEL required else False
var isTIMESLOT=true; // True if TIMESLOT required else False
var isENTRY=true; // True if ENTRY required else False
var isCOMMENT=true; // True if COMMENT required else False
//------------------------------------------------------------------

function reset_error()
{
var error_ad=false;
var error_co=false;
var error_pr=false;
var error_ch=false;
var error_ti=false;
var error_en=false;
var error_cm=false;
errormsg='Following Errors Occured ::\n_____________________________\n\n';
}

function validate_ADVERTISEMENT_ID()
{
if(isADVERTISEMENT_ID)
{
if(form.ADVERTIESMENT_ID.value=="")
{
errormsg+='Please enter your Advertisement_id.\n';
error_ad=true;
form.ADVERTISEMENT_ID.focus();
}
}


}


function validate_COMPANY_ID()
{
if(isCOMPANY_ID)
{
if(form.COMPANY_ID.value=="")
{
errormsg+='Please enter your Company_id.\n';
error_co=true;
if(!error_ad)
{
form.COMPANY_ID.focus();
}
}
}
}

function validate_PRODUCT_ID()
{
if(isPRODUCT_ID)
{
if(form.PRODUCT_ID.value=="")
{
errormsg+='Please enter your Product_id.\n';
error_pr=true;
if((!error_ad) && (!error_co))
{
form.PRODUCT_ID.focus();
}
}
}
}

function validate_CHANNEL_ID()
{
if(isCHANNEL_ID)
{
if(form.CHANNEL_ID.value=="")
{
errormsg+='Please enter Channel_id.\n';
error_ch=true;
if((!error_ad)&&(!error_co)&&(!error_pr))
{
form.CHANNEL_ID.focus();
}
}
}
}

function validate_TIMESLOT()
{
if(isTIMESLOT)
{
if(form.TIMESLOT.value=="")
{
errormsg+='Please enter Timeslot.\n';
error_ti=true;
if((!error_ad)&&(!error_co)&&(!error_pr)&&(!error_ch))
{
form.TIMESLOT.focus();
}
}
}
}

function validate_ENTRY_ID()
{
if(isENTRY_ID)
{
if(form.ENTRY_ID.value=="")
{
errormsg+='Please enter Entry_id.\n';
error_en=true;
if((!error_ad)&&(!error_co)&&(!error_pr)&&(!error_ch)&&(!error_ti))
{
form.ENTRY_ID.focus();
}
}
}
}

function validate_COMMENT()
{
if(isCOMMENT)
{
if(form.COMMENT.value=="")
{
errormsg+='Please enter Comment_id.\n';
error_cm=true;
if((!error_ad)&&(!error_co)&&(!error_pr)&&(!error_ch)&&(!error_ti)&&(!error_en))
{
form.COMMENT.focus();
}
}
}
}

function validate()
{
reset_error();
validate_ADVERTIESMENT_ID();
validate_COMPANY_ID();
validate_PRODUCT_ID();
validate_CHANNEL();
validate_TIMESLOT();
validate_ENTRY_ID();
validate_COMMENT();
if(error_ad||error_co||error_pr||error_ch||error_ti||error_en||error_cm)
{
error=true;
}
else
{
error=false;
}
if(!error)
{
form.submit();
}
else
{
alert(errormsg);
}
}

</script>
<!--END FORM VALIDATION SCRIPT-->
<!--------------------------------------END REQUIRED------------------------------------------->

beetle
08-06-2002, 02:48 PM
I have written a validator that may better suit your needs. Check it out.

www.peterbailey.net/jsdemo/

adios
08-06-2002, 08:29 PM
You've referenced something called form in a number of places - including form.submit(); is this a valid global reference to the Form object (document.forms[0]) ? By itself, it's just a name.

beetle
08-06-2002, 08:35 PM
Good question adios. Using 'form' is not valid. Like you mention, it should be document.forms[0] or some other valid DOM reference.

The only way that *could* work is if 'form' was an object variable holding the form in question itself. However, even that would not be valid, because 'form' is a reserved word and cannot be used in the variable namespace. It would have to be 'frm' or 'theForm' or something like that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum