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

    Simple Return To Element Needed

    I'm stumped and can use a pointer in the right direction. Here is some stripped down code:

    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    function CheckDate(Info) {
    // When the user enters an invalid date, they need to return to whichever Date field they came from
    return false;
    }
    </script>
    </head>
    <body>
    <form name="form1">
    Date 1 <input type="text" id="Date1" name="Date1" onblur="return CheckDate(this);" /><br />
    Date 2 <input type="text" id="Date2" name="Date2" onblur="return CheckDate(this);" /><br />
    Place <input type="text" id="Place" />
    </form>
    </body>
    </html>


    In this situation I have two Date elements. I have a routine called CheckDate that looks at the entry and determines whether or not the user has entered a valid date.

    If they have not entered a valid date then they need to return to the field they were entering. In this non-working code they simply go to the next field. I have tried several things, like adding a focus, but nothing appears to return the user to the field where they entered the invalid information. If I only had a single date field then placing the focus on the specific element would be no problem, but I have not been able to figure out how to have the function know which of the two elements to send the user back to. The form I am working on has seven Date fields, so obviously I don't want to write seven different Date Check functions. <g>

    I'm sure that this can be done, but I'm just missing something very elemental.

    Cheers -

    george

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    It's a weird way that you've done it. I don't know about returning false on an onblur, I've never done form validation like that. Clearly it's not working the way you expected it to (and, err )

    Do you need the function to actually return a value at all? Can you not just put a focus() in there? I know you said that but what code, exactly, did you use?

    Code:
    function CheckDate(objField) {
    objField.focus();
    }
    onblur="CheckDate(this)" should send them back to the right element. If it's not... umm

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    You have a reference to the element, so to restore its focus you can use:
    Code:
    function CheckDate(Info)
    {
     /*validation*/
     if( date fails )
      Info.focus();
    }
    Unfortunately, restoring focus leads to endless loops, because if the onblur is triggered by focusing another validated field, its own onblur event will be triggered as it loses the focus to the previous field.
    Perform validation on submission.

  • #4
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I was hoping that there would be some way of doing this, but I guess doing the validation on the way out would work also, so I'll go that route.

    Thanks for the help.

    Cheers -

    george

  • #5
    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
    Quote Originally Posted by GLSmyth View Post
    Okay, I was hoping that there would be some way of doing this, but I guess doing the validation on the way out would work also, so I'll go that route.

    Thanks for the help.

    Cheers -

    george
    I have had already explained you in another place about the endless loop. And here, as there, my mind is the same... use onsubmit with a return condition to validate this type of fields.
    Last edited by Kor; 10-16-2006 at 08:54 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
    •