...

View Full Version : Resolved Have Text Field Validation need to add Checkbox Validation



SyncSpin
01-15-2012, 09:21 PM
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.



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

jmrker
01-15-2012, 11:02 PM
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.



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

<input type="checkbox" id="CBox" value="CheckBox setting">
You could add something like this to your function above:


if ( (oops != "") && (document.getElementById('CBox').checked == false) )
{
alert("ERROR:" + oops);
return false;
}

SyncSpin
01-15-2012, 11:13 PM
Here is everything:



<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
01-16-2012, 02:40 AM
I figured it out myself. What I did was add the following:





if (
document.sig.tc.checked == false)
{
alert ('You didn\'t agree to the consent!');
return false;
} else {
return true;
}

xelawho
01-16-2012, 03:22 AM
cool... but why are you looping though your text fields as if there are various? Wouldn't it work the same like this:



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

felgall
01-16-2012, 09:03 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum