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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Required Field Validations 2

Status
Not open for further replies.

rmz8

Programmer
Aug 24, 2000
210
US
How can I validate that a textbox has text in it? Also, how can I make a file field a required entry?

Ryan ;-]
 

You can always do

<form name=&quot;form&quot; action=&quot;&quot;>
<input type=&quot;text&quot; name=&quot;user&quot;>
<br>
<input type=&quot;text&quot; name=&quot;pass&quot;>
<input type=&quot;submit&quot; name=&quot;B1&quot; onclick=&quot;
if (!document.form.user.value || !document.form.pass.value){
alert('Fill In All The Fields');
return false;
}
&quot;>
</form>

But I'd actually like to know a better way to do it, if no one else posts I guess you could use that hehe.

Tony
 
Is there any way to do it without using OnClick? I'm also using a loop with ColdFusion, so I need the code to check if a variable number file boxes are filled out or not.

Ryan ;-]
 
Instead of onclick?

Hmm, There's always onsubmit and you could move the javascript into a function call in the head, but I can't remember if return false; will kill the submission if it finds empty fields.

I'll check it out in front page ...

Alrighty, that was quick ehh, ok, so what you could do is this,

<form name=&quot;form&quot; method=&quot;POST&quot; action=&quot;&quot; onsubmit=&quot;
if (!document.form.user.value || !document.form.pass.value){
alert('Fill In All The Fields');
return false;
}
&quot;>
<p><input type=&quot;text&quot; name=&quot;user&quot; size=&quot;20&quot;></p>
<p><input type=&quot;text&quot; name=&quot;pass&quot; size=&quot;20&quot;></p>
<p><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;></p>
</form>

Just don't try moving the javascript into a function the way it is, because as is is wouldn't kill the submission when needed using return false;

Ok, hope that'll work.

Tony
 
to precise : the actual correct call to a validation fonction is the onsubmit one : <form .... onsubmit=&quot;return validation_function()&quot;>
if you use onclick in the submit button you might get unexpected results (such as : submitting the form even if the validation function returned false)
also, don't forget to return a value from the validation function - and to expect one in the onsubmit call (was the : RETURN ... part)
 
I didn't want to use this method because I wanted to place the code within the head. What I have is a a dynamic page that generates a specified number of upload boxes based on a number that the user enters. I have to have each upload box filled in (i.e. Browsed so that a file path is in it). I need to use a ColdFusion loop within whatever JavaScript that I use to generate the validation. That is what I'm asking how to do; how can I create a function incorporating my ColdFusion loop and then call to it using OnClick. I already have a function validating something else that is being called with OnClick, and I didn't know if you could call two functions with OnClick, so that's why I wanted to use another method. Here is the complete code for the page:

=====================================================

Code:
<CFOUTPUT>
<HTML>
<HEAD>

<META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=iso-8859-1&quot;>
<!--Check for password in URL --> 
<CFIF IsDefined(&quot;Session.PWord&quot;) AND IsDefined(&quot;Session.UN&quot;)>
<!--Authorization check --> 
<CFIF IsDefined(&quot;Session.Auth&quot;) is NOT TRUE>
<CFLOCATION URL=&quot;logout.cfm&quot;>
<CFELSE>
<!--Run query to get user information --> 
<CFQUERY datasource=&quot;#DSN#&quot; name=&quot;getUser&quot;>
SELECT RealName FROM Users WHERE (UserName = '#Session.UN#' AND Password = '#Session.PWord#')
</CFQUERY>
<CFIF getuser.recordcount EQ &quot;0&quot;>
<CFLOCATION url=&quot;logout.cfm&quot;>
<CFELSE>
<!--Checks if URL primary key value is present --> 
<CFIF IsDefined(&quot;URL.ID&quot;)>
<CFSET NewRecord = &quot;No&quot;>
<CFELSE>
<CFSET NewRecord = &quot;Yes&quot;>
</CFIF>
<!--Sets variables for primary key not being present --> 
<CFIF NewRecord>
<CFSET PageTitle = &quot;Add Resource&quot;>
<CFSET Teacher = Trim(Session.Teacher)>
<CFSET Resource_Title = &quot;&quot;>
<CFSET Resource_Type = &quot;--SELECT AN OPTION--&quot;>
<CFSET Subject_Area = &quot;--SELECT AN OPTION--&quot;>
<CFSET Grade_Level = &quot;--SELECT AN OPTION--&quot;>
<CFSET Resource_Description = &quot;&quot;>
<CFELSE>
<!--Queries database and sets variables for primary being present --> 
<CFQUERY datasource=&quot;#DSN#&quot; name=&quot;getResource&quot;>
SELECT * FROM Resources WHERE ID = #URL.ID#
</CFQUERY>
<CFSET PageTitle = &quot;Update Resource&quot;>
<CFSET Teacher = Trim(Session.Teacher)>
<CFSET Resource_Title = &quot;#Trim(getResource.Resource_Title)#&quot;>
<CFSET Resource_Type = &quot;#Trim(getResource.Resource_Type)#&quot;>
<CFSET Subject_Area = &quot;#Trim(getResource.Subject_Area)#&quot;>
<CFSET Grade_Level = &quot;#Trim(getResource.Grade_Level)#&quot;>
<CFSET Resource_Description = &quot;#Trim(getResource.Resource_Description)#&quot;>
</CFIF>

