0

Hi all I am new to javascript & I have written up a script to validate a form. I have a few problems that i am stuck with 1. I am unsure how to validate my date also testing for leap years and months with 31 days . 2. once that date is entered making sure that the date is not in the past if it is to alert that the date is incorrect. 3. how to combine all alerts in the same alert box . Like I said I am new to javascript so a simple as possible explanation would be very much appreciated. Thank you in advance for your time :). below is my script.

( javascript to go with this form )

function validateForm() {
    // this part of the script will collate all errors into one should the user leave an input blank
    var Fname=document.forms["myForm"]["fname"].value;
    var Lname=document.forms["myForm"]["lname"].value;
    var address=document.forms["myForm"]["addr1"].value;
    var postcode=document.forms["myForm"]["pcode"].value;
    var email=document.forms["myForm"]["email"].value;
    var number=document.forms["myForm"]["tel"].value;
    var date=document.forms["myForm"]["mydate"].value;

    if (Fname == null || Fname == "" || Lname == null || Lname == ""
            || address == null || address == "" || !postcode || !email
            || !number || (myForm.sex[0].checked == false)
            && (myForm.sex[1].checked == false)
            || (myForm.age[0].checked == false)
            && (myForm.age[1].checked == false)
            && (myForm.age[2].checked == false)
            && (myForm.age[3].checked == false)
            && (myForm.age[4].checked == false)
            || !date) {
        alert("Please make sure all fields are filled or checked correctly out ");

        return false;
    }

    // end of collating script
    // start of postcode script
    var regPostcode = /^[a-zA-Z]{1,2}\d[\dA-Za-z]? \d[a-zA-Z]{2}$/;
    if (!postcode.match(regPostcode))
    {
        alert("That Post Code is incorrect, correct way mk4 4tr");

        return false;
    }

    // end of postcode script
    // start of email script
    var regEmail = /^\S+@\S+\.\S+$/;

    if (!email.match(regEmail))
    {
        alert("That email is incorrect");

        return false;
    }

    // end of email script
    // start of phone number script
    var phonestring = /^(?:0|\+44)[12378]\d{8,9}$/;
    if (!number.match(phonestring)) {
        alert(" correct format 01908234837");

        return false;
    }
    // end of phone script
    // start of gender script

    if ((myForm.sex[0].checked == false) && (myForm.sex[1].checked == false)) {
        alert("Please choose your Gender: Male or Female");

        return false;
    }

    // end of gender script
    // start of age group script
    if ((myForm.age[0].checked == false)
            && (myForm.age[1].checked == false)
            && (myForm.age[2].checked == false)
            && (myForm.age[3].checked == false)
            && (myForm.age[4].checked == false)) {
        alert("please select an age group");

        return false;
    }

    // end of age script
    // start of datefield
    var dateformat = /^(?:(?:31\/(?:0[13578]|1[02])|(?:29|30)\/(?:0[13-9]|1[012])|(?:0[1-9]|1\d|2[0-8])\/(?:0[1-9]|1[0-2]))\/[2-9]\d{3}|29\/02\/(?:[2-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[3579][26])00))$/;

    if (!date.match(dateformat)) {
        alert("format incorrect use dd/mm/yyyy make sure you are entering correct days to the month remember 30 days have september, april, june & november, only 28 days in february unless leap year next is 2016");

        return false;
    }

    // end of date field
    // start of terms and condition script
    if (myForm.tandc.checked == false) {
        alert("Please accept the terms and conditions");

        return false;
    } else {
        alert(" Thank you a member of our team will get back to you shortly");

        return true;
    }
}
Darkzaelus
  • 2,059
  • 1
  • 15
  • 31
adam
  • 19
  • 2
  • 9

4 Answers4

0

Perhaps you would have a look at a genuine library: Moment.js especially the function isBefore()
Otherwise:

Community
  • 1
  • 1
Marvin Emil Brach
  • 3,984
  • 1
  • 32
  • 62
0

1 - Javascript: how to validate dates in format MM-DD-YYYY?

2 - You can use library : http://momentjs.com/ or http://www.datejs.com/

3 - No choice you have to check all combination

Community
  • 1
  • 1
OlZ
  • 346
  • 1
  • 3
  • 11
0

If you dont like to include other .js you could also create a Date Object and validate it yourself.

var valDate = new Date();
var dateToCheck = new Date(yourdate);//should create a date from a DateString

if( dateToCheck < valDate ) {
  //some error here
}

You also might want to check this document out: http://www.w3schools.com/js/js_obj_date.asp

cptnk
  • 2,430
  • 18
  • 29
  • Hi thank you for your advice so in the brackets(yourdate) could i put my date regex or is there a way to split up my date regex to then put it in the correct format if so how do you do this?. – adam Jun 24 '13 at 08:18
  • The constructor accepts a string there are some possibilities like: new Date("2010-06-09")), new Date("2010-06")), new Date("21 June")) etc. you could replace some characters if not needed with: date= date.replace(/chars_you_want_to_replace/g,"chars_you_replace_it_with"); – cptnk Jun 24 '13 at 08:29
  • Hey thanks, i think because ive been stuck on this for so long ive just come to a complete mental block , would you be able to give me an example of this please , can i put in my regex as that works out dd/mm/yyyy. how do i split that as my regex is pretty long? and then i need to check to see if it is in the past , im sorry for being a pain. – adam Jun 24 '13 at 08:52
  • try to inspect your date variable with console.log(date). That way you can determine if you really need to change it. Also it would be helpfull if you could create a jsfiddle http://jsfiddle.net/ – cptnk Jun 24 '13 at 08:59
  • ok so i have altered my regex to suit the format of yyyy/mm/dd and then added this script will this work? – adam Jun 24 '13 at 09:15
  • this will create 2 Date objects once your string is converted to a DateTime object you can use comparision like '<,>,=' etc. If the inputed date from the string is in the past you can add some sort of error message and stop the sumbit. I really can not say it will work out of the box I just wanted to give you a Idea on how to work with the DateTime object. – cptnk Jun 24 '13 at 09:22
0

$split_array = ($('#MyText_input_1').val()).split(" ");//alert($split_array);

       /*** past date validation script ***/
        var c = $split_array[0]; // this is the format of the date coming in.
        var temp = new Array();
        temp = c.split('-');

        var x = new Date ( temp[1]+" "+temp[0]+", "+temp[2] );
        //alert((new Date().getTime()));
        if (x.getTime() < (new Date().getTime())) {
            alert('past date');
            return false;
        }
       /*** end script ***/
Amol Navsupe
  • 172
  • 1
  • 11