SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validate that a string is valid year

    How to validate that a string is valid year, in format 1985

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey onesign

    You could use a regular expression like this...

    var sYearRegEx = /^[0-9]{4}$/;
    var sYear = '1234';
    alert(sYearRegEx.test(sYear));

    This will check for everything from the year 0000 to the year 9999. If you want something a little more specific you can just extend your regular expression.

    Hope that helps you along!

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    The expresssion
    Code:
    /^[1-2][0-9]{3}$/
    will get any year from 1000 to 2999, limiting the scope a little more .

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    First ask then think (me)
    it is simple min - max range check
    if(year < min || year > max)
    min 1900
    max 2003

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    You can do that too:
    Code:
    var year = parseInt(form.textboxName.value);
    if (year < 1900 || year > 2003) {
      alert('You naughty boy!');
    }

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you please tell me , what is wrong
    var re = new RegExp("[0-9-]{10,}");
    var result = re.test(phone);

    if(result != true)
    return false;
    else
    return true;

    It do not work right
    I just want any digit and - , in any order
    (213-546-879)

  7. #7
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I'm not seeing a problem. Your regex says 'ten and only ten items that will be: digits 0 through 9 and/or dashes', which will match on 800-555-12 and ----1-----.

    If you're trying to match american style phone numbers (area code-prefix-number)

    ([0-9]{3}-){2}[0-9]{4}

    may be more to your liking.

    (says: 2 groups of 3 digits 0 through 9 with a dash following each group, then a group of 4 digits 0 through 9)

    btw: there are 12 'places' in a american phone number (if you include the dashes)

    final note:
    instead of
    Code:
    var result = re.test(phone);
    
    if(result != true)
    return false;
    else
    return true;
    just say
    Code:
    return re.test(phone);
    less code == fewer places to make mistakes && less to look at when debugging ==

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd like to add something to Vinnie's code - when taking user input as an integer, always specify the radix, because you never know when somebody might type in a base-8 looking number.

    var year = parseInt(form.textboxName.value, 10);
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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