Solved

Javascript validation problem

Posted on 2007-11-19
45
233 Views
Last Modified: 2010-05-18
I was given a function to use 'onsubmit' however, I already am calling a validation form upon submit...so I tried to add the VERY FIRST validation to my validation script, but it isn't working right...Can you all see why?
It works by itself if I call IT on submit, but I need it WITHIN my validation script ??

function validate(theForm){
      
***********this is the part that I stuck in here************************
      var rs = document.getElementsByName('ownstatus');
      var chk = false;
      for(i=0; i<rs.length; i++){
            if(rs[i].checked) return true;
      }
     
      alert('You must select one of the choices!');
      return false;
}
*********************end of code I stuck in here.......
       if (theForm.Problem.value == ""){
    theForm.Problem.focus();
    alert("Please key in the 'Problem' field!");
    return false;
  }

  else if (theForm.closename.value != "" && theForm.Resolution.value == ""){
    theForm.Resolution.focus();
    alert("Please key in the 'Resolution' field!");
    return false;
  }
 
 
 else if (theForm.closename.value != '<%=CSR%>' && theForm.closename.value != ""){
   var blnConfirm1 = confirm('You did not open this call, are you sure you want to close this log?');
  return (blnConfirm1) ? true : false;
  this.form.closename.value = ''
 
}

   if (theForm.closename.value != "" ){
   var blnConfirm = confirm('Are you sure you want to close this log?');
  return (blnConfirm) ? true : false;
  this.form.closename.value = ''
 
} //close brace here
   
      else
            var submitDone;
            submitDone = 0;
            theForm.LogCallButton.disabled = true;
      theForm.LogCallButton.value = 'Logging..';
      if (submitDone == 0){
            submitDone = 1;
            return true;
      }
      

      else{
            if (!theForm.LogCallButton){
                  alert("Logging Call... Please wait...");
            }
      return false;
      }
}
0
Comment
Question by:tsmithinnc
  • 19
  • 10
  • 10
  • +3
45 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20311606
var rs = document.getElementsByName('ownstatus'); ?
i know there is getElementsByTagName and getElementByID..
is this IE/FFox/Safari/Konqueror?
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20311608
Add the second event to the element:

<button id="test" onclick="FIRST_EVENT() SECOND EVENT();">Click Me</button>

Or, put a call ro the second function *after* the first function.

But you can only return "TRUE" or "FALSE" from one, not both, and the result should be the same anyhow...
0
 

Author Comment

by:tsmithinnc
ID: 20311683
It is for IE and firefox...
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:tsmithinnc
ID: 20311689
     

This isn't working?!?!
	<form method="POST" onsubmit="return validate(this); frmChk()" action="LogUpdate.asp" name="CallLog"  >

Open in new window

0
 
LVL 17

Expert Comment

by:gops1
ID: 20311706
Try
<form method="POST" onsubmit="if(validate(this)) frmChk()" action="LogUpdate.asp" name="CallLog"  >
 

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 20311715
Yeah, the return false thing and multiple events - I'm not sure about that working or not. Might have to be a mega-event, eh?
<form method="POST" onsubmit="return validate(this) frmChk()" action="LogUpdate.asp" name="CallLog"  >

Open in new window

0
 

Author Comment

