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 3 of 3
  1. #1
    By@
    By@ is offline
    New Coder
    Join Date
    Sep 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variables in paths

    Hey.

    Im kinda new to JavaScripting but I cant seem to find the answer to this anywer.

    I have a function which checks to see if a date is in the dd/mm/yyyy format in a certain field. If it returns false, it alerts the user and is supposed to focus back to that field.

    Code:
    function DateValid(myDate, fieldName) {
        if (testDate(myDate) == false){
            alert(reason);
            document.form1.fieldName.select();
        }
    }
    Except that the fieldName variable, which is sent from the field via onBlur(), seems to stop it working... I think.

    Any ideas?
    Last edited by By@; 09-17-2005 at 07:06 AM.

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Welcome to codingforums!

    Do you require something like this mate?
    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 checkDate(field) {
    	if(field.value == field.defaultValue) {
    		return false;
    	}
    	
    	if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(field.value)) {
    		alert("invalid date supplied\nDate must be DD/MM/YYYY");
    		field.select();
    		field.focus();
    		return false;
    	} else {
    		return true;
    	}
    }
    </script>
    </head>
    
    <body>
    <form name="frm">
    Enter date then click anywhere else to blur() the text box:
    <br><input type="text" name="txtDate" onblur="checkDate(this);">
    </form>
    </body>
    </html>
    Hope this helps.

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    By@
    By@ is offline
    New Coder
    Join Date
    Sep 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I already have all the date validation stuff set up, I just need to make it select the field if it returns false. But because I have numerous fields using the same function, I have to have the name of the field as a variable.

    But if I make the path:

    Code:
    variable = 'DateCompleted';
    
    document.form1.variable.select();
    It says that the field doesnt exist or is null. Because its looking for a field called 'variable' instead of one called 'DateCompleted'

    If I make sense...



    And thanks for the welcome Mn
    Last edited by By@; 09-18-2005 at 02:10 AM.


  •  

    Posting Permissions

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