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

onClick JScript function 1

Status
Not open for further replies.

sondev

IS-IT--Management
Jan 7, 2005
22
I need a small script that will verify that three fields are not empty. If one or more of the three fields is empty then "onClick" should recieve an error and a message preventing the button action from submitting. If all the fields are filled in then "onClick" should process the action. My script looks like this so far:


function Validate()

{
var InvoiceNum = Invoice
var OrderNum = Order
var InvoiceDate = DateBilled
var message="Your command cannot be completed. One of the required fields is not completed. Please fill in the correct information and then proceed.";
if (InvoiceNum = "");(OrderNum = "");(InvoiceDate = "");
alert(message);
else ???
}


Since I am a beginner with Jscript, I am not sure how the syntax should be completed. I have all the feilds defined properly. I just need the script to evaluate each to see if its empty and then display the error message or submit the action.
 
Use a submit button and check in the onsubmit handler of your form instead of your onclick handler of the button. Here's an example:
Code:
<script language=javascript>
function validateFunction() {
   str1 = document.forms["blahForm"].elements["text1"].value;
   str2 = document.forms["blahForm"].elements["text2"].value;
   str3 = document.forms["blahForm"].elements["text3"].value;
   if (str1 == '' || str2 == '' || str3 == '') {
      alert("you must fill out all 3 fields");
      return false;
   }
   return true;
}
</script>
<body>
<form name=blahForm onsubmit='return validateFunction()'>
<input type=text name=txt1><br>
<input type=text name=txt2><br>
<input type=text name=txt3><br>
<input type=submit value='submit form'>
</form>
</body>

-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
 
Kaht thanks alot. That was very helpful. Unfortunately I had to complicate the jscript to define exactly what elements are missing. so now i have this script. It does not seem to return false. It returns true everytime.... what could be wrong?

function Validate()
{
var Field1=example1
var Field2=example2
var Field3=example3


if (Field1 == '' && Field2 == '' && Field3 == '') {
alert("Your command cannot be completed. None of the required fields are completed. Please fill in the correct information for Field2, Field1, and Field3.");
return false;

}
if (Field1 != '' && Field2 == '' && Field3 == '') {
alert("Your command cannot be completed. Certain required fields are not completed. Please fill in the correct information for Field1, and Field3.");
return false;

}
if (Field1 != '' && Field2 != '' && Field3 == '') {
alert("Your command cannot be completed. Certain required fields are not completed. Please fill in the correct information for Field3.");
return false;

}
if (Field1 == '' && Field2 != '' && Field3 == '') {
alert("Your command cannot be completed. Certain required fields are not completed. Please fill in the correct information for Field2, and Field3.");
return false;

}
if (Field1 == '' && Field2 != '' && Field3 != '') {
alert("Your command cannot be completed. Certain required fields are not completed. Please fill in the correct information for Field2.");
return false;

}
if (Field1 =='' && Field2 == '' && Field3 != '') {
alert("Your command cannot be completed. Certain required fields are not completed. Please fill in the correct information for Field2, and Field1.");
return false;

}
if (Field1 != '' && Field2 == '' && Field3 != '') {
alert("Your command cannot be completed. Certain required fields are not completed. Please fill in the correct information for Field1.");
return false;

}
if (Field1 != '' && Field2 != '' && Field3 != '') {
return true;

}
}
 
where is example1, 2, and 3 defined?

this portion of the code:

[tt] var Field1=example1
var Field2=example2
var Field3=example3[/tt]

seems incomplete, if you're pulling values from text fields then you need to define them similar to what I posted above:

[tt] str1 = document.forms["blahForm"].elements["text1"].value;
str2 = document.forms["blahForm"].elements["text2"].value;
str3 = document.forms["blahForm"].elements["text3"].value;[/tt]

-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
 
The examples are defined to the application I am developing in. As far as assigning variables are concerned, everything is complete. I am even getting the correct error response for each alert when I click on the submit button.

The only problem is the "if statement" does not return false; it returns true everytime. If nothing is wrong with my Jscript code, then it is a problem I am having with my application. Please do tell me if you see something wrong with my code. Thanks.
 
I don't see anything "wrong" with the code, although I think you could break it back down into far less if statements. However, that's irrelevant to your question. I'd suggest using alert statements to see what's inside the Field1-3 variables. I'm guessing that the info isn't being pulled correctly because there's nothing wrong with the syntax of the code you posted above.

alert is your best friend when debugging.

-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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top