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!

Data validation via vbscript 1

Status
Not open for further replies.

Terpsfan

Programmer
Dec 8, 2000
954
US
I know in vb you can loop through all the controls on a form and check to see if none are null, before trying to update the database, but not sure how this could be done via vbscript or html. In vb it goes like this:

Dim ctl as Control
For Each ctl in Form.controls
If TypeOf ctl Is TextBox Then
If IsNull(ctl) Then
MsgBox "Enter data here"
ctl.SetFocus
End If
End If
Next ctl
 
you ahve two options really here. you can do a loop after the form is submitted and write all null or "" objects to the screen
example something like
Dim obj
For Each obj In Request.Querystring
If Request.QueryString(obj) <> &quot;&quot; Then
'Do something
End If
Next

or much easier you can keep it client side with a simple function after the user submits the form
example in javascript
<html>
<head>
<script>
function validate(){
frmLength = document.frm.length;
var errorHandle = false;
var fldName = &quot;The following form fields must be entered\n&quot;;
for (i=0; i<frmLength;i++){
if(document.frm.value == &quot;&quot;)
fldName += document.frm.name + &quot;\n&quot;
errorHandle = true;
}

if(errorHandle) {
alert(fldName);
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<form name=&quot;frm&quot; onSubmit=&quot;validate()&quot;>
<input type=&quot;text&quot; name=&quot;txt1&quot;>
<input type=&quot;text&quot; name=&quot;txt2&quot;>
<input type=&quot;text&quot; name=&quot;txt3&quot;>
<input type=&quot;text&quot; name=&quot;txt4&quot;>
<input type=&quot;text&quot; name=&quot;txt5&quot;>
<input type=&quot;text&quot; name=&quot;txt6&quot;>
<input type=&quot;submit&quot; value=&quot;submit&quot;>
</form>
</body>
</html> I dare to learn more
admin@onpntwebdesigns.com
 
not only did the italics bandit get me I forgot the return in the call event
change the form tag to read
onSubmit=&quot;return validate()&quot;
and this is the code without the italics in there (sorry about that)
<script language=&quot;javascript&quot;>
function validate(){
frmLength = document.frm.length;
errorHandle = false;
var fldName = &quot;The following form fields must be entered\n&quot;;
for (i=0; i<frmLength;i++){
if(document.frm.value == &quot;&quot;) {
fldName += document.frm.name + &quot;\n&quot;
errorHandle = true;
}
}
if(errorHandle) {
alert(fldName);
return false;
}
else
{
alert(&quot;the form was found error free&quot;);
return true;
}
}

</script> I dare to learn more
admin@onpntwebdesigns.com
 
Even if you do your checking client side, which is a great idea to keep round trips to the server down, you should also conduct the same validation on the server. Some browsers don't support JavaScript and even those that do, the user can turn it off.
Thanks,

Gabe
 
Also i wanted to add, I see you have a msgbox in your VB code and incase you try to incorpurate this into your vbscript via server side (ASP) validating you cannot use this function. Unfortunetly alerts and msgbox's are not available to us in ASP. There are ways around it with the use of popups etc.. so don't be discouraged by it. Personaly always thought a well formatted popup is always more appealing to the user and developer then a simple alert I dare to learn more
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top