I am forced to use the old style validation for some basic form elements, and I am having problems with a date input box, I figured that it automatically stores the value as a String so the "AND isDate(form.Effective_Date)" should return a true or false, what it does instead return a blank space that ends up passing my initial validation .

I guess me question is, I am grabbing a value from a form (YYYYMMDD) which I need converted to number and/or then checked for valid, present and non space value.

<p><label for="Effective_Date">Effective Date of the assignment (yyyymmdd):</label><br />
	<input type="text" class="date" maxlength="8" value="" id="Effective_Date" name="Effective_Date" tabindex="1400" /></p>
<cfif ISDEFINED("form.Effective_Date")>
	<cfset Effective_Date = Trim(form.Effective_Date)>
	  <cfset Effective_Date = "">
<cfif NOT ISDEFINED("form.Effective_Date") OR form.Effective_Date EQ "">
	<cfset errors = errors & "<li class=""errors"">Effective Date</li>">

Recommended Answers

All 6 Replies

<cfif NOT ISDEFINED("form.Effective_Date")>

Text fields always exist. So IsDefined will do nothing.

the "AND isDate(form.Effective_Date)" should return a true or false

It will, but the code's not using IsDate ;-) It's checking if the value is an empty string "". Trim() the field and use IsDate instead.

thanks guys, will give all this a go, and I didn't know that about isDefined..seems pointless now.

<cfif TRIM(form.Effective_Date) NEQ "" AND isDate(TRIM(form.Effective_Date))>
	<cfset Effective_Date = TRIM(form.Effective_Date)>
	  <cfset errors = errors & "<li class=""errors"">Effective Date</li>">

So, if I put numeric value into the form field, it keeps failing since it's stored as a string, how do I convert it to pure numeric before making any validation onto it? Otherwise the trim works awesome, and isDefined is useless as stated...

That's not why it's failing. Just about everything is stored as a string in CF. Trimming only removes leading/trailing white space. If the field has any other value, it'll be evaluated by IsDate. So trimming does not buy you much here either.

What's an example of these "numeric" date values that are failing? Seems like you're trying to pass in something isDate doesn't understand.

isDefined is useless

With forms it's useful for checkboxes, radio buttons or multi-select lists. ie Fields that don't always exist.

well, I have tried any type of input but the ideal is that if they enter (numeric) yyyymmddd (20110224). Since it is being examined as string, isDate is not working.

html form code below:

<input type="text" class="date" maxlength="8" value="" id="Effective_Date" name="Effective_Date" tabindex="1400" />

since it would only return true or false, I was wondering if before I made any validation attempt, that I would store it as an integer instead of string...

"20110224" isn't a standard date string. IsDate expects something recognizable as a US date string, such as: yyyy-mm-dd. So even 2011-02-24 would work. Keep in mind the form field value's just a string. You can easily validate it and format it however you want with a few string functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.