PDA

View Full Version : submitting validation


chelvis
08-15-2002, 03:24 PM
I have a form with 3 submit buttons. Now I wrote a validation for one button. If the user doesnt type any thing in the text box, and when user click on the login button it gives an alert.

I wrote an onclick function for that login submit button. But when the alert box comes, when I click on the "OK" button on the alert box the page is blanking out. why? I want the text box to get focus.

<script type="TEXT/JAVASCRIPT" language="javascript">
<!-- /* Hide content from old browsers */

function validateForm(login) {
if (login.emailaddr.value =="") {
alert ("You must enter your email")
login.emailaddr.focus()
return false
}
if (login.emailaddr.value.indexOf ('@',0) == -1 ){
alert("The E-Mail field contains an invalid Email Format.\nPlease enter your correct E-Mail address.");
login.emailaddr.focus();
return false;
}
return true;
}
// end hiding content from old browsers -->
</script>

<!-- code for the form -->

<FORM NAME="login" ACTION="usr_lookup" METHOD="POST">
<INPUT TYPE="submit" NAME="button" VALUE="Back to search">
<INPUT TYPE="button" NAME="button" VALUE="Signup">
<P><A CLASS="BlackText">
If you have not established your own User Profile, click the <b>Signup</b> button above to proceed.
<INPUT TYPE="text" NAME="emailaddr" SIZE="45" MAXLENGTH="100">
<INPUT TYPE="submit" NAME="button" VALUE="Login" onclick="validateForm(login);">
</FORM>

Skyzyx
08-15-2002, 04:12 PM
What's happening is that when you click submit, you are still submitting to "usr_lookup". Nothing is stopping the submission from going through.

What you could do is change the submit to a regular button, and use the javascript to do the submitting, once everything has passed your inspection.

For Example:

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

function validateForm(login)
{
// Checking for any value
if (login.emailaddr.value =="")
{
alert ("You must enter your email");
login.emailaddr.focus();
}

// Checking for the @ symbol
else if (login.emailaddr.value.indexOf ('@',0) == -1 )
{
alert("The E-Mail field contains an invalid Email Format.\nPlease enter your correct E-Mail address.");
login.emailaddr.focus();
}

// Everything good? Submit it!
else document.login.submit();
}

//-->
</script>

<FORM NAME="login" ACTION="usr_lookup" METHOD="POST">
<INPUT TYPE="submit" NAME="button" VALUE="Back to search">
<INPUT TYPE="button" NAME="button" VALUE="Signup">
<P><A CLASS="BlackText">
If you have not established your own User Profile, click the <b>Signup</b> button above to proceed.
<INPUT TYPE="text" NAME="emailaddr" SIZE="45" MAXLENGTH="100">
<INPUT TYPE="button" NAME="button" VALUE="Login" onclick="validateForm(login);">
</FORM>

</body>
</html>


This will check that the e-mail address is actually an e-mail address before even submitting the information.

I hope this helps! =)

adios
08-15-2002, 05:50 PM
<INPUT TYPE="submit" NAME="button" VALUE="Login" onclick="return validateForm(this.form);">