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

Not validating a single field!

Status
Not open for further replies.

madHatter1

Technical User
Feb 27, 2003
54
0
0
GB
Hello

I have a fairly simple form (no check boxes, etc), with 5 fields on it: name, email, business, country, message. Only a couple of these fields are essential, but once I understand how one works, I can work on the others. The problem is that, despite a fair amount of work (and countless uploadas!), I still can't get a single field to validate.

The contents of the form are passed to the databse - which works nicely - but empty wrongly-typed/fields do not generate the error messages which they should.

This is the code which I have, and would be grateful if someone could highlight the glaring error(s) I must be making.

<%option explicit%>
<%Dim ErrorMsg,fullname,email,business,country,message%>

<%
const numFields = 5
dim errorArray()
redim preserve errorArray(numFields)

if Request.Form(&quot;isSubmitted&quot;) = &quot;yes&quot; then

fullname = Request.Form(&quot;fullname&quot;)
email = Request.Form(&quot;email&quot;)
business = Request.Form(&quot;business&quot;)
city = Request.Form(&quot;country&quot;)
message = Request.Form(&quot;message&quot;)


ErrorMsg = &quot;&quot;
dim re, results
set re = New RegExp

'Full Name

re.Pattern = &quot;^[^0-9\/><\.,\\!\^\$\*\+\?@#%&\(\);:\[\]\{\}=&quot;&quot;']+$&quot;
re.Global = True
re.IgnoreCase = True
results = re.Test(fullname)
if results then
errorArray(0) = &quot;False&quot;
else
errorArray(0) = &quot;True&quot;
ErrorMsg = &quot;First Name<br>&quot;
end if


etc, etc, etc for each of the fields

until:

end if

%>

Beginning of HTML code here, and:

<% if ErrorMsg <> &quot;&quot; then %>
<font color=&quot;red&quot; face=&quot;verdana&quot; size&quot;2&quot;><b>
<%= ErrorMsg %>
</font><br>
<% end if %>

<form action=&quot;form_ac.asp&quot; method=&quot;post&quot;>

<% if errorArray(0) = &quot;True&quot; then %>
<font color=&quot;red&quot; face=&quot;verdana&quot; size=&quot;2&quot;><b>
<% end if %> Please type in your full name
<% if errorArray(0) = &quot;True&quot; then %>
</b></font>
<% end if %>

The above is repeated for each form field, each of which have the following characteristics:

<input type=&quot;text&quot; name=&quot;fullname&quot; value=&quot;<%= fullname %>&quot; class=&quot;txt&quot;></p>

I just can't see what I must be doing wrong!!

Hatter

 
It looks like you are trying to validate the the client with a server-side script. You might want to validate the from like this:
<Script language=&quot;vbscript&quot;>
Function CheckInputs
'required field

mLastName = document.addDir.mLastName.value

If mLastName = &quot;&quot; Then msg = &quot;Family Name is Required&quot;&vbCrLF

If msg = &quot;&quot; Then
document.addDir.submit
Else
MsgBox msg, vbCritical, End if
End Function
</script>

In script will look for a form named &quot;addDir&quot; and a field named &quot;mLastName&quot;. It will make see if the mLastName variable is populated with a value, if it is, then the form will submit, if it is not, then it will display a message box telling the user a last name is Required.

let me know if this is what you are looking for.
 
Hello RalphTrent

Thank you for your message.

I know form field validation can be done with JavaScript, and I can try your suggestion (I've saved your post) rather than the form be submitted without being validated.

However, I would prefer the server-side script. It seems cleaner, somehow, though there's no pint in that, is there, if it doesn't work!

Thanks again for your post.

Hatter
 
then you are going to need to use 2 files then. The form with all its elements and what not, and then a process asp page, that checks to see if forms if filled out to your
liking.
the form page could look like this:
<% if request.querystring(&quot;msg&quot;) <> &quot;&quot; then response.write request.form(&quot;msg&quot;)%>

<form action = &quot;checkFields.asp&quot; method=&quot;post&quot;>
<input type=&quot;text&quot; name=&quot;field1&quot;>
<input type=&quot;submit&quot;>
</form>

the asp page could look like this

<%
field1 = request.form(&quot;field1&quot;)
if field1 = &quot;&quot; then msg = &quot;Field 1 is not filled out&quot; & vbCrLf

if msg <> &quot;&quot; then
response.redirect &quot;pagewithform.asp?msg=msg&quot;
else
response.redirect &quot;thankyoupage.asp&quot;
%>

Let me know how this goes.
 
Hello ralphtrent

Many thanks for your message.

I think I might be getting there with your help!

OK, I now have a form.asp page and a thanks.asp page.

The code above holds the form and its different elements.
What I have noticed is that if I have this included in the code above:

<form name=&quot;Sample1&quot; method=&quot;post&quot;>

in other words, removing:

<form action=&quot;form_ac.asp&quot; method=&quot;post&quot;> (I don't mind remving this for the time being because this has to do with a database funtion which I am trying not to think about at the moment!)

then the fields are validated although I still have a doubt over the message field).

Unfortunately, this <form name=&quot;Sample1&quot; method=&quot;post&quot;>
doesn't point anywhere.

On the other hand, if I remove it and use:

<form name=&quot;thanks&quot; method=&quot;POST&quot; action=&quot;thanks.asp&quot;> it does bring up my 'Thank you' page although it doesn't actually personalise anything like it is supposed to do).

First things first. What I need, really, is to be able to use the:

<form name=&quot;Sample1&quot; method=&quot;post&quot;>

because it validates the form fields (the whole purpose of my 'help' message here!), while, at the same time, getting it to redirect to the thanks.asp page.

Using the response.redirect as you suggest it, what do you think about this (in the case of the email field, for instance):

re.Pattern = &quot;^\w+@\w+\.\w+&quot;
results = re.Test(email)
if results then
errorArray(1) = &quot;False&quot;
else
errorArray(1) = &quot;True&quot;
ErrorMsg = ErrorMsg & &quot;Please type in a valid email address<br>&quot;
else
response.redirect &quot;thanks.asp&quot;

Thanks, again ralphtrent!

Hatter









 
doesn't work either, Ralphtrent.

I tried:

re.Pattern = &quot;^\w+@\w+\.\w+&quot;
results = re.Test(email)
if results then
errorArray(1) = &quot;False&quot;
response.redirect &quot;thanks.asp&quot;
else
errorArray(1) = &quot;True&quot;
ErrorMsg = ErrorMsg & &quot;Please type in a valid email address<br>&quot;
end if

and deliberatly left out the name field. I was directed to the thanks.asp page, whereas I should have been advised to complete the name field correctly.

It's as if I can't have the redirection to the thanks.asp page AND have the redirection.

Cheers

hatter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top