by:tsmithinnc
ID: 20311869
: (
so it isn't possible :(
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20311937
Of course it is possibe - anything is possible - you were on the right track when you combined the two functions.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20311945
Oh, and this is why your validation fails:


     var rs = document.getElementsByName('ownstatus');
      var chk = false;
      for(i=0; i<rs.length; i++){
            if(rs[i].checked) return true;
      }
 

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 20311952
If the first 'ownstatus' is checked, you return true. No further processing occurs.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20312026
1. Gops is correct
onsubmit="return validate(this) frmChk()"
will not work since anything after a return something will never be executed
so
onsubmit="if (validate(this)) frmChk()"
is how to run both from the onsubmit

2. Badotz is correct. the validate returns as soon as one checkbox is checked.

3. why mix DOM and Forms access???

And why have I seen this so many times before now? I thought we'd fixed your forms and validations???
0
 

Author Comment

by:tsmithinnc
ID: 20315783
this doesn't work ...because it doesnt go through the validation process...it comes and tells me I have to have a resolution...GOOD GOD ALMIGHTY DONT I KNOW IT...lol...anyway...I say OK and it processes the form anyway?!?!
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20316420
See my comment #20311952 for part of your problem: wromg thimkimg.
0
 

Author Comment

by:tsmithinnc
ID: 20316818
Then how do I get around that?
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20316849
Set a flag instead of returning from the test. Then test the flag as part of the validation process. If false (or true - you will know which), no need to continue with any other tests. Otherwise continue validating.
0
 
LVL 17

Expert Comment

by:gops1
ID: 20317354
Like this:

      var chk = false;
      for(i=0; i<rs.length; i++){
            if(rs[i].checked){ chk = true;break;}
      }
      if(!chk){
          alert('You must select one of the choices!');
          return false;
      }

Open in new window

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 20319121
as your validate() always returns false the action is never called
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20319248
How so?

    if (submitDone == 0){
            submitDone = 1;
            return true;
      }
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20319254
You still mess up the bottom

      else
            var submitDone;
            submitDone = 0;
            theForm.LogCallButton.disabled = true;
      theForm.LogCallButton.value = 'Logging..';

I have fixed this so many times now :((

Missing braces and messy formatting...
0
 

Author Comment

by:tsmithinnc
ID: 20319305
Tough to be a hero ehh mplungjian ?   I'm TRYING.... HHHHHUGGGGGGGGGS
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20319353
No, just a little frustrating to help and see what I spent time doing ignored or disappear...
Especially since I normally get paid for this kind of thing (custom javascript instead of just answering questions)
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20319777
@mplungjan: I so agree. Sure wish there was a place where people could go to validate their code. Oh wait - there is! EE!!

At least that is what it feels like  :-/ But there is no recourse, save not participating, eh?

Syntax errors are so yesterday...
0
 

Author Comment

by:tsmithinnc
ID: 20319949
GUYS GUYS GUYS  :)  I applaud you for your efforts...and apologize for your frustration...
I DO appreciate you...I didn't start when you all did with this stuff...given time I will get there...
and I can't help the changes...
Your works are NOT unappreciated...but having said that...I didn't force you to be here either...nor did the others who are not "at your level" with this type thing...
HEHE...I could run circles around you on my bike or at the firestation I am sure...I help others because I CAN...and I appreciate YOU guys for doing the same...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20320111
All I ask is when I post a suggestion or format code and fix it for you that I do not see the old code again with the same formatting problems as 2 weeks ago...

I am talking about what I see in this question looks like it did before I touched it here:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22934232.html#a20198870
0
 

Author Comment

by:tsmithinnc
ID: 20320358
You are appreciated...
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20320404
So are we done here? I'm thinking this horse is dead, eh?
0
 

Author Comment

by:tsmithinnc
ID: 20320435
I am trying to put it into place now...It is HARD to take it and not only put it where it goes but try and learn WHAT is happening too...like I said I am trying...I will let you know...and I TRULY from the BOTTOM of my heart in as LEAST a girly girl way as I can POSSIBLY do THANK YOU...
...REALLY...
0
 

Author Comment

by:tsmithinnc
ID: 20323329
:) Like I said...I would concur...and I LOVVVVVVVVVVVE to read...felt like you were attacking me...
and yeah...I'm not one to lie down...
But...i DO appreciate your help...
I bought an online course just to help me understand the code YA'LL gave me ...OMG...I think there are little javascripts coming out of my EARS at this point :(
I just don't see it...
Will keep trying...and like I said...
i DOOOO appreciate the selfless efforts here on the board...which is the only reason I am paying for the subscription...
YA'LL ROCK!
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20323490
An on-line course is a good idea.

May I make a suggestion? Go to amazon.com and look for a book titled, "Javascript", by Kelly L. Murdock, published by IDG Books. It is from the "Read Less, Learn More" series, and assumes you know nothing (I would *never* make that assumption ;-) The examples are not what one would call "real world", but they are clear.

If you are feeling adventurous, look for "The Javascript Bible, 5th Edition", by Danny Goodman et. al., published by Wiley. See if you can find it used - you might save a bundle.

I applaud your determination and wish you the very best. And be sure to take advantage of EE when things really get rough.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20327672
@tsmithinnc: Apologies for how this thread turned out

Do you need more help implementing the new validation?
0
 

Author Comment

by:tsmithinnc
ID: 20327681
Yes Sir, I want to put it together as best I can and then put it here if there are problems...I would LIKE to think I can piece it together...but am a little leary :(

THANK YOU!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20327711
So do you have the REAL validation?
The one you posted has problem here:


} //close brace here
   
      else             <<<<<<<<<< Where is the brace from here?
            var submitDone;
            submitDone = 0;
            theForm.LogCallButton.disabled = true;
      theForm.LogCallButton.value = 'Logging..';
      if (submitDone == 0){
            submitDone = 1;
            return true;
      }
     
Can we use the formatted validation I did earlier here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22934232.html#a20198870

plus the suggestion by gops: http:#20317354


0
 

Author Comment

by:tsmithinnc
ID: 20327721
Meeting at 9:15 , then I will put this in place...
I HOPE :)
THANK YOU!!!!
0
 

