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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello group ;o) i have written t 2

Status
Not open for further replies.

DarkestSello

Programmer
Apr 12, 2001
6
GB
Hello group ;o)

i have written the following script, but i can't quite get it to do what i want it to do

if the checkbox is ticked, i would like the email fields to gray out and not be able to be typed in, and for that piece of the script to be ignored as i don't need to make sure the two email address' are the same - and if the checkbox isn't ticked for the two email's to be checked against each other

i can get both things to work seperately but not together

function validForm(theForm)
//this gets ticked if the user doesn't have an email address
if ((!theForm.noemailaddress.checked) && (theForm.email1.value == ""))
{
alert("Please enter your email address or check the NO email box");
theForm.email1.focus();
return (false);
}

return (true);

//this double checks the email address has been entered the same
if (theForm.email1.value == "") {
alert("You must enter a valid email address")
theForm.email1.focus()
return false
}
if (theForm.email1.value != emailCheck.email2.value) {
alert("Entered emails did not match")
theForm.email1.focus()
theForm.email1.select()
return false
}
return true
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the form
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<form onsubmit=&quot;return validForm(this)&quot; method=POST action=someAction.cgi>
<table>
<tr>
<td align=&quot;right&quot; colspan=&quot;4&quot;>Please tick here if you do not have an email address:</td>
<td align=&quot;left&quot; colspan=&quot;2&quot;><input name=&quot;noemailaddress&quot; type=&quot;checkbox&quot; value=&quot;noemailaddress&quot;></td>
</tr>
<tr>
<td align=&quot;right&quot; colspan=&quot;3&quot;>Email:</td>
<td align=&quot;left&quot; colspan=&quot;3&quot;><input name=&quot;email1&quot; type=&quot;text&quot; size=&quot;20&quot;></td>
</tr>
<tr>
<td align=&quot;right&quot; colspan=&quot;3&quot;>Retype Email:</td>
<td align=&quot;left&quot; colspan=&quot;3&quot;><input name=&quot;email2&quot; type=&quot;text&quot; size=&quot;20&quot;></td>
</tr>
</table>
</form>





tia
.Ds.
 
The best way to do that would be to set up the form entirely dynamic (atleast thats how I would do it). Define a separate function to replace form fields/objects on an event trigger.

For example (just a quick little sample! Sorry, I didn't have time to code more...on my way out the door):

<script language=&quot;JavaScript&quot;>
function replace()
{var email = &quot;&quot;;}

document.writeln(&quot;<form action=\&quot;someAction.cgi\&quot; onSubmit=\&quot;return validForm(this)\&quot;>&quot;);
var email = document.writeln(&quot;<input type=\&quot;text\&quot; name=\&quot;email1\&quot;>&quot;);
</script>

-PAINKILLER
 
try changing it to this:


function validForm(theForm)
{
//only check the fields if the noemailaddress element is unchecked
if(!theForm.noemailaddress.checked) {

//this checks the first email element has a value
if (theForm.email1.value == &quot;&quot;) {
alert(&quot;You must enter a valid email address&quot;)
theForm.email1.focus();
return false;
}
//validate that both email elements contain the same data
if (theForm.email1.value != theForm.email2.value) {
alert(&quot;Entered emails did not match&quot;);
theForm.email1.focus();
theForm.email1.select();
return false;
}
}
return true;
}

function noEmailClick()
{
//set the disabled properties to the opposite of thier current value
//this only works for IE :(

document.emailForm.email1.disabled =
document.emailForm.email2.disabled =
!document.emailForm.email1.disabled;
}



now change your form open tag to look like this:
<form name=&quot;emailForm&quot; onsubmit=&quot;return validForm(this)&quot; method=POST action=someAction.cgi>

lastly change the noemailaddress input to look like this:
<input name=&quot;noemailaddress&quot; type=&quot;checkbox&quot; value=&quot;noemailaddress&quot; onClick=&quot;javascript:noEmailClick();&quot;>

EMail: lgworks at uklinux.net
 
Lachlan

thank you for your reply, and it all works lovely apart from the disabling of the email entry seems to be working backwards....

eg; if you click the no email box - you can type in the email address area & if u unclick it you can't (IE only)

& i'm unsure of how to turn it round...

tia
.Ds.


;o)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top