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.
Page 4 of 9 FirstFirst ... 23456 ... LastLast
Results 46 to 60 of 124
  1. #46
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thats extremely difficult to understand old pedant lol , Any way i can make it alot simpler and smaller ? Baby steps man baby steps

  2. #47
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Johnny87auxsm View Post
    Baby steps man baby steps
    Good idea. Start with no code at all inside the form validation function and add the validation one small piece at a time. Test each piece you add to make sure that it and the previously added pieces all work properly before you add the next piece.

    If you had done that in the first place rather than trying to take many giant steps all at once then you wouldn't have a whole lot of code that you don't properly understand and which also doesn't work.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  3. #48
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I understand the document get elementby ID code's and how to check if checkboxes/radio buttons etc are checked dude just the date between 2001 and 2004 i find extremely difficult as the textbook has nothing close, so i have to google etc

  4. #49
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Assuming the following in the HTML:

    Code:
    <input type="text" size="30" id="date" name="date"/>
    then the JavaScript to validate that it contains a date AND is between 2001 and 2004 might look like this:

    Code:
    validateDate = function(y,m,d) {
     var dt;
     if (d != +d || m != +m || y != +y) return false;  // not a number
     dt = new Date(y, --m, d);
     if (d !== dt.getDate() || m !== dt.getMonth() || y !== dt.getFullYear()) return false; // month, day or year outside of the valid range
    
    if (y < 2001 || y > 2004) return false; // added line to check between 2001 and 2004
     return dt;
     }
    
    var dateStr = document.getElementById('date').value;
    var dateAry = dateStr.split('/');
    if (3 !== dateAry.length || !validateDate(dateAry[2],dateAry[1],dateAry[0])
        return false; // date is invalid or outside of range
    Using the Date object is the easiest way to check if a date is valid without having to specifically check how many days there are in the month. 29/2/2001 would return false because February only had 28 days in that year. 29/2/2000 would also return false because it is outside of your year range.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  5. #50
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    Assuming the following in the HTML:

    Code:
    <input type="text" size="30" id="date" name="date"/>
    then the JavaScript to validate that it contains a date AND is between 2001 and 2004 might look like this:

    Code:
    validateDate = function(y,m,d) {
     var dt;
     if (d != +d || m != +m || y != +y) return false;  // not a number
     dt = new Date(y, --m, d);
     if (d !== dt.getDate() || m !== dt.getMonth() || y !== dt.getFullYear()) return false; // month, day or year outside of the valid range
    
    if (y < 2001 || y > 2004) return false; // added line to check between 2001 and 2004
     return dt;
     }
    
    var dateStr = document.getElementById('date').value;
    var dateAry = dateStr.split('/');
    if (3 !== dateAry.length || !validateDate(dateAry[2],dateAry[1],dateAry[0])
        return false; // date is invalid or outside of range
    Using the Date object is the easiest way to check if a date is valid without having to specifically check how many days there are in the month. 29/2/2001 would return false because February only had 28 days in that year. 29/2/2000 would also return false because it is outside of your year range.
    I get the code but it can't be a function as i have one main function with validation inside which is being used to validate the whole form hence this tag
    Code:
    		<form method="post" action="http://tl28serv.uws.edu.au/twainfo/form.asp" id="formtag"  onsubmit="return validateSectionz(this)">

  6. #51
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Johnny87auxsm View Post
    I get the code but it can't be a function as i have one main function with validation inside which is being used to validate the whole form hence this tag
    Ij JavaScript as with most programming languagesd you can have as many functions as you like with function 1 calling function 2 calling function 3 ... calling function 57
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  7. #52
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Felgall mind showing me dude? This is my html tag
    Code:
    		<form method="post" action="lala.asp" id="formtag"  onsubmit="return validateSectionz(this)">

  8. #53
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nevermind sorted out how to use 2 functions, Tryed using this code but my whole javascript stops working, its broken somehow?

    Code:
    validateDob = function(y,m,d) {
     	var dt;
     	if (d != +d || m != +m || y != +y) return false;  // not a number
     	dt = new Date(y, --m, d);
     	if (d !== dt.getDate() || m !== dt.getMonth() || y !== dt.getFullYear()) return false; // month, day or year outside of the valid range
    
    	if (y < 2001 || y > 2004) return false; // added line to check between 2001 and 2004
    	 return dt;
    	 
    
    	var dateStr = document.getElementById('dob').value;
    	var dateAry = dateStr.split('/');
    	if (3 !== dateAry.length || !validateDate(dateAry[2],dateAry[1],dateAry[0])
        return false; // date is invalid or outside of range
    	
    	}

  9. #54
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Are you sure you are ready to tackle JavaScript?

    You don't seem to have a grasp of the principles of coding. To wit: Code is executed in a LINEAR fashion, except when you make a call to a function or method. And even then, when the function/method returns, you continue in a linear fashion.

    Your code in post #53 makes not one iota of sense.

    *AFTER* your validateDOB function has checked to see if the y,m,d that was passed to it is valid *THEN* you go get the "dob" element from the form.

    WHAT???? How can you validate something you haven't even gotten yet????

    The sad part of this is that I handed you the answer on a plate in post #45, written nearly as simply as I could, and you just ignored it as being too complex. And then you produce this mess which is more complex and which doesn't work, at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  10. #55
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Looking at your post #53, it becomes clearer and clearer that you don't really understand the code you are using, at all. You are grabbing at pieces of code you find all over the internet and just hoping you can shove them together and make them work. I call it the "mud on the wall" technique: You hope enough will stick to cover the wall.

    I'll hate myself in the morning. I think the net result of this is that you will get a (barely) passing grade on this homework but when it comes time to take the exam, without our help, you will be more lost than ever.
    Code:
    function validateDob( )
    {
    	var dateStr = document.getElementById('dob').value;
    	var dateAry = dateStr.split('/');
    	if (3 !== dateAry.length )
            { 
                return false; // date is invalid 
            }
     	var m = Number(dateAry[0]) - 1;
     	var d = Number(dateAry[1]);
     	var y = Number(dateAry[2]);
     	var dt = new Date(y, m, d);
     	if (d !== dt.getDate() || m !== dt.getMonth() || y !== dt.getFullYear()) 
            {
                return false; // month, day or year outside of the valid range
            }
            if  (y < 2001 || y > 2004)
            {
                return false; // added line to check between 2001 and 2004        
            }
    	return true;
    }
    By the way: That code was actually tested and it *DOES* work. So if it doesn't work for you, you are using it wrong.
    Last edited by Old Pedant; 05-06-2013 at 07:40 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  11. #56
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    old pedant i've been reading javascript everyday trying to learn but im having a hard time, they've thrown us in the deep end with this huge assignment due in 20 days about 80% of it is javascript so im screwed either way..

  12. #57
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    And your code i've copied it how it is mate and even using 2 functions on submit but yours doesn't detect ?

    This is how my onsubmit looks not sure if it's right though..
    Code:
     onsubmit="return ((validateSectionz() &  checkDob()) == 1)">

  13. #58
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Looking at your post #53, it becomes clearer and clearer that you don't really understand the code you are using, at all. You are grabbing at pieces of code you find all over the internet and just hoping you can shove them together and make them work. I call it the "mud on the wall" technique: You hope enough will stick to cover the wall.

    I'll hate myself in the morning. I think the net result of this is that you will get a (barely) passing grade on this homework but when it comes time to take the exam, without our help, you will be more lost than ever.
    Code:
    function validateDob( )
    {
    	var dateStr = document.getElementById('dob').value;
    	var dateAry = dateStr.split('/');
    	if (3 !== dateAry.length )
            { 
                return false; // date is invalid 
            }
     	var m = Number(dateAry[0]) - 1;
     	var d = Number(dateAry[1]);
     	var y = Number(dateAry[2]);
     	var dt = new Date(y, m, d);
     	if (d !== dt.getDate() || m !== dt.getMonth() || y !== dt.getFullYear()) 
            {
                return false; // month, day or year outside of the valid range
            }
            if  (y < 2001 || y > 2004)
            {
                return false; // added line to check between 2001 and 2004        
            }
    	return true;
    }
    By the way: That code was actually tested and it *DOES* work. So if it doesn't work for you, you are using it wrong.
    Tested 100% as is, does not work mate ??? Even the dob value matches my textbox from form but still not working ? Don't see how im using it wrong

  14. #59
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Working test page. And at this point I have to give up.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form action="http://www.google.com" onsubmit="return validateForm()">
       Enter the DOB: <input id="dob"/>
       <br/>
       <input type="submit" />
    </form>
    
    <script type="text/javascript">
    function validateDob( )
    {
    	var dateStr = document.getElementById('dob').value;
    	var dateAry = dateStr.split('/');
    	if (3 !== dateAry.length )
            { 
                return false; // date is invalid 
            }
     	var m = Number(dateAry[0]) - 1;
     	var d = Number(dateAry[1]);
     	var y = Number(dateAry[2]);
     	var dt = new Date(y, m, d);
     	if (d !== dt.getDate() || m !== dt.getMonth() || y !== dt.getFullYear()) 
            {
                return false; // month, day or year outside of the valid range
            }
            if  (y < 2001 || y > 2004)
            {
                return false; // added line to check between 2001 and 2004        
            }
    	return true;
    }
    
    function validateForm( )
    {
        if ( ! validateDob( ) ) 
        {
            alert("Invalid DOB");
            return false;
        }
        alert("Okay, form will submit");
        return true;
    }
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  15. #60
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Assignment states this ,
    "For testing purposes, the form could use http://tl28serv.uws.edu.au/twainfo/form.asp as the action URL. For the final version submitted for marking the page should submit to itself. Submission should take place only when client side validations are successful. Once submitted, the server side validations should be carried out prior to writing the content into the database."

    But when i change to action to action="<?php echo $_SERVER['PHP_SELF']; ?>" MY JS don't work ???


 
Page 4 of 9 FirstFirst ... 23456 ... LastLast

Posting Permissions

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