javascript to validate form

Discussion in 'Javascript' started by Mick Walker, Aug 29, 2007.

  1. Mick Walker

    Mick Walker Guest

    Hi,

    I have a problem that I have been trying to figure for a couple of days,
    and am wondering if anyone out there would be so kind as to give me a
    solution. (Deadline time)

    I am trying to validate a form. Its quite a simple form, but I am a wee
    bit stuck. Baically it consists of 9 text input fields and a select
    element. All elements on the form, have to have a value in them
    (manditory), BUT 2 items (txtEmail, and txtDOB) have to match a certian
    criteria.

    The first is obvious, it has to be a valid email address, the second
    however is a lot mroe difficult. The dob entered, not only have to be a
    valid date, it has to make the user over 18, otherwise the form wont submit.

    I am a ASP.Net programmer, but due to time resrictions we are updating
    an existing project using classic asp (which I have never used) were it
    asp.net I would simply handle this problem server side (lot of resources
    I know), but that isnt an option for me. Plus I could do with learning a
    little more than the bard Basics of Javascript.

    At the moment I am using 3 different function, one to validate the date,
    one to validate the email, and another to make sure all elements contain
    a value.

    These are like so:

    function validateDate(fld) {
    var RegExPattern =
    /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
    var errorMessage = 'Please enter valid date as month, day, and four
    digit year.\nYou may use a slash, hyphen or period to separate the
    values.\nThe date must be a real date. 30/2/2000 would not be
    accepted.\nFormat dd/mm/yyyy.';
    if ((fld.value.match(RegExPattern)) && (fld.value!='')) {

    } else {
    alert(errorMessage);
    fld.focus();
    return false;
    }
    }
    function validateWinStuffForm(f)
    {
    if(!valid_Required(f)){
    alert('The form was filled out incorrectly');
    return false;
    }
    }
    function valid_Email(t) {
    if(!r_validEmail.test(t)) {
    alert('Please enter a valid email address');
    return false;
    }
    return true;
    }

    I would like one function, that takes the form as an argument and does
    all of these checks. The names of the DOB and Email fields will always
    be the same on any form which the function is used (as the forms are
    generated server side).

    I hope someone can help

    Kind Regards
    Mick Walker
     
    Mick Walker, Aug 29, 2007
    #1
    1. Advertising

  2. In comp.lang.javascript message <>, Wed,
    29 Aug 2007 14:34:41, Mick Walker <> posted:

    >At the moment I am using 3 different function, one to validate the
    >date,


    You are attempting a remarkably complex RegExp-only method. See,
    without delay, <URL:http://www.merlyn.demon.co.uk/js-date4.htm>, for
    validating a string as a date and reading it. Then check the age by
    comparison of as many of Y M D as is necessary, after checking the exact
    applicable definition of 18.

    > one to validate the email,


    Don't get carried away : see, likewise,
    <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

    > and another to make sure all elements contain a value.


    Literally meaningless - all input controls always have a string value.
    You can check that the string is non-empty or non-visibly-blank or long
    enough or has only the right sort of characters or ...

    > var errorMessage = 'Please enter valid date as month, day, and four
    >digit year.\nYou may use a slash, hyphen or period to separate the
    >values.\nThe date must be a real date. 30/2/2000 would not be
    >accepted.\nFormat dd/mm/yyyy.';


    Confusing. Is the error that February rarely has a 30th or that 2 is a
    single character or both? OTOH, you might as well allow any non-digit
    separator, matching with /\s*(\d\d?)\D+(\d\d?)\D+(\d\d\d\d)\s*/ or
    similar.

    >I hope someone can help


    Your firm's best move is to hire a javascript programmer if javascript
    is to be programmed; also, seek a Thunderbird spelling-checker.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Aug 29, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Wheeler

    form wont validate on remote server

    Jack Wheeler, Jun 14, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    298
    George
    Jun 14, 2004
  2. =?Utf-8?B?UmFqYW5p?=

    Validate form

    =?Utf-8?B?UmFqYW5p?=, Nov 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    380
    Patrick.O.Ige
    Nov 13, 2004
  3. Colin Graham

    Javascript Validate Form Fields

    Colin Graham, Jan 25, 2005, in forum: Javascript
    Replies:
    1
    Views:
    114
  4. Replies:
    1
    Views:
    705
    Randell D.
    Mar 17, 2005
  5. Abhishek
    Replies:
    5
    Views:
    218
    David Mark
    Aug 7, 2008
Loading...

Share This Page