/    Sign up×
Community /Pin to ProfileBookmark

Date Validation Not Working

I have a field called “txtWorkOfDate.” It is a field that can be modified, but upon page load it is already populated with a date from a SQL table (as indicated below…RS(“WorkOfDate”). However, JS doesn’t seem to like the value in that text box because when I try to submit the page, I get an alert to provide a valid date. (Maybe JS thinks it’s blank for some reason?) Can anyone explain why this might be? Maybe this is not the way to do date validation when a field is already populated upon page load? Or, something else I need to account for?

Thanks.

[code=html]

<input type=”text” name=”txtWorkOfDate” id=”txtWorkOfDate” value=”<%=(RS(“WorkOfDate”))%>”>

<input type=”button” name=”Submit Changes” class=”button” onClick=”submitChanges()” value=”Submit Changes”>

<script language=”javascript”>
//Validate date
{
var today = new Date();

function valid_date(arnumbers)
{
var A= arnumbers.split(/D+/);

try
{
A[2]*= 1;
A[0]-= 1;
A[1]*= 1;
}

catch(er)
{
throw ‘Bad Date Input: should be a valid date in mm/dd/yyyy format.’;
}

var D= new Date(A[2],A[0],A[1]);
if(D.getDate()== A[1] && D.getMonth()== A[0] && D.getFullYear()== A[2])
return D;
return false;
}
}

//Submit Page
function submitChanges()

{
var msg = “”;
var WorkOfDate = document.frmEditIssue.txtWorkOfDate.value;
var WorkOfDateResult = valid_date(WorkOfDate);

//Validate work of date
if (!WorkOfDateResult)
{
msg += “Please enter a valid work of date in mm/dd/yyyy format.n”;
}

if (WorkOfDateResult > today)
{
msg += “Work of date cannot be in the future. Please enter a valid date.n”;
}

if (msg == “”)
{

frmEditIssue.action = “update_issue_post.asp”;
frmEditIssue.submit();
}

else
{
alert (msg);
}
}
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@baldwingrandauthorOct 06.2009 — FYI - I did an alert on the variable WorkOfDate, and it is displaying the correct date in the correct format. However, when I do an alert of the variable WorkOfDateResult, it returns false. So, for some reason, the date in the field is not being recognized as valid, even though it is. Any ideas?
×

Success!

Help @baldwingrand spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...