PDA

View Full Version : Validating checkbox


ramandeep
12-28-2005, 09:02 AM
Hi,
This is a PHP registration page,all other fileds have are bieng validated by the javascript.
Now i have added a new checkbox for terms and condition in the form.I need to validate it aswell.
Kindly guide!

<?php
}

function registerForm($option, $useractivation) {
?>
<script language="javascript" type="text/javascript">
function submitbutton() {
var form = document.mosForm;
var r = new RegExp("[\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-]", "i");

// do field validation
if (form.name.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_NAME);?>" );
}else if (form.username.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_UNAME);?>" );
} else if (r.exec(form.username.value) || form.username.value.length < 3) {
alert( "<?php printf( html_entity_decode(_VALID_AZ09), html_entity_decode(_PROMPT_UNAME), 2 );?>" );
} else if (form.email.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_MAIL);?>" );
} else if (form.password.value.length < 6) {
alert( "<?php echo html_entity_decode(_REGWARN_PASS);?>" );
} else if (form.password2.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_VPASS1);?>" );
} else if ((form.password.value != "") && (form.password.value != form.password2.value)){
alert( "<?php echo html_entity_decode(_REGWARN_VPASS2);?>" );
} else if (r.exec(form.password.value)) {
alert( "<?php printf( html_entity_decode(_VALID_AZ09), html_entity_decode(_REGISTER_PASS), 6 );?>" );
} else {
form.submit();
}
}
</script>
<div class="componentheading">
<?php echo _REGISTER_TITLE; ?>
</div>
<form action="index.php" method="post" name="mosForm" >
This a Term for registration!
<p><input type="CHECKBOX" name="siteterms" value="" checked> I accept</p>

<table cellpadding="0" cellspacing="0" border="0" width="100%" class="contentpane">
<tr>
<td colspan="2"><?php echo _REGISTER_REQUIRED; ?></td>
</tr>
<tr>
<td width="30%"><?php echo _REGISTER_NAME; ?> *</td>
<td><input type="text" name="name" size="40" value="" class="inputbox" /></td>
</tr>

<tr>

<td><?php echo _REGISTER_UNAME; ?> *</td>
<td><input type="text" name="username" size="40" value="" class="inputbox" /></td>
<tr>

<td><?php echo _REGISTER_EMAIL; ?> *</td>
<td><input type="text" name="email" size="40" value="" class="inputbox" /></td>
</tr>

<tr>
<td><?php echo _REGISTER_PASS; ?> *</td>
<td><input class="inputbox" type="password" name="password" size="40" value="" /></td>
</tr>

<tr>
<td><?php echo _REGISTER_VPASS; ?> *</td>
<td><input class="inputbox" type="password" name="password2" size="40" value="" /></td>
</tr>

<tr>
<td colspan="2"></td>
</tr>

<tr>
<td colspan=2>
</td>
</tr>
</table>
<input type="hidden" name="id" value="0" />
<input type="hidden" name="gid" value="0" />
<input type="hidden" name="useractivation" value="<?php echo $useractivation;?>" />
<input type="hidden" name="option" value="<?php echo $option; ?>" />
<input type="hidden" name="task" value="saveRegistration" />
<input type="button" value="<?php echo _BUTTON_SEND_REG; ?>" class="button" onclick="submitbutton()" />
</form>
<?php
}

}
?>

Creative Insanity
12-28-2005, 09:23 AM
DW has a behaviour for validating forms.
I use it all the time and it is rather nice.

ramandeep
12-28-2005, 09:46 AM
Yups CI even i use that behaviour but here in this case we are calling a validating funtion on form submit!

I think we cant call 2 functions on form submit !

I am looking for how to validate the checkbox in this function
<script language="javascript" type="text/javascript">
function submitbutton() {
var form = document.mosForm;
var r = new RegExp("[\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-]", "i");

// do field validation
if (form.name.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_NAME);?>" );
}else if (form.username.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_UNAME);?>" );
} else if (r.exec(form.username.value) || form.username.value.length < 3) {
alert( "<?php printf( html_entity_decode(_VALID_AZ09), html_entity_decode(_PROMPT_UNAME), 2 );?>" );
} else if (form.email.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_MAIL);?>" );
} else if (form.password.value.length < 6) {
alert( "<?php echo html_entity_decode(_REGWARN_PASS);?>" );
} else if (form.password2.value == "") {
alert( "<?php echo html_entity_decode(_REGWARN_VPASS1);?>" );
} else if ((form.password.value != "") && (form.password.value != form.password2.value)){
alert( "<?php echo html_entity_decode(_REGWARN_VPASS2);?>" );
} else if (r.exec(form.password.value)) {
alert( "<?php printf( html_entity_decode(_VALID_AZ09), html_entity_decode(_REGISTER_PASS), 6 );?>" );
} else {
form.submit();
}
}
</script>

davidj
12-28-2005, 09:53 AM
think we cant call 2 functions on form submit !

yes you can. well sorta


create functions...
function1 does stuff

function no1(){
bla bla bla

}

function2 does stuff

function no2(){
bla bla bla

}


function3 fires the above and submits when finished

function no3(){

no1()
no2()
document.form1.submit();
}

NOTE: change your button from a submit to a normal button then add an onClick="no3()" event

also NOTE that bla bla bla is not javascript

ramandeep
12-28-2005, 09:58 AM
Thank you David,
I am givin it a try!

And i will not write " bla bla bla "
Cheers!

davidj
12-28-2005, 10:28 AM
or to answer that validation Q


just add this..

else if (document.form1.checkbox.checked=false) {

alert("the box is not checked")
}
elseif (bla bla bla){

etc etc
}

ramandeep
12-28-2005, 11:33 AM
Thanx DJ,
Thats what i did and i came to tell you that i did this way!

And you the master already have written that here :D

Thanks David and CI ,we did a new tweak in mambo now!

Creative Insanity
12-28-2005, 06:37 PM
DJavascript, the new language!

<script language="DJavaScript">
document.write('.bla bla {etc.etc, blah.bla, ++ etc etc}\n')
</script>
LOL

ramandeep
12-28-2005, 09:16 PM
LOL CI,
DJ must be coming with the response!

Cheers!

Creative Insanity
12-28-2005, 09:53 PM
Yeah but needs debugging as when I tried it FF smacked me in the ear and told me to stop being such a fool LOL

davidj
12-29-2005, 08:38 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">

function youarean(){
var a = 'I';
var e = 't';
var d = 'o';
var c = 'i';
var b = 'd';

for(i=0; i < 50; i++){

alert('you are an '+a+b+c+d+e);

}
}
</script>

</head>

<body onLoad="youarean()">
</body>
</html>

Creative Insanity
12-29-2005, 04:26 PM
Very funny that dj. Very funny.
Love the way it locks the browser. hehe
Is that de famous djavascript :P