Click to See Complete Forum and Search --> : Also another qu


DanUK
04-16-2003, 06:02 PM
Hello guys. Another question too.
I will paste my script below, I was wondering instead of bringing up 'seperate' alerts, it would just bring them up in one box like 'The following fields were misformed, please retry:' and then list them. At the moment it comes up all seperately.

Here's the HEAD script:

<script language="javascript"><!--

function simpleCheck(form) {

if (!/^[a-z0-9]+$/i.test(form.newuser.value)) {
alert("Please include your desired use.");
form.newuser.select(); }

else if (form.userPASS.value =="") {
alert("Please include your desired user password.");
form.userPASS.select(); }

else if (form.userRPTPASS.value =="") {
alert("Please repeat your desired user password.");
form.userRPTPASS.select(); }

else if (form.userPASS.value != form.userRPTPASS.value){
alert("Your passwords did not match, please try again.");
}

else if (!/^\w+@\w+\.[a-z]{2,3}(\.[a-z]{2})?$/i.test(form.userEMAIL.value)) {
alert("Please include your e-mail address. It should be in the format of you@host.com.");
form.userEMAIL.select(); }

else if (form.policyON.checked==false) {
alert("You forgot to tick the box stating you agree to our terms.");
form.policyON.select(); }

else {
form.method="POST";
form.target="_self"
form.submit();
}

}
//--></script>

Now, the BODY script:

<form onSubmit="simpleCheck(this); return false;" form action="http://my post script" method="POST">

Thanks!

DanUK
04-16-2003, 06:19 PM
:( *confused look* :eek:

DanUK
04-16-2003, 06:25 PM
well..not exactly. I just have no idea, sorry.

Thanks for any help. This forum is such a help, i'm really that clueless :\

DanUK
04-16-2003, 07:22 PM
didnt' seem to work. Do i need to change the body line too? thanks

DrDaMour
04-16-2003, 07:24 PM
you need to specify how it didn't work, although i did notice that the submit thing was not put inside an else{ } brace set, which it woudl need to be otherwise the form would always submit. if that was your problem

DanUK
04-16-2003, 07:27 PM
yes it just submitted, didnt check anything. Which submit line was it you saw that on, and what should it be sorry?

DrDaMour
04-16-2003, 07:31 PM
if (errors != "" {
str = str.substr(0,str.length-2);
alert("The following fields were misformed, please retry:\r" + errors + ".");
return false;
}
fm.method="POST";
fm.target="_self";
fm.submit();
return true;
}



should be

if (errors != "" {
str = str.substr(0,str.length-2);
alert("The following fields were misformed, please retry:\r" + errors + ".");
return false;
}
else{
fm.method="POST";
fm.target="_self";
fm.submit();
return true;
}
}

see the way an if then else statement works is:

say you have
if(statement){
code1
}
else{
code2
}
code3

in that setup if statement is true then code1 will be executed,
then code3. if statement is false then code2 will execute then code3. See in the original it had that submit outside of any braces so it was getting ran eveerytime the function was called, but you only want it to run when teh form is proper, ie when the errs variable is equal to "". In that case (errs != "") evaluates to false, and the code within else woudl be executed

DanUK
04-16-2003, 07:34 PM
still shows an error on the page and tries to submit. It says line 212, which is:

<form onSubmit="simpleCheck(this); return false;" form action="my.pl.script" method="POST">

DrDaMour
04-16-2003, 07:37 PM
come on help yourself

<form onSubmit="simpleCheck(this); return false;" form action="my.pl.script" method="POST">

read that, that's not even properly formatted HTML, what's the form just floating there?

and what is the return false doing for you?

DanUK
04-16-2003, 07:38 PM
i am trying :( I found it hard to create my site but i find javascript a lot harder

DrDaMour
04-16-2003, 07:51 PM
well i'm not tryign to be mean, but you could look at that snipet and see taht form just floating there and know that was wrong. I'm trying to help you learn how to do it, not just give you the answer.

also look at that last if statement

if (errors != "" {code}


now as i said before if statments have to look like

if (statement){code}

see what's wrong with it?

DrDaMour
04-16-2003, 09:33 PM
sorry i did miss that you were using the value of errs as a sentinel to leave the function early. You sneaky fellow, you! I get upset when i explain teh logic behind teh code, and people reply 5 seconds later with, "i copied and pasted yoru code exactly and it doens't work, fix it" without them even hazarding an attempt at making it work themselves

DanUK
04-17-2003, 04:59 AM
Thanks for the help.

Here's my £0.02 worth ... I know it must be frustrating for you guys having to answer all types of queries etc, and to you, I understand, it must seem like we just take what you say and don't try at all.
Some people like me, really don't find things like this easy. I personally spent a lot of time learning HTML, or trying to, javascript I could never really understand or try to get working. That's why people liek you, and places like this are brililiant. It might not show that people are actually thankful for your help, but personally I am.

anyway thanks again :)

DanUK
04-17-2003, 06:18 AM
still not working either :(
It just seems to submit..IE says error on page, when I view that it says line 23 ... which is the first str += line

DanUK
04-17-2003, 07:23 AM
hi again dave, thanks.
Unfortunately my site is down, I'm having a lil dispute with my ISP who is currently hosting it. Thats why i'm taking chances now to get it how i like it.

The code i've used is below, thanks again:

<script language="javascript">
<!--
function simpleCheck(fm) {
var errors = "";
if (!/^[a-z0-9]+$/i.test(fm.newuserUSERNAME.value)) {
str += "Please include your desired username. If it contains any other form of text (other than A-Z, a-z or 0-9) please remove it.;\r";
fm.newuserUSERNAME.select();
}
if (fm.newuserPASS.value =="") {
str += "Please include your desired username password.;\r";
fm.newuserPASS.select();
}
if (fm.newuserRPTPASS.value =="") {
str += "Please repeat your desired username password.;\r";
fm.newuserRPTPASS.select();
}
if (fm.newuserPASS.value != fm.newuserRPTPASS.value) {
str += "Your passwords did not match, please try again.;\r";
fm.newuserPASS.select();
}
if (!/^\w+@\w+\.[a-z]{2,3}(\.[a-z]{2})?$/i.test(fm.newuserEMAIL.value)) {
str += "Please include your e-mail address. It should be in the format of you@host.com.;\r";
fm.newuserEMAIL.select();
}
if (fm.AGEAUPon.checked==false) {
str += "You must be 13 or over in order to submit this form, if you are please make sure you've checked the relevant box.;\r";
fm.AGEAUPon.select();
}
if (errors != "") {
str = str.substr(0,str.length-2);
alert("The following fields were not completed correctly, please check and try again:\r" + errors + ".");
return false;
}
return true;
}
//-->
</script>

and then in body:

<form method="POST" action="http://cgi.uk2.net/cgi-bin/uk2-formmail.pl" target="_self" onSubmit="return simpleCheck(this);">

DanUK
04-17-2003, 07:56 AM
thank you so much !