Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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 02:41 AM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    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;
            }

  • Users who have thanked jmrker for this post:

    SyncSpin (01-15-2012)

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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;
    	}

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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;
    }

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •