Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 588529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:19:40+00:00 2026-05-13T15:19:40+00:00

I am trying to create a validate-email javascript and get it working with forms

  • 0

I am trying to create a validate-email javascript and get it working with forms and PHP. Of coures, some problems…

  1. As you can see in my form, I did define “post” as the method. But I can only retreive the data as if it was a get method. It was working before I started to add the e-mail verification script and adopt the code to it.

  2. If the e-mail is incorrect, I do return false. Isn’t the point that the request to the test.php (defined in action) should not be executed? As it is now, the page is accessed even if I return false.

  3. Depending on the answers to the questions above, do I need to submit the form from the Javascript if the e-mail is verified ok?

javascript:

function isValidEmail() {
    regExp = /^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/;
 if(document.subscribe.email1.value.search(regExp) == -1){
          alert(document.subscribe.email1.value + " Incorrect email address");
    return false; 
    } 

//document.subscribeForm.submit();

return true; 
}

php:

<?php

echo $_POST['email1'];

$mysqli = mysqli_connect("localhost", "root", "", "test", "3306");

$result = mysqli_query($mysqli, "SELECT email, id, subscriber, subscribed_date FROM `user` u;");

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
foreach($row as $key => $value){

   echo "$key = $value<BR/>";
}
}

mysqli_free_result($result);
mysqli_close($mysqli);

?>

html:

<div id="subscribe">
  <form action="test.php" name="subscribe" method=post">
    <p id="formlabel">E-mail</p> <input type="text" name="email1">
    <br>
    <p id="formlabel">Repeat e-mail</p> <input type="text" name="email2"> <br/>
    <input type="submit" value="Subscribe" onclick="isValidEmail()">
  </form> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:19:41+00:00Added an answer on May 13, 2026 at 3:19 pm
    <input type="submit" value="Subscribe" onclick="isValidEmail()">
    

    This executes isValidEmail() and then throws away the result. The onclick itself returns undefined and the submission is not prevented.

    You can say onclick="return isValidEmail()". However:

    1. Put validation/submission stuff on form onsubmit, not input click, to ensure it is always called for all types of form submission.

    2. Better to avoid inline event handlers.

    3. You missed a " in your form’s method attribute, which is presumably why it was defaulting back to get.

    so:

    <form id="subscribe" method="post" action="test.php">
        ...
    </form>
    
    <script type="text/javascript>
        document.getElementById('subscribe').onsubmit= function() {
            if (!this.elements.email1.value.match(/^[^@]+@[^@]+$/) {
                alert('Please enter an e-mail address');
                return false;
            }
            if (this.elements.email1.value!=this.elements.email2.value) {
                alert('E-mail addresses do not match');
                return false;
            }
            return true;
        } 
    </script>
    

    I replaced the regexp with a trivial one, because the expression you’re currently using is bogus and will deny many valid e-mail addresses. Turning customers away because their e-mail address doesn’t fit your conception of what an e-mail address is sucks.

    ‘Validating’ e-mail addresses correctly with regex is absurdly difficult. Better to include only a trivial check for obviously-malformed strings like the above. If you need to really check the e-mail address, you will have to actually try to send a mail to it, or at least try to lookup the domain name part of the address for an MXer.

    See this question for discussion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a registration form and validate input fields using javascript, but
I am trying to validate some form fields but it always errors out on
I'm trying to create a simple form that will validate that the first password
I am trying to create a regular expression to validate file names for further
I am trying to create a custom RegularExpressionValidator to validate the current expression number(0-99).number(100-1000)\number(0-99).number(100-1000)
I'm trying to create an xsd schema that will validate a map with an
I am trying to create an XSD schema which will validate the following xml.
I am trying to using a jquery.validate.unotrusive.js plugin to dynamically created form fields like:
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying to create a regex validation attribute in asp.net mvc to validate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.