Click to See Complete Forum and Search --> : Form Validation using ASP
ncozzolino
03-30-2005, 09:30 AM
I tried searching but couldn't find what I was looking for. I need to validate a form that has about 35 fields. Each field must contain data and to save some coding time I'd do it all on the server end and use response.write to display the empty fields. I could do 35 IF statements but I hope there is a more efficient way. Any ideas??
Thanks!
lmf232s
03-30-2005, 03:55 PM
your best way to do this is with javascript IMOP, as you can validate before you post back to the server. This just saves a un-needed trip to the server.
When i started doing ASP, i did all server side validation, but have started using javascript to do the validation, IMOP its alot cleaner and again it saves a un-needed trip to the server. No reason to make a trip to the server just to display a error message.
ASP
WIth asp you could write a function to check if its valid.
(Now i just typed this up so i dont know if this code actually works, but it should give you an idea of how not to use multiple IF's.)
'AmINull(value of your field, name of the field you want to be dispalyed in the error message)
Function AmINull(obj, fieldName)
if obj <> "" then
'do nothing there is a value
else
errMsg = errMsg & filedName & " is a required field <BR>"
bError = True
end if
End Function
txtFieldName = AmINull(request.form("txtFieldName"), Customer)
txtFieldName = AmINull(request.form("txtFieldName"), FirstName)
txtFieldName = AmINull(request.form("txtFieldName"), LastName)
'after you have all your fields then something maybe like
if bError = false then
'add all your data to the datbase
else
'dispaly error message
response.write errMsg
end if
JAVASCRIPT
also check out the values, sValue, sType, sName, as sType will tell you if
its a textbox, radio, button, select, etc, so that if you need to only validate
textboxs, you could make sure that sType = "text" first.
function validate(){
var sType;
var sValue;
var sName;
var oForm = document.form;
var errMsg = "";
var bError;
for (i = 0; i < oForm.elements.length; i++){
sType = oForm.elements[i].type;
sValue = oForm.elements[i].value;
sName = oForm.elements[i].name;
if(sValue==""){
errMsg = errMsg + sName + " is a required field\n";
bError = true;
}
}
if(bError==true){
' there was a error, do not submit form, display the error.
alert(errMsg);
}else{
' all fields are filled in, lets submit the form.
document.form.submit();
}
}
phpnovice
03-30-2005, 05:45 PM
your best way to do this is with javascript IMOP, as you can validate before you post back to the server. This just saves a un-needed trip to the server.
As long as you also do the validation at the server anyway -- in case the visitor has JavaScript disabled.
phpnovice
03-30-2005, 05:53 PM
I could do 35 IF statements but I hope there is a more efficient way. Any ideas??
Yes. However, one might think that the "more efficient way" is to use an ASP/VBScript For...Each loop or an ASP/JScript for...in loop against the Request object directly.
For Each fld in Request.Forms
'
' code to validate Request.Forms(fld)
'
Next
However, keep in mind that empty fields will not be transmitted to the server. Thus, you would want to create an array of all the field names in the form and loop through that array instead.
fldary = Array("fld1", "fld2", "fld3", etc.)
For Each fld in fldary
'
If Len(Request.Forms(fld)) > 0 Then
' validate field content
Else
' inform for required field
End If
'
Next
Get it?