2013-06-24 119 views
1

大家好我已經做了一個腳本來驗證表單,我現在需要做的是檢查日期不是過去。我使用了一套substr,但它似乎並沒有工作,請有人可以幫忙。下面是我的代碼日期部分是朝向底部。如果輸入日期是過去的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 =/^\[email protected]\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(" incorrect,correct format 01908234874"); 
    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; 
} 
var today = new Date(); 

var courseYear =string.substr(6,4) // use substr or substring to capture the last four digits 
var courseMonth =string.substr(3,2) // use substr or substring to capture the four and fifth digits 
var courseDay = string.substr(0,2)// use substr or substring to capture the first and second digits 

var dateToCompare = new Date(courseYear, courseMonth, courseDay); 

if (dateToCompare < today) { 
alert("this date is in the past"); 
return false; } 

//end of date field 
else 
{ alert(" Thank you a member of our team will get back to you shortly"); 

return true;} 
} 
+1

爲什麼不使用jqueryui date-plugin? –

+0

你也可以看一下moment.js:http://momentjs.com/ – fero

+0

@Jigar:JS標籤描述狀態_「如果使用了一個庫或框架,然後用適當的標籤標記問題」_,沒有jQ標籤,所以這可能是爲什麼OP不使用插件的原因 –

回答

2

這看起來不正確:

var courseYear = string.substr(6,4) // use substr or substring to capture the last four digits 
var courseMonth = string.substr(3,2) // use substr or substring to capture the four and fifth digits 
var courseDay = string.substr(0,2)// 

它應該是:

var courseYear =date.substr(6,4); 
var courseMonth =date.substr(3,2); 
var courseDay = date.substr(0,2); 

否則它是好的:http://jsfiddle.net/balintbako/mKmaE/

你仍然可能要考慮使用用於表單驗證的jquery插件。

+0

哦,是的,我看到我在那裏做錯了,謝謝你,我會給你一個去應該我的變種today = new Date date =「24/06/2014? – adam

+0

或'var parts = date.split (「/」); courseYear = parts [2]; courseMonth = parseInt(parts [1],10); courseDay = parseInt(parts [0],10);' – mplungjan

+0

'不帶參數的new Date()當前日期 – xmashallax

0

不確定正則表達式是驗證日期的最簡單方法。考慮這裏的答案:Test if date is valid

一旦你轉換的字符串轉換成日期對象(2行代碼),您可以測試是否用在了過去:

if (d < new Date()) { 
    // date is in the past 
} 

我不認爲有在堅持填充的值的任何值日期和月份,當然是d/m/yyyy就足夠了?

0
 $split_array = ($('#MyText_input_1').val());//alert($split_array); 
     //ex. 31-Oct-2013 
     /*** 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 ***/