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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript code for validation

    Hello Everyone

    I am new on java script and I want to check the form blank text fields by using onChange event.

    Please guide me, is it possible or not....If possible then please send me the code.

    MY EMAILID IS dhiman.anurag1@gmail.com


    ........
    THANK YOU

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Form validation of the pattern if (document.forms[0].elements[1].value == "") (that is, checking simply for a blank field) is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    For example, this is how you would validate an email address:-

    Code:
    <form>
    
    Email address <input type = "text" name = "email" size = "45" onblur = "testEmail(this)">
    <span id = "message"  style="display:none; color:red;"> The email address is invalid - please retype it</span>
    
    
    </form>
    
    <script type = "text/javascript">
    function testEmail(which) {
    document.getElementById("message").style.display="none";
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(which.value))) {  
    document.getElementById("message").style.display="inline";
    which.value = "";
    setTimeout("document.forms[0].email.focus()", 25);
    return false;
    }
    }
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 03-05-2012 at 07:09 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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