www.webdeveloper.com
Results 1 to 2 of 2

Thread: Error in date-validation script..?

  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Error in date-validation script..?

    Can't get this thing to work the way I want.
    I have two scripts. One of them validates the fields in a form, one by one(this is not included here, it works). In the end it checks the date fields. It does this by calling another script.

    Writing something in the datefield is not mandatory.
    The error happends when the user have written something in the datefield "nesteservice". Independent of what is written, I get the message "Wrong format of the date. Correct format is dd-mm-yyyy." from the date validation script. Then, when the user klicks ok on this, he submits the form, even if the date format and range is correct or not.

    to sum up:
    Error 1: Error message "Wrong format of the date...." Even if its wrong or not.
    Error 2: One submits the form even if the alert has popped up..and independently of the written date format is correct or not.


    Code:
    <!-- START Validernynypumpe FORM-SJEKK --> 
    function Validernynypumpe(){ 
    	 var nesteservice=document.nypumpe.nesteservice 
    	 if (nesteservice.value==""){ //If no date is written. 
    		  var svar = confirm("Skal ikke pumpen ha flere servicer? Trykk 'OK' for  g videre uten  registrere neste servicedato, trykk 'Avbryt' for  avbryte.") 
    		  if (!svar) { 
    			   nesteservice.focus() 
    			   return false 
    		  } 
    	 } else { //If a date is given.. 
    		  if (checkdate(nesteservice.value)==false){ //checking date validity.. 
    			   nesteservice.focus() 
    			   return false 
    		  } 
    	 } 
    	 return true 
    } 
    
    <!--SLUTT nypumpe FORM-SJEKK--> 
    
    
    <!--START DATO-SJEKK--> 
    
    /**-------------------------- 
    //* Validate Date Field script- By JavaScriptKit.com 
    //* For this script and 100s more, visit http://www.javascriptkit.com 
    //* This notice must stay intact for usage 
    ---------------------------**/ 
    
    function checkdate(input){ 
    	 var validformat=/^\d{2}\-\d{2}\-\d{4}$/ //Basic check for format validity 
    	 var returnval=false 
    	 if (!validformat.test(input.value)) 
    	 alert("Wrong format of the date. Correct format is dd-mm-yyyy.") 
    	 else{ //Detailed check for valid date ranges 
    	 var dayfield=input.value.split("-")[0] 
    	 var monthfield=input.value.split("-")[1] 
    	 var yearfield=input.value.split("-")[2] 
    	 var dayobj = new Date(yearfield, monthfield-1, dayfield) 
    	 if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) 
    	 alert("Invalid date! Choose another date!") 
    	 else 
    	 returnval=true 
    	 } 
    	 if (returnval==false) input.select() 
    	 return returnval 
    } 
    
    <!--SLUTT DATO-SJEKK-->

  2. #2
    Join Date
    Nov 2009
    Posts
    2
    Found the error!

    if (checkdate(nesteservice.value)==false)
    must be
    if (checkdate(nesteservice)==false)
    without the ".value".

    Doesn't sound logic to me...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles