Skip to main content
Participating Frequently
December 29, 2008
Question

server-client form validation

  • December 29, 2008
  • 1 reply
  • 406 views
Hello to All!!
I'm thinking about site-validation, I'm going to use both: server & client. But I don't want to give to server extra work.
For example:
I want to validate a simple text input in a form (text must have from 3 to 15 symbols)
<form name="myform" method="post" action="action.cfm" onsubmit="return validation();">
<input type="text" name="name">
</form>

On the "form-page" I write:
<script language="javascript" type="text/javascript">
function validation(){
if ((document.myform.name.value.length < 3)||(document.forma.name.value.length > 15)){
alert ("ERROR");
return false;
}
return true;
}
</script>

But on the action.cfm page I also have:

<cfscript>
if (structKeyExists(form,"name") and len(trim(form.name))) {
variables.name = Len(form.name);
if (name LT 3 or name GT 15) {
writeoutput("ERROR") ;
}
}
</cfscript>

So my <cfscript> will always work, when form will submit, even when form will submit with CORRECT DATA after "script-check".

How I can identify that client turn off "enable javascript"??? Can I do it with CGI, how???

Thanks for your comments!!!
    This topic has been closed for replies.

    1 reply

    Inspiring
    December 29, 2008
    We do this for other reasons. Our method is

    <cftry>
    set cookie with js
    read cookie with cf
    <cfcatch>
    code for no js

    However, I don't think it's worth it for the situation you described.
    December 29, 2008
    Unless you REALLY need JS, which you clearly don't, then I wouldn't bother with trying to detect if it's off - what would be the point? If so, why have the server side code at all? Some apps are fully JS driven, and in this case Dans method would be the best. But for your situation I think a simple <noscript> tag will do the job just nicely.

    e.g:

    <noscript><p>You have JavaScript disabled. Please enable it if you can or the world will end.</p></noscript>

    Cheers.