Skip to main content
Known Participant
November 13, 2006
Answered

Simple text field

  • November 13, 2006
  • 14 replies
  • 910 views
I want to call a javascript if User clicks on a button and the text field is empty.
here is the button and the text field:
<cfinput type="file" name="custCartFile">
<cfinput type="submit" name="submit" value="Add Image">
Thanks,
Trint
This topic has been closed for replies.
Correct answer MrBonk
The easiest way is to return the javascript function from the onSubmit event of your form. onSubmit = "return your_function()". That way, when your user clicks your submit button, your script can check whatever you want to check *before* the form is actually submitted.

Like this:

<script type="text/javascript" language="javascript">
function check_file_field()
{
var file_field = document.getElementById("flFileField");
if(file_field.value.length == 0)
{
alert("Select a file first!");
return false;
}
else
{
return true;
}
}
</script>

<cfform action="#cgi.script_name#" method="post" name="frmTest" preloader="no" onsubmit="return check_file_field()">
<cfinput type="file" id="flFileField" name="flFileField">
<cfinput type="submit" name="btnSubmit" value="Go">
</cfform>

14 replies

Inspiring
November 13, 2006
My mistake. You have to check it on your action page.

form page
<form action="actionpage.cfm" enctype="multipart/form-data" method="post">
<input type="file" name="inputFile" size="45">

action page
<cfif len(InputFile) is 0>
no file, write code to handle it
</cfif>
November 13, 2006
> here is the button and the text field:

You don't have a text field. You have a type="file" and a type="submit".
atsidiAuthor
Known Participant
November 13, 2006
I tried that and got "error in submit text"
Can you give me the exact context?
Thanks
Inspiring
November 13, 2006
required="yes" is easier