Author Comment

by:tsmithinnc
ID: 20328999
OK...I wiped EVERYTHING AWAY...and THIS is what I am left with...
but when I press submit it DOESNT check ANYTHING...

And I STILL need to have my dropdown REQUIRE a radio button selection and I don't want my radio buttons to even SHOW until the drop down is selected...
?!?!?



 // Validation script -------------------------------------------------
function validate(theForm){
	if (theForm.Category.selectedIndex == 0){
  	theForm.Category.focus();
  	alert("Please select a Category!");
    return false;
 }
  else if (theForm.CatSubj.selectedIndex == 0){
  	theForm.CatSubj.focus();
   	alert("Please Select a Subject!");
   	return false;
  }
	else if (theForm.Problem.value == ""){
    theForm.Problem.focus();
    alert("Please key in the 'Problem' field!");
    return false;
  }
  else if (theForm.Problem.value == ""){
    theForm.Problem.focus();
    alert("Please key in the 'Problem' field!");
    return false;
  }
  else if (theForm.closename.value != "" && theForm.Resolution.value == ""){
    theForm.Resolution.focus();
    alert("Please key in the 'Resolution' field!");
    return false;
  }
 if (theForm.closename.value != "" ){
   var blnConfirm = confirm('Are you sure you want to close this log?');
  return (blnConfirm) ? true : false;
  this.form.closename.value = ''
  
} //close brace here
 
	else {
		var submitDone;
		submitDone = 0;
		theForm.LogCallButton.disabled = true;
	theForm.LogCallButton.value = 'Logging..';
	if (submitDone == 0){
		submitDone = 1;
		return true;
	}
	else{
		if (!theForm.LogCallButton){
			alert("Logging Call... Please wait...");
		}
	return false;
	}
}
// End Validation script ---------------------------------------------------------
 
 
HERE ARE THE SUBMIT BUTTONS:
 
<input class="btn" name="LogCallButton" type="submit" value="Log Call" onClick="this.form.closename.value = ''; " tabindex="15">
 
                  
<input class="btn" name="LogCallButton" type="submit" value="Close Call" onClick="this.form.closename.value = '<%=loginname%>'; " tabindex="16">

Open in new window

0
 

Author Comment

by:tsmithinnc
ID: 20329135
OK....
NOW it works...EXCEPT for the drop down scenario AND if I am closing it isn't requiring the 'problem' field and it should...it does if I submit???
 // Validation script -------------------------------------------------
var submitDone;
submitDone = false;
function validate(theForm){
  if (theForm.Category.selectedIndex <1){
    theForm.Category.focus();
    alert("Please select a Category!");
    return false;
  }
  if (theForm.CatSubj.selectedIndex <1){
    theForm.CatSubj.focus();
    alert("Please Select a Subject!");
    return false;
  }
    if (theForm.closename.value != "" && theForm.Resolution.value == ""){
    theForm.Resolution.focus();
    alert("Please key in the 'Resolution' field!");
    return false;
  }
  
 
 if (theForm.closename.value != '<%=CSR%>' && theForm.closename.value != ""){
   var blnConfirm1 = confirm('You did not open this call, are you sure you want to close this log?');
  	return (blnConfirm1) ? true : false;
  	this.form.closename.value = ''
 		return false;
  }
 
   if (theForm.closename.value != "" ){
   	var blnConfirm = confirm('Are you sure you want to close this log?');
  	return (blnConfirm) ? true : false;
  	this.form.closename.value = ''
 		return false;
  }
  
  if (theForm.Problem.value == ""){
    theForm.Problem.focus();
    alert("Please key in the 'Problem' field!");
    return false;
  }
 
var chk = false;
      for(i=0; i<rs.length; i++){
            if(rs[i].checked){ chk = true;break;}
      }
      if(!chk){
          alert('You must select one of the choices!');
          return false;
      }
 
  theForm.LogCallButton.disabled = true;
  theForm.LogCallButton.value = 'Logging..';
  if (!submitDone){
    submitDone = true;
    return true;
  }
  if (!theForm.LogCallButton){
    alert("Logging Call... Please wait...");
  }
  return false;
}
// End Validation script ---------------------------------------------------------

Open in new window

0
 

Author Comment

by:tsmithinnc
ID: 20330514
mplungjan:  WE DID IT!!!
And I put that piece in there like you said...and didn't give up!!!
IT IS BEAUTIFUL!!!
THANK YOU!
 // Validation script -------------------------------------------------
var submitDone;
submitDone = false;
function validate(theForm){
	
	  
  if (theForm.Problem.value == ""){
    theForm.Problem.focus();
    alert("Please key in the 'Issue(s)' field!");
    return false;
 
  }
  if (theForm.closename.value != "" && theForm.Resolution.value == ""){
    theForm.Resolution.focus();
    alert("Please key in the 'Resolution' field!");
    return false;
  }
 
  var rs = document.getElementsByName('ownstatus');
			var chk = false;
      for(i=0; i<rs.length; i++){
            if(rs[i].checked){ chk = true;break;}
      }
      if(!chk){
          alert('You must select either temporary or permanent assignment');
          return false;
      }
 
 
 	if (theForm.closename.value != '<%=CSR%>' && theForm.closename.value != ""){
  	var blnConfirm1 = confirm('You did not open this call, are you sure you want to close this log?');
  	return (blnConfirm1) ? true : false;
  	this.form.closename.value = ''
 		return false;
  }
  
 
   if (theForm.closename.value != "" ){
   	var blnConfirm = confirm('Are you sure you want to close this log?');
  	return (blnConfirm) ? true : false;
  	this.form.closename.value = ''
 		return false;
 
      }
 
  theForm.LogCallButton.disabled = true;
  theForm.LogCallButton.value = 'Logging..';
  if (!submitDone){
    submitDone = true;
    return true;
  }
  if (!theForm.LogCallButton){
    alert("Logging Call... Please wait...");
  }
  return false;
}
// End Validation script ---------------------------------------------------------

Open in new window

0
 

Author Comment

by:tsmithinnc
ID: 20330763
ARGGGGGGGGGGGGGGG....it is FORCING this:  var rs = document.getElementsByName('ownstatus');
                  var chk = false;
      for(i=0; i<rs.length; i++){
            if(rs[i].checked){ chk = true;break;}
      }
      if(!chk){
          alert('You must select either temporary or permanent assignment');
          return false;
      }
 
no matter what???  I only want it to enforce it if there is a selection from the AssignOwner Dropdown box  :(
What did I do wrong???
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20333647
You did not do anything wrong. Just did not tell us about the AssignOwner...

Here is a modified script with some changes and assumptions... Hope they were right


 // Validation script -------------------------------------------------
var submitDone;
submitDone = false;
function validate(theForm){
        
  if (theForm.Problem.value == ""){
    theForm.Problem.focus();
    alert("Please key in the 'Issue(s)' field!");
    return false;
  }
  if (theForm.closename.value != "" && theForm.Resolution.value == ""){
    theForm.Resolution.focus();
    alert("Please key in the 'Resolution' field!");
    return false;
  }
  // test only if AssignOwner has been selected
  if (theForm.AssignOwner.selectedIndex > 0) { // assuming first option is "Please select"  
    var rs = theForm.ownstatus;
    var chk = false;
    for(var i=0; i < rs.length; i++){
      if(rs[i].checked){ chk = true;break;}
    }
    if(!chk){
      alert('You must select either temporary or permanent assignment');
      return false; // return to the form
    }
   }
 
   if (theForm.closename.value != '<%=CSR%>' && theForm.closename.value != ""){
      if (confirm('You did not open this call, are you sure you want to close this log?')) {
    // the following two lines were NEVER executed. What do you want to to if OK to close the log
    // and what if CANCEL close the log
        this.form.closename.value = ''; // clear the value
        return true; // submit the form
      }
      return false; // return to the form
  }
 
  if (theForm.closename.value != "" ){
     if confirm('Are you sure you want to close this log?') {
        this.form.closename.value = ''
        return true; // submit the form
     }
     return false // return to the form
  }
  // Here we are only arriving if closename was blank and the form was otherwise ok
  theForm.LogCallButton.disabled = true;
  theForm.LogCallButton.value = 'Logging..';
  if (!submitDone){
    submitDone = true;
    return true;
  }
  if (!theForm.LogCallButton){ // if there is no LogCallButton (?)
    alert("Logging Call... Please wait...");
  }
  return false; // cancel submit
}
// End Validation script ---------------------------------------------------------

Open in new window

0
 

Author Comment

by:tsmithinnc
ID: 20338925
Here is a look at 'assignOwner'...
the latest works, but doesn't acknowledge anything with assignowner...maybe becasue I have it built the way I do ??

 	<select size="1"  name="assignOwner"  tabindex="2" id="sel"  onChange="if(this.options[this.selectedIndex].value!=''){document.getElementById('myFieldSet').style.visibility='visible'}else{document.getElementById('myFieldSet').style.visibility='hidden'}">
 		<%
dim mRSa
dim mRS1a
 
 
set mRSa = server.createobject("ADODB.Recordset")
mRSa.Open "SELECT DISTINCT deptGroup from EmpDB where active= 1 and support= 1 and Business = 'Drake Software' order by deptGroup", Conn
 
set mRS1a = server.createobject("ADODB.Recordset")
 
response.write "<option selected value= ''   >Select associate to assign this call to:</option>"
do while not mRSa.EOF
   response.write "<option value='"& mRsa.fields("deptGroup") &"'  >------:" & mRsa.fields("deptGroup") & ":------</option>" 
   mRS1a.Open "select * from EmpDB where deptGroup='" & mRsa.fields("deptGroup") & "' and Business = 'Drake Software' and support ='1' and active ='1' Order By fname, lname", Conn
   do while not mRS1a.EOF%>
   	 <option value=<%= mRS1a("login")%>   ><%= mRS1a("fName")%>&nbsp;<%= mRS1a("lName")%></option>
         <%'response.write "<option   >" & mRS1a.fields("fname") & "</option>"
         mRS1a.Movenext
   loop
  mRS1a.Close
  mRSa.Movenext
loop
%>
</select>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 20339453
if (theForm.AssignOwner.selectedIndex > 0) { // assuming first option is "Please select"  

is not good if the name is
assignOwner and not AssignOwner

JS is case sensitive....

How do you see if assignOwner is chosen if there is no
"please select" ?

You could try

if (theForm.assignOwner.selectedIndex > -1) { // assuming it is -1 when nothing is selected


0
 

Author Comment

by:tsmithinnc
ID: 20339585
AHHHHHHHHHHHHH...WITH ALL DUE RESPECT TO YOUR WIFE :)
I LOVE YOU!!!
I am good to go...just sent to my tester!!!
YEAH!!!!!!

HUGS HUGS HUGS HUGS!!!
0
 

Author Closing Comment

by:tsmithinnc
ID: 31409912
THIS MAN IS A SAINT!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20342114
You are welcome... Now we need to get the  ARGGGGGGGGGGGGGGG.. off the end of this question ;)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question