<TITLE>ZEAL network's GATSBY: #PageTitle#</TITLE>
  <SCRIPT language=&quot;JavaScript&quot;>
<!--

function func(obj, num, othobj)
{
str = new String(obj.value);
othobj.value = new Number(str.length+1);
dnum = num -1;
if(str.length >= dnum)
{
alert('You can only have '+num+' characters in this field.');
obj.onfocus = obj.blur;
}
}
    function checkSelects(form) {
    
    select1 = form.Resource_Type
    select2 = form.Subject_Area
    select3 = form.Grade_Level

    if(select1.value=='xyz') {
        alert('Please select a valid option.')
        return false;
        }
    if(select2.value=='xyz') {
        alert('Please select a valid option.')
        return false;
        }

    if(select3.value=='xyz') {
        alert('Please select a valid option.')
        return false;
        }
    return true;    
    } 
//-->
</SCRIPT>
  </HEAD>


<BODY BGCOLOR=&quot;##FFFFFF&quot;><cfinclude template=&quot;../includes/header.html&quot;>
  <H2><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot;><U>#PageTitle#</U> <B><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;></FONT></B><U><B><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;>[<A HREF=&quot;index.cfm&quot;>Go 
    back</A>] </FONT></B></U></FONT></H2>
<CFFORM action=&quot;add_update.cfm&quot; method=&quot;post&quot; name=&quot;frm&quot; enctype=&quot;multipart/form-data&quot;>
    <INPUT type=&quot;hidden&quot; name=&quot;Teacher&quot; value=&quot;#Session.Teacher#&quot;>
<CFIF newrecord EQ &quot;No&quot;>
 <INPUT type=&quot;hidden&quot; name=&quot;ID&quot; value=&quot;#URL.ID#&quot;></CFIF>  
    <TABLE BORDER=&quot;0&quot;>
      <TR> 
        <TD HEIGHT=&quot;7&quot; VALIGN=&quot;TOP&quot; COLSPAN=&quot;2&quot;><I><B><FONT SIZE=&quot;2&quot; FACE=&quot;Arial, Helvetica, sans-serif&quot;></FONT></B></I><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Resource 
          Title:</B></FONT></TD>
        <TD HEIGHT=&quot;7&quot; WIDTH=&quot;84%&quot;> <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;> 
          <CFINPUT TYPE=&quot;TEXT&quot; NAME=&quot;Resource_Title&quot; VALUE=&quot;#Resource_Title#&quot; SIZE=&quot;10&quot; REQUIRED=&quot;yes&quot; MESSAGE=&quot;Please enter a title for your resource.&quot;>
          </FONT></TD>
      </TR>
      <TR> 
        <TD HEIGHT=&quot;2&quot; VALIGN=&quot;TOP&quot; COLSPAN=&quot;2&quot;><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Resource 
		  Type:</B></FONT><BR>
		  <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;1&quot;>If you are uploading 
		  a web page with images and other files make sure the type selected is 
		  HTML.</FONT></TD>
        <TD HEIGHT=&quot;2&quot; WIDTH=&quot;84%&quot;> 
          <SELECT NAME=&quot;Resource_Type&quot;>
            <CFIF newrecord><OPTION VALUE=&quot;xyz&quot; SELECTED>#Resource_Type#</OPTION><CFELSE><OPTION VALUE=&quot;#Resource_Type#&quot; SELECTED>#Resource_Type#</OPTION></CFIF>
            <OPTION VALUE=&quot;xyz&quot;>+-+-+-+-+-+-+-+</OPTION>
            <OPTION VALUE=&quot;Adobe PDF&quot;>Adobe PDF</OPTION>
            <OPTION VALUE=&quot;GIF&quot;>GIF</OPTION>
            <OPTION VALUE=&quot;HTML&quot;>HTML</OPTION>
