Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Form validation error message not showing up

    Im using javascript form validation to show an error message when the first name text field is empty, but im not having any luck. I've followed tutorials using code that works fine for them. Any help would be great..

    Below is the javascript:

    <script type="text/javascript">
    function checkforblank() {

    if (document.getElementById("fname").value = "") {
    alert('enter your first name');
    document.getElementById('fname').style.borderColor="red";
    return false;
    }

    }</script>

    This is part of the form:

    <form method="post" action="#" onsubmit="return checkforblank()">

    <label>First name:<br>
    <input type="text" id="fname" name="fname"></label><br>

    <input type="submit" value="Submit">
    <input type="reset" value="Clear">

    </form>

  • #2
    Regular Coder
    Join Date
    Aug 2012
    Posts
    116
    Thanks
    0
    Thanked 25 Times in 25 Posts
    Add a second '=' to your if statement:

    Code:
    function checkforblank() {
    
    if (document.getElementById("fname").value == "") {
    alert('enter your first name');
    document.getElementById('fname').style.borderColor="red";
    return false;
    }
    
    }
    1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for?

  • Users who have thanked Brandnew for this post:

    Error Unknown (06-11-2013)

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank You! it works fine now haha


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •