ASP ScriptingJava ScriptingCGI ScriptingHTMLJavaLanguage CERP Education Links

ASP SCRIPTING

Validation in ASP

function validdate(d)
Dim name
Dim conditions
Dim value
Dim subtype
Dim minVal
Dim maxVal
Dim allowNull
Dim i
Dim typeString
Dim validChars
validate=vbNullString
For Each name In d
conditions=d(name)
subtype=conditions(1)
minVal=conditions(2)
maxVal=conditions(3)
validChars = conditions(4) 
allowNull = conditions(5) 
If Not allowNull Then
If conditions(0) = vbNullString Then
validate = "<font color='#ff0000'>
You must " & "enter a name. </font><BR>"
Exit Function 
End If
End If
On Error Resume Next 
Select Case subtype 
Case vbInteger
typeString = "Integer" 
value = CInt(conditions(0)) 
If IsNumeric(minVal) Then
If value < minVal Then
validate = "<font color='#ff0000'>" & name & " must be at least " & minVal & ".</font><BR>"
Exit Function 
End If
End If
If IsNumeric(maxVal) Then 
If value > maxVal Then
validate = "<font color='#ff0000'>" & name & " may be at most " & minVaT & ".</font><BR>" 
Exit Function
End If 
End If
d.Item(name) = value 
Case vbLong
typeString = "Long"
value = CLng(conditions(0)) 
If IsNumeric(minVal) Then 
If value < minVal Then
validate = "<font color='#ff0000'>" & name & " must be at least " & minVal & ".</font><BR>" 
Exit Function
End If
End If
If IsNumeric(maxVal) Then 
If value > maxVal Then
validate = "<font color='#ff0000'>" & name & " may be at most " & minVal & ".</font><BR>" 
Exit Function
End If 
End If
d.Item(name) = value 
Case vbString
typeString = "String"
value = CStr(conditions(0)) 
If Len(value) < minVal Then
validate = "<font color='#ff0000'>" & name & " must be at least " & minVal & " characters long.</font><BR>" 
Exit Function
ElseIf Len(value) > maxVal Then
validate = "<font color='#ff0000'>" & name &  " may only be " & maxVal & " characters long.</font><BR>" 
Exit Function
End If
If Len(validChars) > 0 Then 
For i = 1 To Len(value)
If InStr(validChars, Mid(value, i, 1)) = 0 Then 
validate = "<font color='#ff0000'>" & name & " contains invalid characters.</font><BR>" 
Exit Function
End If 
Next
End If
d.Item(name) = value 
Case vbDate
typeString = "Date"
If Not IsDate(conditions(0)) Then
validate = "<font color='#ff0000'>" & name & " is not a valid date.</font><BR>"
Exit Function 
End If
value = CDate(conditions(0)) 
If IsDate(minVal) Then
If value < minVal Then
validate = "<font color='#ff0000'>" & name & " must be at least " & minVal & ".</font><BR>" 
Exit Function
End If 
End If
If IsDate(maxVal) Then 
If value > maxVal Then
validate = "<font color='#ff0000'>" & name & " may be at most " & minVal & ".</font><BR>" 
Exit Function
End If
End If
d.Item(name) = value 
Case Else
Err.Raise 50000, "validate","Unhandled variable type in validate function." 
End Select
If Err.Number <> 0 Or VarType(value) <> subtype Then
validate = "<font color='#ff0000'>Invalid value-" & name & ". Expected a " & typeString & " value.<BR></font>" 
Exit Function
ElseIf typeString = "String" And Len(value) < minVal Then
validate = "<font color='#ff0000'>" & name & " must be at least " & minVal & " characters long.</font><BR>" 
Exit Function
ElseIf typeString = "String" And Len(value) > maxVal Then
validate = "<font color='#ff0000'>" & name & " may only be " & maxVal & " characters long.</font><BR>" 
Exit Function
End If
Next
End Function

 


Your Ad Here
Not All Of Your Subscribers Use RSS - AWeber Email Marketing
Your Ad Here