<OPTION VALUE=&quot;JPEG&quot;>JPEG</OPTION>
<OPTION VALUE=&quot;Microsoft Access&quot;>Microsoft Access</OPTION>
            <OPTION VALUE=&quot;Microsoft Excel&quot;>Microsoft Excel</OPTION>
            <OPTION VALUE=&quot;Microsoft PowerPoint&quot;>Microsoft PowerPoint</OPTION>
            <OPTION VALUE=&quot;Microsoft Word&quot;>Microsoft Word</OPTION>
            <OPTION VALUE=&quot;Plain Text&quot;>Plain Text</OPTION>
          </SELECT>
        </TD>
      </TR>
      <TR> 
        <TD HEIGHT=&quot;2&quot; VALIGN=&quot;TOP&quot; COLSPAN=&quot;2&quot;><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Subject 
          Area:</B></FONT></TD>
        <TD HEIGHT=&quot;2&quot; WIDTH=&quot;84%&quot;> 
          <SELECT NAME=&quot;Subject_Area&quot;>
            <CFIF newrecord><OPTION VALUE=&quot;xyz&quot; SELECTED>#Subject_Area#</OPTION><CFELSE><OPTION VALUE=&quot;#Subject_Area#&quot; SELECTED>#Subject_Area#</OPTION></CFIF>
            <OPTION VALUE=&quot;xyz&quot;>+-+-+-+-+-+-+-+</OPTION>
            <OPTION VALUE=&quot;Computers&quot;>Computers</OPTION>
            <OPTION VALUE=&quot;Guidance&quot;>Guidance</OPTION>
            <OPTION VALUE=&quot;English&quot;>English</OPTION>
            <OPTION VALUE=&quot;Family & Consumer Sciences&quot;>Family & Consumer Sciences</OPTION>
            <OPTION VALUE=&quot;Fine Arts
&quot;>Fine Arts</OPTION>
            <OPTION VALUE=&quot;Foreign Language&quot;>Foreign Language</OPTION>
            <OPTION VALUE=&quot;Mathematics&quot;>Mathematics</OPTION>
            <OPTION VALUE=&quot;Music&quot;>Music</OPTION>
            <OPTION VALUE=&quot;Physical Education&quot;>Physical Education</OPTION>
            <OPTION VALUE=&quot;Science&quot;>Science</OPTION>
            <OPTION VALUE=&quot;Social Studies&quot;>Social Studies</OPTION>
            <OPTION VALUE=&quot;Special Educaton&quot;>Special Educaton</OPTION>
          </SELECT>
        </TD>
      </TR>
      <TR> 
        <TD HEIGHT=&quot;2&quot; VALIGN=&quot;TOP&quot; COLSPAN=&quot;2&quot;><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Grade 
		  Level:</B></FONT></TD>
        <TD HEIGHT=&quot;2&quot; WIDTH=&quot;84%&quot;> 
          <SELECT NAME=&quot;Grade_Level&quot;>
<CFIF newrecord>
            <OPTION VALUE=&quot;xyz&quot; SELECTED>#Grade_Level#</OPTION><CFELSE><CFIF grade_level LT &quot;4&quot;><CFIF grade_level EQ &quot;1&quot;><OPTION VALUE=&quot;#Grade_Level#&quot; SELECTED>#Grade_Level#st Grade</OPTION></CFIF><CFIF grade_level EQ &quot;2&quot;><OPTION VALUE=&quot;#Grade_Level#&quot; SELECTED>#Grade_Level#nd Grade</OPTION></CFIF><CFIF grade_level EQ &quot;3&quot;><OPTION VALUE=&quot;#Grade_Level#&quot; SELECTED>#Grade_Level#rd Grade</OPTION></CFIF><CFELSE>
