www.webdeveloper.com
Results 1 to 4 of 4

Thread: Name and Date Validation?

  1. #1
    Join Date
    Nov 2009
    Posts
    7

    Name and Date Validation?

    Hi, I was wonder if any JavaScript pros could point me in the right direction here.
    I don't know much about scripting yet but I'm trying to find 2 small validation programs, or figure out how to write them myself.

    For one, I need a text box where you would type your age, and have a submit and reset button. Then have JavaScript code that would basically validate whether or not you entered a number and have a pop up window stating whether or not you did.

    And for another, I need to validate whether or not a user typed in the date in the correct format.
    (meaning 12/8/2009 or 12-8-2009. it can have either of those 2 symbols.)
    And again, have a pop up window stating whether or not you did.

    I know how to make the buttons, I just know nothing about JavaScript. I'm not asking anyone to just write the program for me but if you could either explain a little bit about what a program like that consists of or if you know a good tutorial, I'd be forever grateful.

  2. #2
    Join Date
    Feb 2006
    Posts
    2,926
    Here is the tricky bit, figuring if the date string is a real date:
    Code:
    Date.fromMMDDYYYY= function(s){
        var A= s.split(/\D+/), er= 'bad date',
        y= +A[2], m= parseInt(A[0], 10), d= parseInt(A[1], 10);
    
        if(d> 31 || m> 12 || d<0 || m<0) throw er;
        switch(m){
            case 9: case 4: case 6: case 11: 
            if(d> 30) throw er;
            break;
            case 2: 
            if(d== 29){
                if(y&#37;4 || (!y%100 && y%400)) throw er;
            }
            else if(d> 28) throw er;
            break;
            default:
        }
        s= s.replace(/(\d)\-/g,'$1 ');
        d= Date.parse(s)
        if(!d) throw er;
        return new Date(d)
    }

    var s= "2-29-2008 12:05:00 PM";// good date
    s= "2/29/2008"; // good date
    alert(s+'\n'+ Date.fromMMDDYYYY(s));

    var s= "12-32-2009 12:05:00 PM";// bad date
    s= "12/32/2009";//bad date
    alert(s+'\n'+ Date.fromMMDDYYYY(s));
    Last edited by mrhoo; 12-08-2009 at 10:34 PM.

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by mvbf987 View Post
    And for another, I need to validate whether or not a user typed in the date in the correct format.
    (meaning 12/8/2009 or 12-8-2009. it can have either of those 2 symbols.)
    How could you know whether the user, when entering 12/8/2009, had in mind the 12th of August or the 8th of December? Both are valid dates, aren't they? Never relay on the user's good will (nor suppose the user is always an American, or always an European) and don't let him the possibility to make mistakes. Use 3 separate inputs (or select elements), one for the Date, one for the Month and one for the Year.

  4. #4
    Join Date
    Nov 2009
    Posts
    7
    Oh wow I didn't expect anyone to just give code for it. Thank you I'll give it a shot.
    And to Kor, you're right I just meant that it needed to have either the / or - symbols in the date for JavaScript to see it as valid. But that is a good point to remember.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles