Javascript validation problem

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;
      }
}
tsmithinncAsked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
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
 
imitchieCommented:
var rs = document.getElementsByName('ownstatus'); ?
i know there is getElementsByTagName and getElementByID..
is this IE/FFox/Safari/Konqueror?
0
 
BadotzCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
tsmithinncAuthor Commented:
It is for IE and firefox...
0
 
tsmithinncAuthor Commented:
     

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

Open in new window

0
 
gops1Commented:
Try
<form method="POST" onsubmit="if(validate(this)) frmChk()" action="LogUpdate.asp" name="CallLog"  >
 

Open in new window

0
 
BadotzCommented:
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
 
tsmithinncAuthor Commented:
: (
so it isn't possible :(
0
 
BadotzCommented:
Of course it is possibe - anything is possible - you were on the right track when you combined the two functions.
0
 
BadotzCommented:
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
 
BadotzCommented:
If the first 'ownstatus' is checked, you return true. No further processing occurs.
0
 
Michel PlungjanIT ExpertCommented:
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
 
tsmithinncAuthor Commented:
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
 
BadotzCommented:
See my comment #20311952 for part of your problem: wromg thimkimg.
0
 
tsmithinncAuthor Commented:
Then how do I get around that?
0
 
BadotzCommented:
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
 
gops1Commented:
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
 
ahoffmannCommented:
as your validate() always returns false the action is never called
0
 
Michel PlungjanIT ExpertCommented:
How so?

    if (submitDone == 0){
            submitDone = 1;
            return true;
      }
0
 
Michel PlungjanIT ExpertCommented:
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
 
tsmithinncAuthor Commented:
Tough to be a hero ehh mplungjian ?   I'm TRYING.... HHHHHUGGGGGGGGGS
0
 
Michel PlungjanIT ExpertCommented:
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
 
BadotzCommented:
@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
 
tsmithinncAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
tsmithinncAuthor Commented:
You are appreciated...
0
 
BadotzCommented:
So are we done here? I'm thinking this horse is dead, eh?
0
 
tsmithinncAuthor Commented:
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
 
tsmithinncAuthor Commented:
:) 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
 
BadotzCommented:
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
 
Michel PlungjanIT ExpertCommented:
@tsmithinnc: Apologies for how this thread turned out

Do you need more help implementing the new validation?
0
 
tsmithinncAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
tsmithinncAuthor Commented:
Meeting at 9:15 , then I will put this in place...
I HOPE :)
THANK YOU!!!!
0
 
tsmithinncAuthor Commented:
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
 
tsmithinncAuthor Commented:
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
 
tsmithinncAuthor Commented:
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
 
tsmithinncAuthor Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
tsmithinncAuthor Commented:
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
 
tsmithinncAuthor Commented:
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
 
tsmithinncAuthor Commented:
THIS MAN IS A SAINT!
0
 
Michel PlungjanIT ExpertCommented:
You are welcome... Now we need to get the  ARGGGGGGGGGGGGGGG.. off the end of this question ;)
0
All Courses

From novice to tech pro — start learning today.