Copy link to clipboard
Copied
Hi. I have a currency field on a form page that gets inserted into a currency field in Access. Is there a way to check the validation of this field so it's correct before it gets added to the database? I can't use the Integer validation because that requires whole numbers and won't allow decimal points. I also have the $ pre-filled in. I could remove that if I had to. I just want an error to display letting the user know that they entered the dollar value incorrectly. There have been some issues with some people entering a number like 52.00.00 and it errors out and things get all goofed up in the database. Thanks for your help.
Andy
Copy link to clipboard
Copied
Use jQuery for preliminary front-end validation. Then, at the server end, you could do something like
<cfif isDefined("form.someFieldname")>
<cfset session.formSubmissionStatus = structNew()>
<!--- Code to trim currencyField and strip off dollar sign --->
<cfset variables.currency = replace(trim(form.currencyField),"$","")>
<cfif NOT isNumeric(variables.currency)>
<cfset session.formSubmissionStatus.currencyField.value = variables.currency>
<cfset session.formSubmissionStatus.currencyField.message = "Currency must be a numerical value.">
<cflocation url="url-of-form-page">
</cfif>
<!--- Code to insert field(s) to database --->
</cfif>
If the server validation fails, the variables session.formSubmissionStatus.currencyField.value and session.formSubmissionStatus.currencyField.message will be available when the user is redirected to the form page. That enables you to inform the user that "Currency must be a numerical value.".
A more general, and better, validation technique is to give the responsibility of validating the entire form to a function. Something like
<cfif isDefined("form.someFieldname")>
<cfset session.formSubmissionStatus = structNew()>
<cfset session.formSubmissionStatus = validate(form)>
</cfif>
In the validate function, you will then validate each form field, storing each validation result a struct which you finally return.