Jump to content

Recommended Posts

Hi there!

 

I am using: a form in HTML, PHP and then I was trying to use Javascript to validate the email address before it was sent to the next PHP page.

 

The problem is... that even if the email is invalid and the javascript function displays an error message, the page >still< proceeds to the next PHP page (as if everything was fine).

 

However, I >want< it to stay on the same page and allow the user to enter in their email address again (correctly). And not advance to the next PHP page >until< the email address has been successfully validated.

 

Below is my HTML, PHP and Javascript. 

Could anyone kindly tell me what I'm doing wrong? I'd really appreciate it!

Thank you very much!!

Andrea   :)

================

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Ballard Social</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
 
<BODY BGCOLOR="#ddeae7">
 
      <!---------------------- Sign Up on Email List ---------------------->
 
<P> </P><P> </P><P> </P>
 
<table BORDER="1" ALIGN="CENTER" cellpadding="10" CELLSPACING="0" BORDERCOLOR="#bfc2c1" BGCOLOR="white">
  <tr><td WIDTH="355">
<FORM onsubmit='return pValidateForm()' ACTION="pAddEmail_BallardSocial_1_validate.php" METHOD="POST" name='myForm' id='myForm'  >
                            <P ALIGN="center">
<STRONG><font color="#4c4f4e" face="arial,verdana" size=2>Sign Up on the Email List</STRONG><BR>
          <font size=1>You will be notified of the next Ballard Social event</font><BR>
                
                              <INPUT TYPE="Hidden" NAME="SourcePage" VALUE="SignUp.htm">
                              <INPUT NAME="Email" TYPE="text" size=28 placeholder="enter your email address">                              
                              <INPUT NAME="OK2" TYPE="submit" value="OK"></P>
</FORM>
</td></tr></table>
 
 
</BODY>
</HTML>
 
<script language='javascript'>
 
function pValidateForm()
{
var x=document.forms['myForm']['Email'].value;
 
 
var atpos=x.indexOf('@');
var dotpos=x.lastIndexOf('.');
 
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
 {
 alert('Not a valid e-mail address');
 return false;
 }
}
 
 
</script>
 
Link to comment
https://forums.phpfreaks.com/topic/282790-email-validation-in-a-form/
Share on other sites

Well, the first thing I thought was that you were not doing a "return false" to prevent the form from submitting. But, it seems you are. So the second thing to look for is some JavaScript erroring out. But, I don't see any obvious problems there. So, I went ahead and copied/pasted the code (as is) into a document and it worked perfectly. Page does not submit if there is an error in the email logic. I'm using FF, so the problem could be browser specific. You have the JavaScript code block after the HTML closing tag. I don't know if that is even valid markup (I'm thinking not). Try moving that code block within the HTML tags, preferably within the HEAD tags.

 

EDIT: I just took a closer look at that code and - where are you learning to code? FONT tags, really? Those have been deprecated for over a decade. Also, your process for validating the email is pretty worthless since it only validates the content has an "@" and a "." and that the period comes after.

Edited by Psycho

OK, this may not fix your problem, but at least you would be starting from a much cleaner bit of code. As before, this does work for me - both in FF & IE. If there is an error the page does not submit.

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Ballard Social</title>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<style>
body {
    background-color: #ddeae7;
}

#signUpFormTable{
    border: 1px solid #bfc2c1;
    padding: 10px;
    margin: auto;
    margin-top: 100px;
    background: #ffffff;
    width: 355px;
    text-align: center;
}

#formTitle{
    color: #4c4f4e;
    font: bold 16px arial, verdana, serif;
}

#formText{
    font-size: 12px;
}

</style>
<script type='text/javascript'>

function validEmail(emailStr)
{
    //Return true/false for valid/invalid email
    formatTest = /^[\w!#$%&\'*+\-\/=?^`{|}~]+(\.[\w!#$%&\'*+\-\/=?^`{|}~]+)*@[a-z\d]([a-z\d-]{0,62}[a-z\d])?(\.[a-z\d]([a-z\d-]{0,62}[a-z\d])?)*\.[a-z]{2,6}$/i
    lengthTest = /^(.{1,64})@(.{4,255})$/
    return (formatTest.test(emailStr) && lengthTest.test(emailStr));
}

function pValidateForm()
{
    var emailVal = document.getElementById('email').value;
    if(!validEmail(emailVal))
    {
        alert('Not a valid e-mail address');
        return false;
    }
    return true;
}
 
</script>
</head>
 
<body>

<div id="signUpFormTable">
    <form onsubmit='return pValidateForm()' action="pAddEmail_BallardSocial_1_validate.php" method="post" name='myForm' id='myForm'>
        <span id="formTitle">Sign Up on the Email List</span><br>
        <span id="formText">You will be notified of the next Ballard Social event</span><br>
        <input type="Hidden" name="SourcePage" value="SignUp.htm">
        <input name="Email" id="email" type="text" size="28" placeholder="enter your email address">                              
        <input name="OK2" type="submit" value="OK">
    </form>
</div>

</body>
</HTML>

Thank you for the "cleaner" code. I will try it! Thanks for taking the time to write it out for me!

 

To the main issue, it still doesn't work.

 

It wants to move to the next page >even< when it returns a "false" value. The error message will appear but as when the user clicks "ok," it automatically takes them to the next page (pAddEmail_BallardSocial_1_validate.php). What it >should< do is stay there and let them re-enter their email address with a valid format. Then test it and if it's valid, then take them to the next page.

 

Hope that makes sense!  :)

Andrea

Yes, it makes sense. And, as I stated, I tested the code in both IE and Chrome and found it to be working. So, my guess is the problem is due to the browser you are using or you are implementing other code along with this that is not provided.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.