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

Validation Problem

Status
Not open for further replies.

LewisReeder

IS-IT--Management
Jul 18, 2005
38
US
I am fairly new to javascript so I am sure the answer is simply, but when I run this function it only runs through the first IF statement and stops. I have tested this with an alert() window. When the user clicks the submit button I want to validate that each field was completed. Could someone please tell me what I am doing wrong?


function submitClick(strSubmit)
{
var boolError = false, strError = "";
var frm = document.frmTSC, boolAccount, boolJustification;


if(frm.tbxSatFrom.value == "")
{ boolError = true;
strError += "\nMissing...";
}

if(frm.tbxSunFrom.value == "")
{ boolError = true;
strError += "\nMissing...";
}


// Set the submit flag
frm.tbxSubmit.value = strSubmit;
if (boolError) {
alert("Data Validation Errors\n" + strError);

}
else {
// Submit the form
frm.submit();
}

}

//(button click)
input type="submit" value="Submit" onClick="javascript:submitClick('N');">


Thanks,
Lewis
 
You are declaring your input as type="submit". This by default submits the form. Additionally, you are putting an onclick handler on the input that submits the form in a javascript function, this means that even if your function alerts an error the form will still submit.

Change your input to:
Code:
<input type="[COLOR=red][b]button[/b][/color]" value="Submit" onClick="javascript:submitClick('N');">

Or you can handle all the validation thru the onsubmit handler on your form. The above solution should work fine though.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
<html>
<head><titel>Test</title>

<script>

function submitClick()
{
var boolError = false;
var strError = "";
var frm = document.frmTSC;
var boolAccount = false;
var boolJustification = false;

if(frm.tbxSatFrom.value == "")
{

boolError = true;
strError += "\nMissing...tbxSatFrom values";
}

if(frm.tbxSunFrom.value == "")
{
boolError = true;
strError += "\nMissing...tbxSunFrom values";
}


if (boolError)
{
alert("Data Validation Errors\n" + strError);
return false;
}
else
{

//frm.submit();
}
return true;

}
</script>
</head>

<form name="frmTSC">


<input type="text" value="" name="tbxSatFrom"><br>
<input type="text" value="" name="tbxSunFrom"><br><br>
<input type="submit" value="Submit" onClick="return submitClick();">

</form>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top