Solved

form validation

Posted on 1999-01-07
12
326 Views
Last Modified: 2012-05-04
I'm looking for some code that allows me to do the following:

The user fills in a form and clicks a button. The code would check to see if all required fields are filled in. If not, it would display the form again with his data, plus it would put a red star or something next to the required fields so the user knows he/she has to fill in those fields.

I know how to check if something's filled in or not. How do I get the red star to appear? I'd like to use ASP to do this, if not, then javascript. Thanks.
0
Comment
Question by:hobster
  • 5
  • 5
  • 2
12 Comments
 
LVL 2

Expert Comment

by:Quixote
ID: 1276461
This aint that tough, but worht a bit more than 10 points -- if no one solves it for you soon, I'll come back and put the time in Friday night to help you out.  But right now I don't have the time...

the basic idea would be to place a clear gif next to each form field, and then swap that with a red star if the field is empty.  Anyone want to take a crack at that?

Cheers.
-Quixote
0
 

Expert Comment

by:MagicJ
ID: 1276462
u can do this in two ways
1) Javascript
here u have to have 2 images one a blank and second the star
each input box will have the image next to it.First only blank image.After validation if u find the field is filled up or so put the red star image.

code will be

k = new Image(20,20);
k.src = "\\images\\star.jpg"
l = new Image(20,20)
l.src = "\\images\\blank.jpg"

function submitme()
{
 document.imgname.src = k.src

}
<table border=0>
<tr>
<td>
<input type=text name=x>
</td>
<td>
<img name=imgname src="\images\blank.jpg'>
</td>
</tr>
</table>
<input type=button value="Submit" onClick=submitme()>

2) ASP
after the validation for each field next to it put
<font color=red>*</font>

I think this will help u . feel free to ask me
0
 
LVL 2

Author Comment

by:hobster
ID: 1276463
Notice the increase in points. That's all I have left.

Anyway, the form validation code has to be ASP. I can't use javascript because the user might turn that off on their browsers.

If you can point me to a url that shows me how to do this, that would be great. Thanks.
0
 
LVL 2

Expert Comment

by:Quixote
ID: 1276464
Thanks for the increase -- hopefully it will grab the attention of the experts who gives you the answer you need.  Here's my stab at it, given that I don't know ASP and think you should "validate" using both JavaScript and ASP.  Why? Cuz client-side checking is just good intereface, but for reasons you point out, is not very good at solid validation prior to database entry.


Here's how you get the red stars:

First idea I have is to start off with red stars next to the required fields, that turn clear when the field is valid.  Like so...

<img src="red_star.gif" name="zip_code_img">
<input type=text name="zip_code" value=""
   onBlur="if(this.value && document.images)
      document.zip_code_img.src = 'clear_star.gif';">

The star will turn clear as soon as the user leaves the field with something in it.  This happenes automatically, before the user clicks "submit".


Another option works when the user click submit, and actually checks to make sure the required form elements are filled out...

<img src="clear_star.gif" name="zip_code_img" align=left>
<input type=text name="req_zip_code" value=""><br>

<input type=text name="middle_initial" value=""><br>

<img src="clear_star.gif" name="email_img" align=left>
<input type=text name="req_email" value=""><br>

<input type=submit
   onClick="var Errors=0;
      var It=this.form.elements;
      for (var xx=0; xx<It.length; xx++)
         {
         if (It[xx].name.indexOf('req_') +1)
            {
            if (document.images)
               eval('document.'+It[xx].name.substring(4)+'_img.src=\'red_star.gif\'');
            Errors++;
            }
         }
      var Message = (document.images) ? 'They have been marked with red stars.' : '';
      if(Errors)
         {
         alert('Your form contains '+Errors+' incomplete fields.\n'+Message);
         return false;
         }
      else return true;">


This ought to work, so long as you put "req_" in the front of every form element's name that is required, and name the corresponding flag image the same thing but with "_img" trailing it.  The script steps through the form, element by element, looking for "req_".  When it finds it, it verifies it has a value -- and if it doesn't then it flags the field, counts an error, and stops the submission of the form.

But they need to have JavaScript turned on. <sigh type="deep">  Let me know if this is good for you, and I will post it as an answer.


Cheers.
-Quixote
0
 
LVL 2

Author Comment

by:hobster
ID: 1276465
Thanks, but I still need the ASP code as well. However, I will give you some points for your effort. I will post a question early next week just for you only and all you need to do is reply as a answer.
0
 

