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

    onBlur problem ?

    I have two textboxes, each of them is controlled with a function called checknumber. It controlles if they are empty or a valid number and I connected this function to onBlur event of these textboxes. The problem is that when I wrote an invalid number to the first and click the second one, It gives continuously error messages. How can I solve 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
    And we are supposed to know what your code looks like how?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts
    I know the answer to that one, that's easy! ...just stop writing invalid numbers in the boxes. hehee

    Do you have a check to ensure the entered number is actually a number and is within a set range and/or is a valid number contained within your variable specs?
    Last edited by RexxCrow; 08-11-2006 at 09:13 AM.

  • #4
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I checked my code and controls carefully. It is like that basically;

    function controlCode(){

    if( !checknumber(document....("text1).value) )
    alert("Wrong number!")

    }

    And checknumber function is the function given at the link:

    http://www.javascriptkit.com/script/script2/numc.shtml

    I only added one more textbox and instead of controlling from submit button, I added code this "controlCode()" function to the onBlur() event.I didn't add any more code except these two functions.
    User will write a valid number and make tab and write another valid number to the second textbox and make another tab and enter submit button.
    When I wrote an invalid number to the first box and tab to the second box, an alert window appears and how many time you close it, it continues appearing. How can I solve this problem ?

  • #5
    Regular Coder RexxCrow's Avatar
    Join Date
    Jul 2006
    Location
    California
    Posts
    275
    Thanks
    6
    Thanked 2 Times in 2 Posts
    That sounds like you have the alert inside a check/validate loop?


    This recent topic is similar and should help you with your format:

    http://codingforums.com/showthread.php?t=93250

  • #6
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Select textbox1 and write an invalid date and then select textbox2, you will see what I am trying to explain.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>

    <script type="text/javascript">

    function checkdate(input){
    var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
    var returnval=false
    if (!validformat.test(input.value))
    alert("Wrong date format!")
    else{ //Detailed check for valid date ranges
    var dayfield=input.value.split("/")[0]
    var monthfield=input.value.split("/")[1]
    var yearfield=input.value.split("/")[2]
    var dayobj = new Date(yearfield, monthfield-1, dayfield)
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
    alert("Invalid date!")
    else
    returnval=true
    }
    if (returnval==false)
    input.focus()
    return returnval
    }

    </script>

    </head>

    <body>
    <form id="form1" name="form1" method="post" action="">
    <p>&nbsp;</p>
    <p>
    <label>Text1
    <input type="text" name="textfield" onblur="return checkdate(this)" />
    </label>
    </p>
    <p>
    <label>Text2
    <input type="text" name="textfield2" onblur="return checkdate(this)" />
    </label>
    </p>
    <p>
    <input type="submit" name="Submit" value="Submit" />
    </p>
    <p>&nbsp;</p>
    </form>
    </body>
    </html>

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I would have used a validation based on onsubmit event, not onblur. The way you wrote the code, you enter in a loop, as you need to press OK on that alert, but doing that, you do start again an onblur event, which will produce an alert, and so on
    Try this variant:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <script type="text/javascript">
    /* <![CDATA[ */
    function checkdate(f){
    var names=['textfield','textfield2'];
    var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
    for(var i=0;i<names.length;i++){
    	if (!validformat.test(f[names[i]].value)){
    	alert('Wrong date format!');
    	f[names[i]].focus();
    	return false;
    	}
    //Detailed check for valid date ranges
    var dayfield=f[names[i]].value.split("/")[0]
    var monthfield=f[names[i]].value.split("/")[1]
    var yearfield=f[names[i]].value.split("/")[2]
    var dayobj = new Date(yearfield, monthfield-1, dayfield)
    	if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)){
    	alert('Invalid date!');
    	f[names[i]].focus();
    	return false;
    	}
    }
    }
    /* ]]> */
    </script>
    
    </head>
    
    <body>
    <form id="form1" method="post" action="" onsubmit="return checkdate(this)">
    <p>&nbsp;</p>
    <p>
    <label>Text1
    <input type="text" name="textfield" />
    </label>
    </p>
    <p>
    <label>Text2
    <input type="text" name="textfield2" />
    </label>
    </p>
    <p>
    <input type="submit" name="sub" value="Submit" />
    </p>
    <p>&nbsp;</p>
    </form>
    </body>
    </html>
    Last edited by Kor; 08-11-2006 at 03:31 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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