Hello and welcome to our community! Is this your first visit?
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
    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


  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,700 Times in 2,676 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:-

    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>
    <script type = "text/javascript">
    function testEmail(which) {
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(which.value))) {  
    which.value = "";
    setTimeout("document.forms[0].email.focus()", 25);
    return false;
    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 08: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