Expert Comment

by:MagicJ
ID: 1276466
The code that I've given for the Javascript part is same as Quixote except that my code more or less like a algorithm.Hope u understand that.

Now regarding ASP

<%
if request.form("req_field") = "" then
   response.write("<img src=images\star.gif>")
end if
%>

I think u would've got an idea of how validation has to be done using ASP code. If u want more details tell me.

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:hobster
ID: 1276467
MagicJ, how do I get the form to reload the form with the current data along with some text saying "these fields need to be filled in"? If I do a redirect, it brings me back to the form but all the data is gone and it doesn't write "name empty"? Thanks.

P.S. Default.asp is the form the user has to fill out.

If name = "" Then
   Response.Redirect "default.asp"            
   Response.Write "name empty"
Else
   ' display values
End If
0
 

Expert Comment

by:MagicJ
ID: 1276468
<% response.buffer=true%>

<html>
<body>
<%
dim fillFlag
dim ErrStr
dim FirstTimeOnPage


ErrStr= ""
fillFlag = 0
dim CurrentPage
  CurrentPage=Request.ServerVariables("SCRIPT_NAME")

  If Request.ServerVariables("REQUEST_METHOD")="GET" then
      FirstTimeOnPage =True
  else
      if inStr(Request.ServerVariables("HTTP_REFERER"),CurrentPage)=0 then
      FirstTimeOnPage = True
      else      
      FirstTimeOnPage = False
    end if
  end if
%>
<form method=POST action=''>
<table>
<tr>
<td>
Enter your name
</td>
<td>
<input type=text name='name' value=<%=request.form("name")%>>
</td>
<td>
<% if Not FirstTimeOnPage AND request.form("name") = "" then
      fillFlag = fillFlag + 1
      ErrStr = ErrStr & "Name Required "
%>
<font color=red> * </font>
<%end if %>
</td>
</tr>
<tr>
<td>
Enter your no
</td>
<td>
<input type=text name='no' value=<%=request.form("no")%>>
</td>
<td>
<% if  Not FirstTimeOnPage AND request.form("no") = "" then
      fillFlag = fillFlag + 1
      ErrStr = ErrStr & "No Required "
%>
<font color=red> * </font>
<%end if %>
</td>
</tr>
<tr>
<td>
Enter your Addr
</td>
<td>
<input type=text name='addr' value=<%=request.form("addr")%>>
</td>
<td>
<% if Not FirstTimeOnPage AND  request.form("addr") = "" then
      fillFlag = fillFlag + 1
      ErrStr = ErrStr & "Addr Required "
%>
<font color=red> * </font>
<%end if %>
</td>
</tr>
</table>
<br>
<input type=submit>

</form>
<% if  Not FirstTimeOnPage AND fillFlag = 0 then
            response.clear
            response.redirect("RMSchd.asp")
   end if
%>
<br>
<font color=Red><i><%=ErrStr%></i></font>
</body>
</html>

0
 
LVL 2

Author Comment

by:hobster
ID: 1276469
This is for MagicJ. Once all the fields are filled out and I go to RMSchd.asp, how do I make it so the field values are displayed on this page? I've tried session variables but it didn't seem to work. For example:

' on first page
<% if  Not FirstTimeOnPage AND fillFlag = 0 then
    response.clear
    Session("name") = Request.Form("name")
    response.redirect("RMSchd.asp")
       end if
    %>

' on RMSchd.asp
Response.Write Session("name")

P.S. You're very close. I've added 5 more points for the extra effort. Thanks.
0
 

Accepted Solution

by:
MagicJ earned 40 total points
ID: 1276470
<% if  Not FirstTimeOnPage AND fillFlag = 0 then
response.clear
response.redirect("RMSchd.asp?name=" & request.Form("name") & "&No=" & request.form("no") & "&Addr=" & request.form("addr"))
   end if
%>
this will work 'cause the var's are passed as url but in the next page the var's are accessed as Request.QueryString("name")
Is this OK for u?


0
 
LVL 2

Author Comment

by:hobster
ID: 1276471
I will accept the answer. The only problem I have is if there is a space in a field, such as "John Smith" in the name field, then I get an error. Is there a way around this? Thanks.
0
 

Expert Comment

by:MagicJ
ID: 1276472
use server.URLEncode("John Smith") this will translate all the special chars to hex format.

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now