...

View Full Version : Form validation



pinkcat_02
02-23-2003, 09:51 PM
Hiya

I wanna have my form validated before the data goes to the database. But I have so many if...else statements where I wanna connect to the database after else.

It doesn't seem really nice using many if...then...else so is there any other way of keeping them in one if statement?

oracleguy
02-24-2003, 04:52 AM
Well you could put them all (or some) in one if...then statement.
You could do something like:

if str1="" OR len(str2)<6 Then
'Error

End If

Doing the above mentioned method you might be able to condense some of your if...then statements.

glenngv
02-24-2003, 05:31 AM
putting them inside a single IF statement would make it inefficient since all of the conditions will be checked regardless of the value of each condition (true or false). Unlike javascript where in a compound OR condition, once the first condition evaluates to true, the rest of the conditions will not be check anymore since the output of the whole condition will become true. The same is true with the compound AND condition, once the first condition evaluates to false, the rest of the conditions will not be evaluated anymore, since the whole condition will become false. But this is not the case for vbscript. In VB.NET, they introduced new (funny) operators such as OrElse and AndAlso

oracleguy
02-24-2003, 06:27 AM
Interesting glenngv. I didn't know that.

I haven't had a chance to mess around with VB .net yet... its "on the list."

pinkcat_02
02-24-2003, 11:15 AM
do u reckon I better do the validation with javascript then? I wonder what u think the advantages of using vb script or javascript in form validations?

Thanks ;)

Roy Sinclair
02-24-2003, 03:47 PM
Originally posted by pinkcat_02
do u reckon I better do the validation with javascript then? I wonder what u think the advantages of using vb script or javascript in form validations?

Thanks ;)

The advantage of doing validation with javascript is to reduce the turn around on errors, the user doesn't have to go all the way to the server to get a reply. The disadvantage is that you still need to have server side validation because some people don't have javascript enabled and you don't want garbage to get into your database (it's better to prevent it than trying to remove or deal with it). If you want to reduce the complexity of the validation try using some functions to perform repeated tasks or to handle exceptionally complex checks. You can even make the whole validation check into a function (which can call other functions) so the processing of the form isn't cluttered with the validation code.

oracleguy
02-24-2003, 05:46 PM
You also could put your validation code into an include to help reduce clutter on form processing part of the page.

pinkcat_02
02-24-2003, 10:10 PM
Can u please give me an example of what you have suggested oracleguy? I think it will make it more clear.

Thanks

pinkcat_02
02-24-2003, 11:19 PM
<%
Session("FName")=Request("FirstName")
Session("LName")=Request("LastName")
Session("CompletionDate")=Request("SendDate")

'HERE IS ONE WAY OF CHECKING FOR AN EMPTY TEXT BOX
if not len(Request("FirstName")) > 0 then
Session("badFirstName")="T"
Session("Errors")=Session("Errors") + 1
end if

'AND HERE IS ANOTHER, BOTH SHOULD WORK
if Request("LastName")= "" then
Session("badLastName")="T"
Session("Errors")=Session("Errors") + 1
end if


if not IsDate(Request("SendDate")) then
Session("badDate")="T"
Session("Errors")=Session("Errors") + 1
end if


if Session("Errors") > 0 then
‘there were errors, so send back to form
response.redirect "form.asp"
else
'there were no errors, so do the update to the database and redirect to a thank you page
response.redirect "thanks.asp"
end if

%>

I am using the code above for the time being because in the form.asp I would like to tell the user exactly the field that has not been completed rather than saying the form is not completed.
But still I am not happy with it as I see many if else statement.

The other thing I would like to ask is how can I check for the valid UK phonenumbers, postcodes and also valid form of emails?

One more thing I also have a drop down menu with options that the user should choose one of those. At the moment I haven't done any validations for those. How can I do it any suggestions or do I really have to do it?

Last thing one the user submitted the form I don't want the data to be reset that the user has already entered, rather than that I would like to keep the data that the user has already entered and give the error message for them to complete the form.

Thanks very much.

oracleguy
02-25-2003, 01:07 AM
Well what I was saying is you could do something like this.

In validation.inc: Put all your form validation stuff here.


On the page with the validation.



<!--#include file="validation.inc"-->

<%
...process the form...
%>


The method I was suggesting was just to move the code out of the file, so when your working on it, it isn't as cluttered. You'd probably want to do it in combination with the method Roy suggested.

pinkcat_02
02-25-2003, 01:18 AM
Thanks a lot for the help.
Do you have any ideas of doing other validations I have mentioned above?

Thanks

whammy
02-25-2003, 02:17 AM
I guess I must be doing something right with VBScript then, I haven't run into any of the problems you mentioned, Glenn. Probably because I started with JavaScript?

Either that or I haven't run into any of the illogical errors you describe ?!?

Do you have an example?

