Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to validate form textbox and chkbox

Status
Not open for further replies.

duph

Programmer
Sep 22, 2005
20
US
Hi all,

I need a simple script to give an error popup message when user hits submit without filling in certain textboxes and 1 checkbox. I have a script that successfully pops up an error when any of the required textboxes are empty, but

problem 1: in firefox after the error popup the form proceeds to my 'submission successful' url. It needs to stop the form instead and return focus to the field in question. (ie does this fine.)

problem 2: i can't get an error popup for the required checkbox - the form proceeds as if it were not required.

Here is the function:

Code:
function IsFormComplete(FormName)
{
var x       = 0
var FormOk  = true

while ((x < document.forms[FormName].elements.length) && (FormOk))
   {
     if (document.forms[FormName].elements[x].value == '')
     { 
        alert('Please enter your '+document.forms[FormName].elements[x].name +' and try again.')
        document.forms[FormName].elements[x].focus()
        FormOk = false 
     }
     x ++
   }
return FormOk
}

...and my form code:

Code:
<FORM name="form1" method="post" action="/cgi-bin/form/trafficrelief/form.cgi"><div id="form1" align="right">
			<input id="name" type="text" name="name" size="25" tabindex="1" /><br />
			<input id="org" type="text" name="org" size="25" tabindex="2" value=" " /><br />
			<input id="email" type="text" name="email" size="25" tabindex="3" /><br />
			<input id="address" type="text" name="address" size="25" tabindex="4" value=" " /><br />
			<input id="zip" type="text" name="zip" size="25" tabindex="5" /><br />
			<input id="phone" type="text" name="phone" size="25" tabindex="6" value=" " /><br />
			<input id="comments" type="text" name="comments" size="25" tabindex="7" value=" " /><br />
			<input id="submit" type="image" src="images/submit.gif" name="submit" tabindex="9" OnClick=IsFormComplete("form1")>
			</div>
	        <input name="endorse" type="checkbox" id="endorse" tabindex="8" value="I_ENDORSE" />
			
			
			
			
			
<INPUT TYPE="HIDDEN" NAME="data_order" 
    VALUE="name,org,email,address,zip,phone,comments,endorse">
<INPUT TYPE="HIDDEN" NAME="submit_to" VALUE="web@transalt.org">
<INPUT TYPE="HIDDEN" NAME="automessage" VALUE="mymessage">
<INPUT TYPE="HIDDEN" NAME="outputfile" VALUE="coalition">
<INPUT TYPE="HIDDEN" NAME="countfile" VALUE="coalition">
<INPUT TYPE="HIDDEN" NAME="emailfile" VALUE="coalition">
<INPUT TYPE="HIDDEN" NAME="form_id" VALUE="Citywide Coalition for Traffic Relief">
<INPUT TYPE="HIDDEN" NAME="ok_url" 
     VALUE="[URL unfurl="true"]http://...">[/URL]
<INPUT TYPE="HIDDEN" NAME="not_ok_url" 
     VALUE="[URL unfurl="true"]http://...">[/URL]
</form>

Can anyone lend a hand? I'm not writing my own js yet so I'm a bit clueless.

Thanks!
 
To prevent your form from submitting when your validation function returns false...

Remove this from the submit button:
Code:
OnClick=IsFormComplete("form1")
Add this to the <form> tag:
Code:
onsubmit="return (IsFormComplete('form1'))"
Regarding the checkbox, you are testing to see if it's value is empty -- but it's value is never empty (it's checked state is all that varies)... so you cannot use the same method to check for a checkbox as you do for a string being entered in a text input.

To access the checkbox "checked" state, you can use this kind of approach:
Code:
document.forms['form1'].elements['endorse'].checked

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
This is great - the first part now works fine.

can you tell me how exactly I should alter this script with your suggestion for the checkbox? I played around with a couple of things but in the end really don't know what needs to change and what needs to stay..
 
1) give your checkboxes the same name
2) create a function to make sure one is checked
Code:
function isOneChecked( cbObj ) {
    // loop through each checkbox
    for ( var i = 0; i < cbObj.length; i++ ) {
        // if the current box is checked, we're good
        if (cbObj[i].checked) return true;
    }
    alert("please check a checkbox!");
    return false;
}
3) include the call to this function
Code:
onsubmit="return (IsFormComplete('form1')) && isOneChecked(this.cbName);"

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
almost paradise, except now it is returning false even when the cb is checked. ?
 
And wait - the checkbox script isn't working in IE
 
it looks to me like your first function will always return false. what are you trying to do with it? are the fields you are trying to check all text boxes? You might want to try something like this:

Code:
function IsFormComplete( f ) {
    // get all fields in the form
    var e = f.elements;

    // loop through each field
    for ( var i = 0; i < e.length; i++ ) {

        // if the element is a text element and the value is blank
        if ( e[i].type.indexOf('text') > -1 && e[i].value == '' ) {
            alert('please enter your ' + e[i].name + '!');
            return false;
        }

    }
    return true;
}

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
what are you trying to verify with the checkbox? just if it has been checked? if that's the case, all you need to do is test the checked property.

Code:
onsubmit="return ( IsFormComplete(this) && this.endorse.checked );


oh, and for MY changes to the IsFormComplete function, you'll now need to call it as I have in this latest post, passing it "this" instead of the form's name.

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
no I'm with you...

so far no problems with IsFormComplete always returning false.

yes all I want is to make sure the cb is checked, so this.endorse.checked <i>works</i> in that the forms stops progress, but i really need an error message so I liked your isOneChecked fx better, if i could only get it to return true.
 


AWESOME! Thanks so much for all your help cLFlaVA and also BabyJeffy.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top