Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2011, 11:35 PM   PM User | #1
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
Guidance on required field if check box

Hello all.

I need to use javascript to validate 6 text inputs to make sure data is entered and they are not empty. However, there is a checkbox above this and if the checkbox is checked I need these areas to NOT validate as they become optional at that point.

Here is what I have:

Code:
function validateCheckbox() {
if(document.forms["form1"].sales.checked) {
  if(document.forms["form1"].input1.value.length < 1) { // x
    return false;
   }
  else {
    return true;
   }
}
}
The above is if the box is checked to validate the field. I want it to do the opposite. How would you do the above with the If statement referring if the check box is NOT checked? And how could I add more fields to check then just one?

I am new to javascript so any help or guidance is appreciated.

Last edited by SyncSpin; 12-15-2011 at 03:50 AM..
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 12:25 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
    var textFields = ["name","address","city","zip","email","phone"]; // or whatever your text field names are

function validateForm( )
{
    var form = document.form1;
    if ( ! form1.sales.checked )
    {
        for ( var t = 0; t < textFields.length; ++t )
        {
              var field = form[textFields[t]];
              var value = field.value.replace(/^\s+/,"").replace(/\s+$/,""); // trim the input
              if ( value.length < 3 ) /* 3 is arbitrary...choose what you prefer */
              {
                    oops += "\n" + field.name + " does not seem to be filled in";
              }
        }
        if ( oops != "" )
        {
            alert("Please correct these errors:" + oops);
            return false;
        }
    }
    ... other validation not dependent on that checkbox ...

}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
SyncSpin (12-15-2011)
Old 12-15-2011, 01:37 AM   PM User | #3
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
Amazing help. Thank you for posting. Gonna play around to see if I can understand it!
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 02:10 AM   PM User | #4
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
Ok looks like I need a bit more help as its currently not working with no error.

Here is my full code.

Code:


<script type="text/javascript">

var textFields = ["node","pres","knocks","sales","contact","ws"]; // or whatever your text field names are

function validateForm( )
{
    var form = document.wowsales;
    if ( ! wowsales.off.checked )
    {
        for ( var t = 0; t < textFields.length; ++t )
        {
              var field = form[textFields[t]];
              var value = field.value.replace(/^\s+/,"").replace(/\s+$/,""); // trim the input
              if ( value.length < 1 ) /* 3 is arbitrary...choose what you prefer */
              {
                    oops += "\n" + field.name + " does not seem to be filled in";
              }
        }
        if ( oops != "" )
        {
            alert("Please correct these errors:" + oops);
            return false;
        }
    }


}

</script>


    
        
        
      
       	
  
  
  <form action="process.php" name="wowsales" method="post" onsubmit="return validateForm()">


  <b>OFF?  </b><input type="checkbox" name="off" value="yes" />

Required Fields: * if not OFF

  <input type="text" size="10" name="node">
<input type="text" size="10" name="press">
  <input type="text" size="10" name="knocks">
<input type="text" size="10" name="sales">
 <input type="text" size="10" name="contact">
<input type="text" size="10" name="ws">

 Optional Fields:
<input type="text" size="10" name="doort">
<input type="text" size="10" name="webl">
<input type="text" size="10" name="total">
<input type="text" size="10" name="appt">
<input type="text" size="10" name="phone">
<input type="text" size="10" name="waf">
  

 
<input type="submit" value="Submit Info">
  
  </form>
  

  

 </div>

</body>
</html>
Right now it submits every time no matter.

Last edited by SyncSpin; 12-15-2011 at 02:33 AM..
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 03:07 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
One goof mine, one goof yours.
Code:
var textFields = ["node","press","knocks","sales","contact","ws"]; 

function validateForm( )
{
    var oops = ""; // must initialize this!
    var form = document.wowsales;
    if ( ! wowsales.off.checked )
      ...
You spelled "press" wrong. I omitted the initialization of the oops variable.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
SyncSpin (12-15-2011)
Old 12-15-2011, 03:12 AM   PM User | #6
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
Thanks again for the help. Unfortunately, nothing happens when I submit the form. I submit with everything empty with and without the box checked and it works every time with no error.

I added the on submit to the form, what am I missing?
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 03:20 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
With those two changes, it works perfectly for me.

Now, none of the *OTHER* fields are being validated, but you didn't ask for that.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
SyncSpin (12-15-2011)
Old 12-15-2011, 03:26 AM   PM User | #8
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
I am going crazy. On my version it just works. What could I possibly be missing?

Here is a test link:

http://www.syncspin.com/wowsales/teststuff.php

Am I missing something small?
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 03:30 AM   PM User | #9
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
Oh! In Safari it works but Firefox it does not. Probably something I have off or something.
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 03:41 AM   PM User | #10
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
DO I need to add .document in front of anything for firefox?
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 03:49 AM   PM User | #11
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
Figured it out. Had to add document. to the wowsales.off.checked


Thanks!
SyncSpin is offline   Reply With Quote
Old 12-15-2011, 05:15 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
No, actually the only place you should have used document.wowsales is in the very first line:
Code:
    var form = document.wowsales;
From there on out, you just use form in place of document.wowsales. What's the point in having variable for handy use if you don't use them?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-15-2011, 01:20 PM   PM User | #13
SyncSpin
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
SyncSpin is an unknown quantity at this point
See, I am still learning. lol Thanks for that tip. I didn't realize that the variable could be used in that way.
SyncSpin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:10 AM.


Advertisement
Log in to turn off these ads.