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 01-15-2012, 08:21 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
Have Text Field Validation need to add Checkbox Validation

Hey all.

I have a simple validation I need to do. I need to just make sure that a Checkbox is checked, and that a Text field has content. Sounds simple but I cannot find any thing that has a check and a text field.

Here what I have. Can I modify this script to do this? A Checkbox MUST be checked and Text field MUST be filled out. This currently does the text field fine, but no Checkbox obviously.

How can I add a checkbox validation to this?

Thats it. Any help is appreciated.

Code:
<script type="text/javascript">

var textFields = ["digsig"]; 

function validateForm( )
{
    var oops = ""; // must initialize this!
    var form = document.sig;

        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 ) 
              {
                    oops += "You MUST enter your Digital Signature";
              }
        }
        if ( oops != "" )
        {
            alert("ERROR:" + oops);
            return false;
        }
    }


}

</script>

Last edited by SyncSpin; 01-16-2012 at 01:41 AM..
SyncSpin is offline   Reply With Quote
Old 01-15-2012, 10:02 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb More information would be nice ...

Quote:
Originally Posted by SyncSpin View Post
Hey all.

I have a simple validation I need to do. I need to just make sure that a Checkbox is checked, and that a Text field has content. Sounds simple but I cannot find any thing that has a check and a text field.

Here what I have. Can I modify this script to do this? A Checkbox MUST be checked and Text field MUST be filled out. This currently does the text field fine, but no Checkbox obviously.

How can I add a checkbox validation to this?

Thats it. Any help is appreciated.

Code:
<script type="text/javascript">

var textFields = ["digsig"]; 

function validateForm( )
{
    var oops = ""; // must initialize this!
    var form = document.sig;

        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 ) 
              {
                    oops += "You MUST enter your Digital Signature";
              }
        }
        if ( oops != "" )
        {
            alert("ERROR:" + oops);
            return false;
        }
    }


}

</script>
What does your HTML look like?
I'm not sure I understand how your validation of the text works
as the length of any input cannot be less than zero without some additional math.

You also don't show what the checkbox is called.
Assuming it is something like:
Code:
<input type="checkbox" id="CBox" value="CheckBox setting">
You could add something like this to your function above:
Code:
        if ( (oops != "") && (document.getElementById('CBox').checked == false) )
        {
            alert("ERROR:" + oops);
            return false;
        }
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
SyncSpin (01-15-2012)
Old 01-15-2012, 10:13 PM   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
Here is everything:

Code:
<script type="text/javascript">

var textFields = ["digsig"]; 

function validateForm( )
{
    var oops = ""; // must initialize this!
    var form = document.sig;

        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 ) 
              {
                    oops += "You MUST enter your Digital Signature";
              }
        }
        if  (oops != "" )
        {
            alert("ERROR:" + oops);
            return false;
        }
    


}

</script>

<form method=\"post\" name=\"sig\" class=\"eshop eshop-confirm\" action=\"".$this->autoredirect."\" onsubmit=\"return validateForm()\">
<input type=\"checkbox\" name=\"tc\" id=\"tc\" value=\"1\" />
<input type=\"text\" id=\"digsig\" name=\"digsig\" />
</form>
Thanks for any assistance
SyncSpin is offline   Reply With Quote
Old 01-16-2012, 01:40 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
I figured it out myself. What I did was add the following:

Code:

if (
	document.sig.tc.checked == false) 
	{
		alert ('You didn\'t agree to the consent!');
		return false;
	} else { 	
		return true;
	}
SyncSpin is offline   Reply With Quote
Old 01-16-2012, 02:22 AM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
cool... but why are you looping though your text fields as if there are various? Wouldn't it work the same like this:

Code:
function validateForm(){

value = document.sig.digsig.value.replace(/^\s+/,"").replace(/\s+$/,""); // trim the input
if ( value.length < 1 ) {
		alert("ERROR: You MUST enter your Digital Signature");
		return false;
		}
if (document.sig.tc.checked==false){
		alert ("You didn't agree to the consent!");
		return false;
		} else {
return true;
}
xelawho is offline   Reply With Quote
Old 01-16-2012, 08:03 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Rather than

document.sig.tc.checked==false

you would do better to use either

document.sig.tc.checked===false

or simply

!document.sig.tc.checked

Neither of which allows for '' or 0 or null or undefined as matching the way == does.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall 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 02:52 AM.


Advertisement
Log in to turn off these ads.