<OPTION VALUE=&quot;#Grade_Level#&quot; SELECTED>#Grade_Level#th Grade</OPTION></CFIF></CFIF>
            <OPTION VALUE=&quot;xyz&quot;>+-+-+-+-+-+-+-+</OPTION>
            <OPTION VALUE=&quot;1&quot;>1st Grade</OPTION>
            <OPTION VALUE=&quot;2&quot;>2nd Grade</OPTION>
            <OPTION VALUE=&quot;3&quot;>3rd Grade</OPTION>
            <OPTION VALUE=&quot;4&quot;>4th Grade</OPTION>
            <OPTION VALUE=&quot;5&quot;>5th Grade</OPTION>
            <OPTION VALUE=&quot;6&quot;>6th Grade</OPTION>
            <OPTION VALUE=&quot;7&quot;>7th Grade</OPTION>
            <OPTION VALUE=&quot;8&quot;>8th Grade</OPTION>
            <OPTION VALUE=&quot;9&quot;>9th Grade</OPTION>
            <OPTION VALUE=&quot;10&quot;>10th Grade</OPTION>
            <OPTION VALUE=&quot;11&quot;>11th Grade</OPTION>
            <OPTION VALUE=&quot;12&quot;>12th Grade</OPTION>
          </SELECT>
        </TD>
      </TR>
      <TR> 
        <TD HEIGHT=&quot;2&quot; VALIGN=&quot;TOP&quot; COLSPAN=&quot;2&quot;> 
          <P><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Resource Description:<BR>
            </B> <FONT SIZE=&quot;1&quot;>500 Chararacters Maximum</FONT></FONT></P>
          <P> 
            <INPUT TYPE=&quot;text&quot; name=&quot;textbox&quot; id=&quot;textbox&quot; SIZE=&quot;3&quot;>
            <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;1&quot;>Characters</FONT></P>
        </TD>
        <TD HEIGHT=&quot;2&quot; WIDTH=&quot;84%&quot;> 
          <TEXTAREA NAME=&quot;Resource_Description&quot; COLS=&quot;70&quot; ROWS=&quot;8&quot; onkeypress=&quot;func(this, 500, document.frm.textbox)&quot;>#Resource_Description#</TEXTAREA>
        </TD>
      </TR>
      <CFIF newrecord>
      <TR> 
        <TD HEIGHT=&quot;2&quot; WIDTH=&quot;16%&quot; VALIGN=&quot;TOP&quot; COLSPAN=&quot;2&quot;><FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;><B>Files: 
          </B></FONT><BR>
          <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;1&quot;>You can upload up 
          to #form.fileuploads# file(s). If you are uploading a web page make 
          sure that the HTML file is the last file in the upload list.</FONT></TD>
        <TD HEIGHT=&quot;2&quot; WIDTH=&quot;84%&quot;> 
          <input type=&quot;hidden&quot; name=&quot;numberInputs&quot; value=&quot;#form.fileuploads#&quot;>
          <cfloop index=&quot;x&quot; from=&quot;1&quot; to=#form.fileuploads#>

              <p><INPUT TYPE=&quot;file&quot; NAME=&quot;upFile#x#&quot;></p>
</cfloop>

          </TD>
      </TR></CFIF>
      <TR> 
        <TD HEIGHT=&quot;2&quot; COLSPAN=&quot;3&quot;> <FONT FACE=&quot;Arial, Helvetica, sans-serif&quot; SIZE=&quot;2&quot;> 
          <INPUT TYPE=&quot;submit&quot; VALUE=&quot;Submit&quot; onClick=&quot;return checkSelects(this.form);&quot;>
          <INPUT TYPE=&quot;RESET&quot; NAME=&quot;Reset&quot; VALUE=&quot;Reset&quot;>
          </FONT></TD>
      </TR>
    </TABLE>
</CFFORM><cfinclude template=&quot;../includes/footer.html&quot;></CFIF></cfif>
<CFELSE>
<CFLOCATION URL=&quot;logout.cfm&quot;>
</CFIF>
</BODY>
</HTML>
</CFOUTPUT>

=====================================================

Ryan ;-]
 
&quot;<form .... onsubmit=&quot;return validation_function()&quot;>&quot; and &quot;I didn't want to use this method because I wanted to place the code within the head.&quot; -> this method calls a validation function that you can place in the head !!!!! so ????

