Click to See Complete Forum and Search --> : Um... how do you put multiple actions for onsubmit in a form????


Zion
03-18-2003, 03:58 PM
ok here goes nothing.. this may seem a bit confusing but first lt me post my head... the first javascript is to verify the email address entered is in the correct format etc.. the second part is to make sure they enter the same password twice and it is atleast 6 letter lon and doesnt contain space etc. My form name is emailform if you want to make sure i got the script right too... i got it off javascript.com and have tested both scripts seperatly.. i just dont know how to combine them both

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function emailCheck (emailStr) {

var checkTLD=1;

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;


var emailPat=/^(.+)@(.+)$/;

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";


var validChars="\[^\\s" + specialChars + "\]";

var quotedUser="(\"[^\"]*\")";


var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;


var atom=validChars + '+';


var word="(" + atom + "|" + quotedUser + ")";


var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");



var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("(In the email section) The username contains invalid characters.");
return false;
}
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("(In the email section) The domain name contains invalid characters.");
return false;
}
}

if (user.match(userPat)==null) {

alert("(In the email section) The username doesn't seem to be valid.");
return false;
}

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("(In the email section) Destination IP address is invalid!");
return false;
}
}
return true;
}

// Domain is symbolic name. Check if it's valid.

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("(In the email section) The domain name does not seem to be valid.");
return false;
}
}

if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("(In the email section) The address must end in a well-known domain or two letter " + "country.");
return false;
}

if (len<2) {
alert("(In the email section) This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

// End -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function validatePwd() {
var invalid = " "; // Invalid character is a space
var minLength = 5; // Minimum length
var pw1 = document.emailform.password.value;
var pw2 = document.emailform.password2.value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your password twice.');
return false;
}
// check for minimum length
if (document.emailform.password.value.length < minLength) {
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}

// check for spaces
if (document.emailform.password.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed.");
return false;
}

else {
if (pw1 != pw2) {
alert ("You did not enter the same new password twice. Please re-enter your password.");
return false;
}

else {
alert('Nice job.');
return true;
}
}
}
// End -->
</script>

</head>


This next part is the beginning of my form... when submitted i want it to perform both of the checks... <FORM action="http://www.response-o-matic.com/cgi-bin/rom.pl" method="POST" name=emailform onSubmit="return emailCheck(this.email.value)" onSubmit="return validatePwd()">

khaki
03-18-2003, 04:30 PM
Hi Zion...
I didn't de-bug your code, but there are 2 ways to get multiple functions to run consecutively:

1) use a semi-colon to seperate the functions...
onSubmit="firstFunction()";"secondFunction()"

or

2) have the first function call the second function...

firstFunction()
{
code for first function

secondFunction()
}

secondFunction()
{
code for the second function
}

Just one more thing that you should know...
I'm notoriously bad when I step into the Javascript forum to help... but I saw your question dangling without help for a while, so I figured I'd give it a shot.
Give this a try (you've got nothing to lose) and if I f'ed it up, someone is certain to come and clean-up my mess for you.
Let me know it it works.

hopefully not staining the carpet again...
k

pyro
03-18-2003, 04:42 PM
What K suggested should work to run two functions, but one other option you have for what you are doing is to just combine it into one function. Your choice... ;)

Nedals
03-18-2003, 04:49 PM
One minor flaw in suggestion 1
onSubmit="firstFunction()";"secondFunction()"

should be...
onSubmit="firstFunction(); secondFunction()"

and, if you want to get technical...
onSubmit="return firstFunction(); secondFunction()"

Suggestion 2 is perfect (and preferred) :)
Much easier to debug should you ever have problems.

AdamBrill
03-18-2003, 05:54 PM
Nedals - The problem with this:

onSubmit="return firstFunction(); secondFunction()"

is it only return the value for the first one. So, even if the second one returns false, it still submits.

Anyway, Zion, why don't you use something like this instead?
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function check(formElement){
if(formElement.password.value.length<5){
alert("Password must be at least 5 characters long.");
return false;
}
if(/\s/.test(formElement.password.value)){
alert("Sorry, spaces aren't allowed in the password.");
return false;
}
if(formElement.password.value!=formElement.password2.value){
alert("Passwords don't match");
return false;
}
if(/^\w+@\w+(\.\w{1,6}){0,1}$/i.test(formElement.email.value)){
if(!/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/.test(formElement.email.value.split('.')[1])){ //put on above line
alert("Your e-mail address must end in a well-known domain or two letter country.");
return false;
}
}else{
alert("Please put your e-mail in this format: you@yourdomain.ext");
return false;
}
return true;
}
// End -->
</script>That would go in the head and you would have this in the <form> tag:

onSubmit="check(this)"

Your e-mail input box should be named email. Let me know if you have any problems...

Nedals
03-19-2003, 12:15 AM
Adam, thank's for the heads up. I get somewhat confused as to how/when to use 'return'

What's the difference between this...
onSubmit="return firstFunction();
and this...
onSubmit="firstFunction(); where the function returns true or false.

Should I use a return here... or can the function simply return true/false?
<a href="someurl" onClick="someJSfunction()">....

There are many other times when a JS function is called this way. So is there a 'general' rule?

AdamBrill
03-19-2003, 07:45 AM
Originally posted by Nedals
What's the difference between this...
onSubmit="return firstFunction();
and this...
onSubmit="firstFunction(); where the function returns true or false. The first one will return something, the second one won't. Let's say you have that in a link, like so:

<a href="someurl" onClick="return firstFunction()">

You only want it to follow the link if the firstFunction() returns true. Then you have to have the return in there. Basically, if you don't have the return in there, it will always return true. If you do have it in there, it will return whatever you tell it to in the function. So, if the function is:

function firstFunction(){
return false;
}

It will only return false if you have return firstFunction() in the onSubmit. I hope that helps... ;)

Nedals
03-19-2003, 11:57 AM
Got it!
Thanks Adam :)