Solved

Javascript validation problem

Posted on 2007-11-19
45
230 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
Comment Utility
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
Comment Utility
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
Comment Utility
It is for IE and firefox...
0
 

Author Comment

by:tsmithinnc
Comment Utility
     

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
Comment Utility
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
Comment Utility
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
Comment Utility
: (
so it isn't possible :(
0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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
Comment Utility
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
Comment Utility
If the first 'ownstatus' is checked, you return true. No further processing occurs.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
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
Comment Utility
See my comment #20311952 for part of your problem: wromg thimkimg.
0
 

Author Comment

by:tsmithinnc
Comment Utility
Then how do I get around that?
0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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
Comment Utility
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
Comment Utility
as your validate() always returns false the action is never called
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
How so?

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

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
Tough to be a hero ehh mplungjian ?   I'm TRYING.... HHHHHUGGGGGGGGGS
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 29

Expert Comment

by:Badotz
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
You are appreciated...
0
 
LVL 29

Expert Comment

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

Author Comment

by:tsmithinnc
Comment Utility
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
Comment Utility
:) 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
Comment Utility
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
Comment Utility
@tsmithinnc: Apologies for how this thread turned out

Do you need more help implementing the new validation?
0
 

Author Comment

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

Author Comment

by:tsmithinnc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
THIS MAN IS A SAINT!
0
 
LVL 75

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now