whammy
02-25-2003, 02:21 AM
P.S. pinkcat_02, I don't know what the validation criteria for UK postcodes or phonenumbers are, but if you can provide me with the exact criteria, I will attempt to write a regular expression validation function for them.

As for email... I use this:

Function IsEmail(str)
Dim ieRegEx
Set ieRegEx = New RegExp
ieRegEx.Pattern = "^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z0-9]{2,}$"
IsEmail = ieRegEx.Test(str)
End Function

It seems to work ok for now, but it is somewhat loose and most likely not absolutely technically correct, since it appears there are other characters that "may" appear before the @ sign, however with this regex I haven't run into them, so if that's the case they must be VERY rare.

If someone were able to translate the appropriate RFC into human-readable English, I could do better, but apparently noone in the world is capable of doing so (and I can't make heads or tails of it!). :)

pinkcat_02
02-25-2003, 12:12 PM
I didn't put my validation in a function as I am not very sure how to do those so instead I used many if else statements so I have tried to put the function you have advised in a if else statement like:

if not IsCustomer_Email(Request("Customer_Email"))
Dim ieRegEx
Set ieRegEx = New RegExp
ieRegEx.Pattern = "^[\w\+'\.-]+@[\w'\.-]+\.[a-zA-Z0-9]{2,}$"
IsEmail = ieRegEx.Test(str)

then

Session("badCustomer_Email")="T"
Session("Errors")=Session("Errors") + 1
end if

But it is not working that I am sure it might not be the way I have done it. How am I supposed to do that?

Thanks

pinkcat_02
02-25-2003, 12:32 PM
oh ignore my stupidness please I have sorted it out. For the UKpostcode and phonenumber maybe I can try them out myself first and get back to you if I get stuck but I just wonder where I can find a tutorial of writing expression like the format you did? I better learn and be able to write it myself :)

Thanks

whammy
02-25-2003, 11:32 PM
As for using it from a function like I wrote, it's just like using a function that's built-in, i.e.:

If NOT IsEmail(email) Then
'do soemthing
End If

Since the function returns a boolean value (true or false).

As for learning regular expressions, here are some good links, the first two are in javascript but the syntax is pretty much the same:

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/regexp.html
http://www.siteexperts.com/tips/functions/ts23/page1.asp

http://www.4guysfromrolla.com/webtech/RegularExpressions.shtml

:)

pinkcat_02
02-26-2003, 02:07 AM
I have used your funstion but when I test it with whatever value it gives error :(

Function IsMail(str)
Dim ieRegEx
Set ieRegEx = New RegExp
ieRegEx.Pattern = "^[\w\+'\.-]+@[\w'\.-]+\.[a-zA-Z0-9]{2,}$"
IsEmail = ieRegEx.Test(str)
End Function

if NOT IsMail(Request("Email")) then

Session("badCustomer_Email")="T"
Session("Errors")=Session("Errors") + 1
end if


I used like this but even I test it with abc@xxx.com or with any other possible right values it says invalid to all of them. I couldn't find where I have done a mistake :(

whammy
02-26-2003, 02:12 AM
You renamed the function IsMail, but didn't change the function's return value to IsMail. It still reads IsEmail, instead.

I would suggest testing the working version before renaming it or experimenting with it. ;)

glenngv
02-26-2003, 02:42 AM
Originally posted by whammy
I guess I must be doing something right with VBScript then, I haven't run into any of the problems you mentioned, Glenn. Probably because I started with JavaScript?

Either that or I haven't run into any of the illogical errors you describe ?!?

Do you have an example?

<%
Function func1(flag)
response.write "inside func1<br>"
func1 = flag
End Function

Function func2(flag)
response.write "inside func2<br>"
func2 = flag
End Function

response.write "false AND true condition:<br>"
if func1(false) and func2(true) then
response.write "inside AND<br>"
else
response.write "inside else (AND)<br>"
end if

response.write "<br>true OR false condition:<br>"
if func1(true) or func2(false) then
response.write "inside OR<br>"
else
response.write "inside else (OR)<br>"
end if
%>

whammy
02-26-2003, 02:44 AM
Huh?

lol

pinkcat_02
02-26-2003, 03:00 AM
cheers whammy I have fixed that and I also add the format for UK pnonenumber. I have seen another thread of yours in Javascript section about getting rid of all the gaps and - while inseting those to the database which I find useful I wanna do it as well

Where shall i include this code?

enRegEx.Pattern = "\D"
enRegEx.Global = True
ExtractNumbers = enRegEx.Replace(str,"")


Thanks

glenngv
02-26-2003, 03:14 AM
Originally posted by whammy
Huh?

lol

is that for me whammy?

i just illustrated vbscript's behavior on OR/AND conditions, as I described in my first post (http://www.codingforums.com/showthread.php?s=&threadid=15192&perpage=15&pagenumber=1#post74698). if you convert it to javascript, you will see the difference.

