jquery - Date coming as undefined with javascript -



jquery - Date coming as undefined with javascript -

function duplicatewithlines() { var datefield = jquery("#lineitemdate").val(); if ( !( datefield && datefield.match( /^\d{1,2}[/]\d{1,2}[/]\d{4}$/ ) ) ) alert("date required"); else { //do blah blah } } --- --- <input type='button' class='duplicate_button' name='duplicatewithlines' value='duplicate line items:' onclick='duplicatewithlines()'/> <hsi:calendar name="lineitemdate" size='10' maxlength='10' />

at line var datefield = jquery("#lineitemdate").val(); value coming undefined think. because of failing come in else block.if status satisfying alert msg printed. has come in else block. , there date format entered dd/mm/yyyy. way in else block.

addressing val()-problem: trying find element id lineitemdate. element has no id. have name, $('[name=lineitemdate]').val() should work.

if formatting fixed (dd/mm/yyyy), seek modifiyng if part way avoid ugly , expensive matching:

var datefromvalue = new date( $('[name=lineitemdate]').val().split('/').reverse().join('-') ); if (!isnnan(datefromvalue)) { /* it's date! */ } else { /* don't bother */ }

while we're @ it, don't utilize inline handlers. every activation of inline handler spawns new javascript interpreter. in scripting use:

$('[name=duplicatewithlines]').on('click', duplicatewithlines);

it may wise rename function, avoid name clashes.

javascript jquery date

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -