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
    Dec 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    on blur validation

    hi,
    In my program have 2 fields. and that fiels must not be null. when i write the code both field onBlur it goes in loop.

    code

    function firstonblur()
    {
    if(document.getElementById('a').value.length<=0)
    {
    alert("Field A must be enter");
    document.getElementById('a').focus();
    }
    }

    function secondonblur()
    {
    if(document.getElementById('b').value.length<=0)
    {
    alert("Field B must be enter");
    document.getElementById('b').focus();
    }
    }

    plz send the perfect solution for this problem.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Validating onblur is not the best option. Firstly it will put the user into a loop which you have discovered and 2nd it will force a console error in Firefox because of autocomplete. The preferred method is to validate in the end just before the form is submitted.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function doValidate()
    {
    	if(document.getElementById('a').value.length <= 0)
    	{
    		alert("Field A cannot be blank!");
    		document.getElementById('a').focus(); 
    		return false;
    	}
    	else if(document.getElementById('b').value.length <= 0)
    	{
    		alert("Field B cannot be blank!");
    		document.getElementById('b').focus();
    		return false; 
    	}
    }
    </script>
    </head>
    
    <body>
    <form action="#" method="post" onsubmit="return doValidate()">
    <label for="a">Input A: <input type="text" name="a" id="a"></label><br>
    <label for="b">Input B: <input type="text" name="b" id="b"></label><br>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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