whammy
02-27-2003, 12:03 AM
Glenn, will do when and if I get time, but I must be doing something right since I don't run into the problems mentioned...

As for the ExtractNumbers function pinkcat is referring to, just do this when you insert into the database to get rid of anything but numbers:

<%
Function ExtractNumbers(byVal str)
If IsNull(str) Then str = ""
Dim enRegEx
Set enRegEx = New RegExp
enRegEx.Pattern = "\D"
enRegEx.Global = True
ExtractNumbers = enRegEx.Replace(str,"")
End Function

Then, on an insert or whatever...

mySQL = "INSERT INTO tablename (myvar) VALUES ('" & ExtractNumbers(myvar) * "')"
%>

When you create a function such as this one, you can easily test what it returns by just writing it out:

<% = ExtractNumbers("123dfsdf456") %>

Would output:

123456

:)

The IsWhatever() functions that I use should generally return a boolean value... check these out:

http://www.solidscripts.com/downloads/functions.txt

P.S. Glenn, from a cursory examination of that, you'd never get to the else since the first condition would always return true... right?

whammy
02-27-2003, 12:14 AM
Hmm, I see what you're saying from testing it, but to me the way you did it doesn't make sense to me... of course I just got off of a long day's work where at the end I was learning .NET concepts, so my brain might be a little fried, especially trying to see the difference between this and javascript syntax. :)

I fixed it to work easily, like this:



<%
Function function1(flag)
response.write "inside func1<br>"
function1 = flag
End Function

Function function2(flag)
response.write "inside func2<br>"
function2 = flag
End Function

response.write "false AND true condition:<br>"
if function1(false) = false and function1(true) = false then
response.write "inside AND<br>"
else
response.write "inside else (AND)<br>"
end if

response.write "<br>true OR false condition:<br>"
if function1(true) = true or function2(false) = true then
response.write "inside OR<br>"
else
response.write "inside else (OR)<br>"
end if
%>


Of course I'm used to thinking differently when using javascript and VBScript, maybe that's why I'm not experiencing problems like that... oh well :confused:

glenngv
02-27-2003, 01:57 AM
I made the function in such a way that whatever boolean value you pass as the parameter, it will be the function's return value. I do this for simplicity's sake and just for illustration. Try this simple test and see the different paths taken by vbscript and javascript. Im not pointing out a problem, just the difference between vbscript's and javascript's execution of AND/OR statements



<%
Function func1(flag)
response.write "inside func1<br>" & VbCrLf
func1 = flag
End Function

Function func2(flag)
response.write "inside func2<br>" & VbCrLf
func2 = flag
End Function

%>
<html>
<body>
<%
response.write "<h3>VBScript:</h3>" & VbCrLf
response.write "<b>false AND true condition:</b><br>" & VbCrLf
if func1(false) and func2(true) then
response.write "inside AND<br>" & VbCrLf
else
response.write "inside else (AND)<br>" & VbCrLf
end if

response.write "<br><b>true OR false condition:</b><br>" & VbCrLf
if func1(true) or func2(false) then
response.write "inside OR<br>" & VbCrLf
else
response.write "inside else (OR)<br>" & VbCrLf
end if
%>
<script language="javascript">
function func1(flag){
document.write("inside func1<br>");
return flag;
}

function func2(flag){
document.write("inside func2<br>");
return flag;
}

document.write("<h3>JavaScript:</h3>");

document.write("<b>false AND true condition:</b><br>");
if (func1(false) && func2(true))
document.write("inside AND<br>");
else
document.write("inside else (AND)<br>");

document.write("<br><b>true OR false condition:</b><br>");
if (func1(true) || func2(false))
document.write("inside OR<br>");
else
document.write("inside else (OR)<br>");

document.close()
</script>
</body>
</html>


This is the output:


VBScript:
false AND true condition:
inside func1
inside func2
inside else (AND)

true OR false condition:
inside func1
inside func2
inside OR

JavaScript:
false AND true condition:
inside func1
inside else (AND)

true OR false condition:
inside func1
inside OR

pinkcat_02
02-27-2003, 03:45 PM
I also validate if the user has selected any other value than the first one in the drop down menu. I have seen your code below

Function IsSelected(val1,val2)
If val1 = val2 Then IsSelected = " selected=""selected"""
End Function

but it is not the one I am looking for I want conversion of this javascript to ASP or any other solution would do:

<SCRIPT LANGUAGE="JavaScript"'>
<!--
function validateForm(){
if(document.ItemList.Item.selectedIndex==0)
then
....
</script>


Thanks

whammy
02-27-2003, 11:51 PM
If Len(Request.Form("Item")) = 0 Then
' Nothing was selected
End If

pinkcat_02
02-28-2003, 12:37 PM
doh! Stupid me, it was so easy...cheeers whammy, i think i have sorted out all my form validations for now.

Thanks again:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum