Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cfinput and required

Status
Not open for further replies.

bartee

MIS
Mar 20, 2005
147
US
I am having a problem with cfinput required attribute working in a form where I have another jscript function running on submit. For some reason, when I try to use the code as follows, a message does not appear if the form field is left blank like it should, it allows the form to submit empty data. When I remove the javascript function, the cfinput works correctly. Any suggestions greatly appreciated.

Here is javascript that runs on submit.
<script language="javascript">
function checkform()
{

if (membership.Password.value=="")
{alert("Please Enter Your Password.");
return false;}
else

if (membership.Password2.value=="")
{alert("Please Re-Enter Your Password.");
return false;}
else

if (membership.email.value){
{
var emapat = /^[a-z][a-z_0-9\-.]+@[a-z_0-9\-.]+\.[a-z]{2,3}$/i;
if(!emapat.test(membership.email.value))
{alert('Please Enter a Valid Email Address.');
return false;}
}
}
var tempImage;
function getDimensions() {
if(tempImage.width > 75 || tempImage.height > 75){
alert("Forum Logo Must Be 75 * 75 or Less.");
}

if(tempImage.width < 76 && tempImage.height < 76)

{
document.membership.submit();
}
}
var imageName = document.membership.logo.value;
if (imageName != '') {
imageName = 'file:///' + escape(imageName.split('\\').join ('/'));
imageName = imageName.split('%3A').join(':');

tempImage = new Image();
tempImage.onload = getDimensions;
tempImage.src = imageName + '?randParam=' + new Date ().getTime();
return false;
}
}
</script>

Here is cfform:
<cfform action="/index2.cfm" method="post" enctype="multipart/form-data" name="membership" enablecab="yes" OnSubmit="return checkform();">

Here is cfinput not working:
<cfinput name="last" type="text" size="30" required="yes"
message="Please Enter Your Last Name" class="small-black" value="#userinfo.last#" maxlength="35">

Thanks in advance.
 
Don't use cfform. You'll be much better off writing the JavaScript instead of using the built in stuff (which, as you've found out, doesn't like any other JavaScript).




Hope This Helps!

ECAR
ECAR Technologies, LLC

"My work is a game, a very serious game." - M.C. Escher
 
Is there a way the user can bypass the script I used above?
 
danta,

Yes the user can bypass your validation by turning javascript off in their browser. It doesn't hurt to have client side form validation, but if you want to be sure (and secure) that validation was performed, I would use server side validation.

As far as your issue with validation not kicking off, it might be because of how you are referencing the form variables in your function checkForm(); Try prefixing your form filed references with 'document.';

for example:
Code:
function checkform(){
    if ([b]document.[/b]membership.Password.value==""){
       alert("Please Enter Your Password.");
       return false;
    }
    else if ([b]document.[/b]membership.Password2.value==""){
       alert("Please Re-Enter Your Password.");
       return false;
    }
    else if 
    
    ...

jalpino
 
I use a combination of client-side (JavaScript) and server-side (CF validation) for my form validations.

JS, obviously, is what jalpino displayed above. Client-side can be something like this:

Code:
<cfif isdefined("form.formfields") and isdefined("form.SubmitButton")>
  <!--- check if the 1st field is empty --->
  <cfif len(form.field1) eq 0><cfset invalidFIELD1 = "Please enter info in field1."></cfif>

  <!--- check if the 2nd field is empty --->
  <cfif len(form.field2) eq 0><cfset invalidFIELD2 = "Please enter info in field2."></cfif>

  <!--- if "FormError" is not defined, then take them to the next step --->
  <cfif NOT IsDefined("FormError")>
    <!--- Send to thank you page --->
    <cflocation url="formsubmit.cfm">
  </cfif>
</cfif>

<form name="formName" action="<cfoutput>#cgi.script_name#</cfoutput>">
  <!--- output the error message onto the screen if the validation fails --->
  <cfoutput>
    <cfif IsDefined("invalidFIELD1")>#invalidFIELD1#</cfif>
    <cfif IsDefined("invalidFIELD2")>#invalidFIELD2#</cfif>
  </cfoutput>
  ...
  PUT YOUR FORM HERE
  ...
</form>


____________________________________
Just Imagine.
 
try this thread and see if you can get anything out of it: thread232-934793

If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside.
 
I've been beating my head on this one, because I have had this exact problem before, but I can't find the page that had the issue. I remember the resolution was something like I had to hard-code onsubmit="return _cf_checkform(formname)" or maybe it was in the onclick event of the submit button? It was because something I had set up with custom javascript validation was not allowing the coldfusion validation to run.. Danta you might want to post up the view source for the page with the problem.

Anyway, it was kind of a hack, and for all I remember, it could have been my own error causing the validation not to run, but hard coding the cf generated function caused it to run both validations.
 
CFFORM depend heavily on Java. I a similar issue once where on one PC the CFFORM did not work but on another PC it ran perfectly. The reason for that was the 1st PC had out-dated Java class files.

Again, like many others mentioned here, use regular JS instead of the CFFORM. Gives you less headache.


____________________________________
Just Imagine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top