0

大家好,我是 javascript 新手,我已经编写了一个脚本来验证表单。我遇到了一些问题 1. 我不确定如何验证我的日期并测试闰年和 31 天的月份。2. 输入该日期后,如果要提醒日期不正确,请确保该日期不是过去的。3.如何将所有警报组合在同一个警报框中。就像我说的那样,我是 javascript 的新手,所以非常感谢尽可能简单的解释。提前感谢您的时间:)。下面是我的脚本。

(使用此表单的 javascript )

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;
    }
}
4

4 回答 4

0

也许你会看看一个真正的库:Moment.js,尤其是函数isBefore()
否则:

于 2013-06-24T07:32:49.643 回答
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 ***/
于 2013-10-26T06:12:43.703 回答
0

1 - Javascript:如何验证格式为 MM-DD-YYYY 的日期?

2 - 您可以使用库: http: //momentjs.com/http://www.datejs.com/

3 - 没有选择,您必须检查所有组合

于 2013-06-24T07:42:16.700 回答
0

如果您不喜欢包含其他 .js,您还可以创建一个 Date 对象并自己验证它。

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

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

您可能还想查看此文档:http ://www.w3schools.com/js/js_obj_date.asp

于 2013-06-24T07:49:35.853 回答