Link to home
Start Free TrialLog in
Avatar of tsmithinnc
tsmithinncFlag for United States of America

asked on

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;
      }
}
Avatar of imitchie
imitchie
Flag of New Zealand image

var rs = document.getElementsByName('ownstatus'); ?
i know there is getElementsByTagName and getElementByID..
is this IE/FFox/Safari/Konqueror?
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...
Avatar of tsmithinnc

ASKER

It is for IE and firefox...
     

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

Open in new window

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

Open in new window

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

: (
so it isn't possible :(
Of course it is possibe - anything is possible - you were on the right track when you combined the two functions.
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

If the first 'ownstatus' is checked, you return true. No further processing occurs.
Avatar of Michel Plungjan
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???
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?!?!
See my comment #20311952 for part of your problem: wromg thimkimg.
Then how do I get around that?
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.
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

as your validate() always returns false the action is never called
How so?

    if (submitDone == 0){
            submitDone = 1;
            return true;
      }
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...
Tough to be a hero ehh mplungjian ?   I'm TRYING.... HHHHHUGGGGGGGGGS
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)
@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...
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...
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:

https://www.experts-exchange.com/questions/22934232/What-am-I-missing-here-Form-validation.html?anchorAnswerId=20198870#a20198870
You are appreciated...
So are we done here? I'm thinking this horse is dead, eh?
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...
:) 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!
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.
@tsmithinnc: Apologies for how this thread turned out

Do you need more help implementing the new validation?
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!
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:
https://www.experts-exchange.com/questions/22934232/What-am-I-missing-here-Form-validation.html?anchorAnswerId=20198870#a20198870

plus the suggestion by gops: http:#20317354


Meeting at 9:15 , then I will put this in place...
I HOPE :)
THANK YOU!!!!
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

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

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

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???
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

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

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!!
THIS MAN IS A SAINT!
You are welcome... Now we need to get the  ARGGGGGGGGGGGGGGG.. off the end of this question ;)