also, about the cf loop, it is feasible INSIDE the javascript (as js is CLIENT side)
something like
function validation_function() {
<cfloop query=....>
if (#cfeval(select#i#.value=='xyz'))
{
alert('Please select a valid option.')
return false;
}
</cfloop>
}

and no, i didn't read your code, why did you cut/paste the whole page ???? why didn't you keeep only the relevant parts, so that people don't get afraid and read at least a part of your code ?
 
Iza, unfortunately my efforts to create the function did not succeed.

I tried this:

=====================================================
Code:
function validation_function

if (!document.form.Resource_Description.value){
alert('Please enter a description for your resource.');
return false;
}

<cfloop index=&quot;x&quot; from=&quot;1&quot; to=#form.fileuploads#>

if (!document.form.upfile#x#.value){
alert('Please select files to upload for your resource.');
return false;
}

</cfloop>
=====================================================

However, when I try to call to it with this (within the FORM tag):

=====================================================
Code:
onsubmit=&quot;return validation_function()&quot;
=====================================================

I get a JavaScript error. The error might be because I am using ColdFusion JavaScript form validation, which generates JavaScript for an OnSubmit validation. Excuse my density when it comes to JavaScript, but I'm just not well versed in it.

Ryan ;-]
 
I finally got the code and the function to work (Thanks Iza and Slueth). However, there is one problem. I don't think that JavaScript recognizes the value portion of a file upload field:

if (!document.frm.upfile1.value){
alert('Please select files to upload for your resource.');
return false;
}

The first part of the validation function works fine (validating that the textarea is not empty), but this gets skipped over after giving the message that it is &quot;null or not an object.&quot; The name of the form is &quot;frm&quot; and I have the textarea being validated in a similar manner and it works.

Ryan ;-]
 
Thanks a lot guys. I'm working on expanded code that is giving me some trouble and I created a new post. This issue is resolved!

Ryan ;-]
 
rmz8, i'm sorry i didn't reply earlier - too much work ... i'm happy it's now working for you :) - how did you fix the uploaded file problem ?? was it a multipart/form-data missing somewhere ?
 
Hey iza,

The case of the file field was wrong. Link9 told me to change it and it worked! Now, however, we are both baffled by the following code, which just will not work in Netscape 4.7 and 6 (we've worked on it for hours and it is just overridden in Netscape and provides no errors, but in Internet Explorer it works perfectly). The name of the form is frm:
Code:
    if(document.frm.Resource_Type.value=='xyz') {
        alert('Please select a resource type.')
        return false;
        }
    if(document.frm.Subject_Area.value=='xyz') {
        alert('Please select a subject area for your resource.')
        return false;
        }

    if(document.frm.Grade_Level.value=='xyz') {
        alert('Please select a grade level for your resource.')
        return false;
        }


Ryan ;-]
 
are you sure it provides no error at all ? what does the javascript console tell ???
 
Hey iza,

In Netscape 4.7 and 6 no errors are generated and there is nothing in the JS console.

Ryan ;-]
 
iza,

Any advice?

Thanks!

Ryan ;-]
 
sorry, once again i was &quot;underwater&quot; - i mean, almost sinking under work ;-)
if there is no error at all ... it'll be tough to debug !!!
i suggest (in ns) you alert all the values and maybe this will help (i would have done this for you but hey, i'm going to go to bed ;-))
alert(document.frm)
alert(document.frm.Resource_Type)
alert(document.frm.Resource_Type.value)
alert(document.frm.Resource_Type.value=='xyz')
-> if the error is in the way to refer to the elements, you'll spot it this way
-> if it's not, try your validation function for a very very very simple case : for example let only one textbox in the form, and then (in the function) test if this textbox.value==&quot;xyz&quot;. If it does work, then it's not the validation function nor the way to call it
-> finally, i've just noticed (sorry sorry sorry) that you're trying to read values for selectboxes ?? if this is right to write formname.elementname.value for a textbox, the correct way to retrieve the value for a select is : formname.elementname.options[formname.elementname.selectedIndex].value
-> i'll try to check this forum on wenesday, let me know !

 
iza,

You did it! I had to change the way I called the selects to the way you said! It works perfectly now in IE and Netscape. I wish I could give you 100 votes for TipMaster!

Thanks so much.

Ryan ;-]
 
i'm really happy this works for you know :)
and i'm sorry i didn't spot this waaaaay before !!!! it was in your 4th post (the one i told i didn't read !!! sorry sorry sorry !)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top