#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Refresh page if user entered the wrong validation


    So I have this form.html file with JavaScript validation that will validate if the email that user entered is valid or not, if it is, it will be processed by a php file to enter this email to a MySQL database

    Here's the javascrip code:
    Code:
    <script type='text/javascript'>
    function ValidateEmail(inputText)
    {
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    
    if(inputText.value.match(mailformat))
    {
    document.form1.email.focus();
    return true;
    }
    else
    {
    alert("invalid email");
    document.form1.email.focus();
    return false;
    }
    }
    <script>
    And here is the form tag in my HTML page
    Code:
    <form name="form1" action="email.php" method="POST">
    <input name="email" type='text' id='email'>
    <input type="submit" value="Submit" onclick="ValidateEmail(document.form1.email)"/>
    </form>
    However, when I entered an invalid email, the error message displays but then it also saves the invalid email into my MySQL database, is there a way to only save the validate email address into the database. So when user enters invalid email, the error message will come up and then will refresh the textbox to allow user to enter again
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    Client-side validation should never be used as a replacement for hardened server-side validation. You should be employing checks on the server-side, where a user cannot bypass or alter your validation rules.

    Your Javascript event call is not waiting for the function to return a confirmation before submitting the document, this is because it has not been asked to. Also, you should not be running the check when the button is clicked but when the form is submitted.
    HTML4Strict Code:
    <form name="form1" action="email.php" method="post" onsubmit="return ValidateEmail(document.form1.email);">
    	<input name="email" type='text' id='email'>
    	<input type="submit" value="Submit">
    </form>
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.

IMN logo majestic logo threadwatch logo seochat tools logo