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

Thread: Date format

  1. #1
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts

    Date format

    Hi

    I am creating a site for a friend to manage his tax returns. The thing I need help with is a validator function thats checks the format of an input field. The input fields are simple textboxes. They are for recording dates but entry must be in the format of dd-mm-yy e.g. 21-01-07

    I wanted a script to be triggered for onchange event. This is where it will be validated. I hoped to use a regex to test it or something (if possible in js) then if validation fails the box with be cleared and an alert message will be displayed. I am not very experienced with js and would appreciate some help.
    You can not say you know how to do something, until you can teach it to someone else.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    This is how far i have got
    Code:
    function validate(obj){
    		var el = document.getElementById(obj);
    		if(!(/^\d{2}-\d{2}-\d{2}/.test(el.value)))
    		{
    			el.value='';
    			el.focus();
    			alert('Date format must be dd-mm-yy e.g. 21-02-07');
    		}
    	}
    Problem is it only checks if two digits are present if I can prevent users from adding something like 99-99-99 that would be great
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Any ideas?
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    function validate(obj){
    var el = document.getElementById(obj);
    var failFlag=0;
    if(!(/^\d{2}-\d{2}-\d{2}/.test(el.value))) {
    failFlag=1; // wrong format
    }
    var ss=el.split("-");
    if (ss[0] <1 || ss[0] >31) {failFlag=1} // day
    if (ss[1] <1 || ss[1] >12) {failFlag=1} // month
    if (ss[2] <0 || ss[2] <15) {failFlag=1} //max year 2015
    if (failFlag==1) {
    el.value='';
    el.focus();
    alert('Date format must be dd-mm-yy e.g. 21-02-07');
    }
    }

    This still does not check for invalid dates such as 30th February.

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Thank you so much you're a wonderful person
    You can not say you know how to do something, until you can teach it to someone else.

  • #6
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    This script isnt working I am getting an error with the split function

    el.split is not a function

    Any idea why?
    You can not say you know how to do something, until you can teach it to someone else.

  • #7
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Add .value after the el:
    Code:
    var ss=el.value.split("-");
    Edit:
    One more thing, replace this:
    Code:
    if (ss[2] <0 || ss[2] <15) {failFlag=1} //max year 2015
    With this:
    Code:
    if (ss[2] <0 || ss[2] >15) {failFlag=1} //max year 2015
    Last edited by BarrMan; 02-22-2007 at 03:03 PM.


  •  

    Posting Permissions

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