Forums
This topic is locked
Add-on serverside validation?
Posted 22 Apr 2003 23:21:44
1
has voted
22 Apr 2003 23:21:44 Jelle-Jacob de Vries posted:
Does anybody know how to add a function to the serverside validation tutorial in the "latest tutorial" section of this site, that checks a new username?Replies
Replied 23 Apr 2003 10:38:55
23 Apr 2003 10:38:55 Jelle-Jacob de Vries replied:
This is the validation code:
<%
' Validating form fields using server side scripting
' Function RegExpCheck: this function checks the string with a regular expression
' it will return "falso" if the string doesn't pass the validation and "vero" if it does pass.
' if the string is empty it will return "errore"
Function RegExpCheck(theExpression,theString)
dim theMatch , testo
If theString = "" Then
testo = "errore"
Else
Set loRegExp = New RegExp
loRegExp.Global = True
loRegExp.IgnoreCase = True
loRegExp.Pattern = theExpression
set theMatch = loRegExp.Execute(theString)
RegExpCheck = theMatch.count
If theMatch.count = 0 Then
testo = "falso"
Else
testo = "vero"
'Else
' RegExpCheck = -1
End If
End If
RegExpCheck = testo
End Function
alfarefer = Request.ServerVariables("HTTP_REFERER"
alfaserver = Request.ServerVariables("SERVER_NAME"
If Instr(alfarefer, alfaserver) <> 0 then 'checking if the form is submitted from this server
If Request.Form.Count <> 0 then ' checking if the form is submitted or it's the first time the page is loading
alfaErrMsg = "" ' Initializing the Error Message Variable
alfaChkMM = 1 ' this is the submit button that we don't need to check on
If (Request.Form("MM_insert"
<> ""
then
alfaChkMM = 2 ' this is the hidden field that UD adds when you apply an Insert Record SB
End If
If (Request.Form("MM_update"
<> ""
then
alfaChkMM = 3 ' in this case Ultradev adds 2 hidden fields ( MM_update, MM_recordID )
End If
alfaCount = Request.Form.Count - alfaChkMM ' Number of fields that was submitted - the fields we don't need to check
For alfaChk = 1 to alfaCount Step 2
alfaField = Request.Form.Key(alfaChk) ' this is the name of the field we're checking
alfaValue = Request.Form.Item(alfaChk) ' this is the value of the field we're checking
alfaCase = Request.Form.Key(alfaChk+1) ' this is the name of the next field in the form
alfaErrAdd = Request.Form.Item(alfaChk+1) ' this is the value of the next field in the form
' Checking which type of validation is needed for this field
' according to the next field name if the next field name meets
' the standerd we made for the added hidden fields that we added directly
' after the field we want to check
' for example: myfieldFILL = "myfield" must NOT be empty
' 6 AZLNTHNRQ -> if something is inserted the field must be only A-Z characters and it must not be less than x
' 3 LNTHNRQ -> if something is inserted it must not be less than x characters
' 2 LNTH -> it must not be less than x chars
' 1 FILL -> something must be inserted
' 5 AZNRQ -> if something is inserted the field must be only A-Z
' 4 AZ -> required and it must be only A-Z
' 8 AZ09NRQ -> if something is inserted it must be alfanumeric only (letters and/or numbers)
' 7 AZ09 -> required and it must be alfanumeric only (letters and/or numbers)
' 10 NUMBNRQ -> if something is inserted it must be numeric only
' 9 NUMB -> required and must be numeric only
' 12 DATENRQ -> if something is inserted it must be a date
' 11 DATE -> required and must be a date
' 14 TIMENRQ -> if something is inserted it must be a valid time
' 13 TIME -> required and it must be a valid time
' 19 CNFM -> the two fields must be equal (password field and confirmation <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> )
' 18 URLNRQ -> if something is inserted it must be a valid URL
' 17 URL -> required and it must be a valid URL
' 16 ADRSNRQ -> if something is inserted it must be an email address
' 15 ADRS -> required and must be a valid (in the syntax) email address
' AZLNTHNRQ nota: la stringa non è obbligatoria. Se inserita deve -> essere più di X caratteri e contenere SOLO lettere
' AZ = "dalla A alla Z" , LNTH = "controlla la lunghezza" , NRQ = "Not ReQuired"
' è un caso molto complesso che raramente serve...è più utile verificare se ci sono solo lettere
' ad esempio per nomi e cognomi (in fondo uno/a può anche avere un nome cortissimo <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle> )
' AZLNTHNRQ note: the string is optional. If something is inserted it must be more than X characters and contain only alphabetical chars
' AZ = from A to Z - LNTH = Lenght - NRQ = Not ReQuired
' I know it is a very complex case that will rarely be useful. Of course it's more useful to verify if there are only
' alphabetical chars (example: first or last name ) . One person can have a *very* short name , isn't it? <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>
' After each check, if there's an error it gets concatenated with the others and in the end you get a semicolon separated string
' the values separated are the names of the fields that didn't pass the validation.
' es: name;email;telephone = "the fields name , email and telephone didn't pass the validation"
' This is an ARRAY and in you can use the SPLIT function to get the single fields back and to do something with the field name.
' For example show or hide a region (an error message?) in some part of the page
' The function FindError provided in the tutorial can be used for this purpose
Select Case alfaCase
Case alfaField&"AZLNTHNRQ"
If (((alfaValue <> ""
) AND (Len(alfaValue) < CInt(alfaErrAdd))) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else If ((alfaValue <> ""
) AND ((RegExpCheck("^[a-zA-Z]+$",alfaValue)) = "falso" ) Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
End If
Case alfaField&"LNTHNRQ"
If (((alfaValue <> ""
) AND (Len(alfaValue) < CInt(alfaErrAdd))) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"LNTH"
If ((Len(alfaValue) < CInt(alfaErrAdd))) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"FILL"
If alfaValue = "" then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZNRQ"
If ((alfaValue <> ""
) AND ((RegExpCheck("^[a-zA-Z]+$",alfaValue)) = "falso" ) Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZ"
If (RegExpCheck("^[a-zA-Z]+$",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZ09NRQ"
If ((alfaValue <> ""
) AND ((RegExpCheck("^[a-zA-Z0-9]+$",alfaValue)) = "falso" ) Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZ09"
If (RegExpCheck("^[a-zA-Z0-9]+$",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"NUMBNRQ"
If (((alfaValue <> ""
) AND (NOT IsNumeric(alfaValue))) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"NUMB"
If NOT IsNumeric(alfaValue) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"DATENRQ"
If (((alfaValue <> ""
) AND (NOT IsDate(alfaValue))) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"DATE"
If NOT IsDate(alfaValue) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"TIMENRQ"
If ((alfaValue <> ""
) AND ((RegExpCheck("^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$",alfaValue)) = "falso"
Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"TIME"
If (RegExpCheck("^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"CNFM"
If alfaValue <> Request.Form.Item(alfaChk+2) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
alfaChk = alfaChk + 1 ' Skipping the confirmation field from the validation
Else
alfaChk = alfaChk + 1 ' Skipping the confirmation field from the validation
End If
Case alfaField&"URLNRQ"
If ((alfaValue <> ""
) AND ((RegExpCheck("^http:\/\/\w[\w\-]*(\.[a-zA-Z0-9][\w\-]*)*(\/[\w\.]+)*\/?",alfaValue)) = "falso"
Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"URL"
If (RegExpCheck("^http:\/\/\w[\w\-]*(\.[a-zA-Z0-9][\w\-]*)*(\/[\w\.]+)*\/?",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"ADRSNRQ"
If alfaValue <> "" Then
If InStr(alfaValue, " "
<> 0 then
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
alfaAtPos = InStr(alfaValue, "@"
If ((alfaAtPos = 1) OR (alfaAtPos = 0)) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
If InStr(alfaAtPos, alfaValue, "."
= 0 then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
End If
End If
End If
Case alfaField&"ADRS"
If InStr(alfaValue, " "
<> 0 then
'alfaErrMsg = alfaErrMsg & alfaErrAdd & "<BR>"
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
alfaAtPos = InStr(alfaValue, "@"
If ((alfaAtPos = 1) OR (alfaAtPos = 0)) then
'alfaErrMsg = alfaErrMsg & alfaErrAdd & "<BR>"
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
If InStr(alfaAtPos, alfaValue, "."
= 0 then
'alfaErrMsg = alfaErrMsg & alfaErrAdd & "<BR>"
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
End If
End If
If alfaErrMsg <> "" Then
alfaErrMsg = Right(alfaErrMsg,(Len(alfaErrMsg)-1))
Else
alfaErrMsg = ""
End If
' if all the cases were not satisfied then the name of the next field
' is not matching with the naming standards we set for the hidden fields
' so the next field is not one of the hidden fields we added
' thus we have to decrease the alfaChk variable by 1 so when the Next
' steps 2 it still includes the next field in the checking
Case Else
alfaChk = alfaChk - 1
End Select
Next
alfaproceed = "true"
If alfaErrMsg <> "" then
alfaproceed = ""
End If
End If ' the if Request.Form.Count end
Else
alfaproceed = ""
End IF
%>
<%
' function FindError
' checks in the AlfaErrMsg string to find the name of the checked fields and if it finds
' a field it returns "True" , otherwise returns "False". So to make an error message appear
' you can put a conditional region in the page that will show only if the condition
' If (FindError(AlfaErrMsg, 'FieldToCheck') = "True"
' where 'FieldToCheck' is the name of the field to show the error message for.
Dim MyVar , MyField , r
Function FindError(MyVar , MyField)
MyArray = Split(MyVar , ";" , -1 , 1)
For r = 0 to UBound(MyArray)
If MyArray(r) = MyField Then
FindError = "True" : Exit Function
Else
FindError = "False"
End If
Next
End Function
%>
<%
' Validating form fields using server side scripting
' Function RegExpCheck: this function checks the string with a regular expression
' it will return "falso" if the string doesn't pass the validation and "vero" if it does pass.
' if the string is empty it will return "errore"
Function RegExpCheck(theExpression,theString)
dim theMatch , testo
If theString = "" Then
testo = "errore"
Else
Set loRegExp = New RegExp
loRegExp.Global = True
loRegExp.IgnoreCase = True
loRegExp.Pattern = theExpression
set theMatch = loRegExp.Execute(theString)
RegExpCheck = theMatch.count
If theMatch.count = 0 Then
testo = "falso"
Else
testo = "vero"
'Else
' RegExpCheck = -1
End If
End If
RegExpCheck = testo
End Function
alfarefer = Request.ServerVariables("HTTP_REFERER"

alfaserver = Request.ServerVariables("SERVER_NAME"

If Instr(alfarefer, alfaserver) <> 0 then 'checking if the form is submitted from this server
If Request.Form.Count <> 0 then ' checking if the form is submitted or it's the first time the page is loading
alfaErrMsg = "" ' Initializing the Error Message Variable
alfaChkMM = 1 ' this is the submit button that we don't need to check on
If (Request.Form("MM_insert"


alfaChkMM = 2 ' this is the hidden field that UD adds when you apply an Insert Record SB
End If
If (Request.Form("MM_update"


alfaChkMM = 3 ' in this case Ultradev adds 2 hidden fields ( MM_update, MM_recordID )
End If
alfaCount = Request.Form.Count - alfaChkMM ' Number of fields that was submitted - the fields we don't need to check
For alfaChk = 1 to alfaCount Step 2
alfaField = Request.Form.Key(alfaChk) ' this is the name of the field we're checking
alfaValue = Request.Form.Item(alfaChk) ' this is the value of the field we're checking
alfaCase = Request.Form.Key(alfaChk+1) ' this is the name of the next field in the form
alfaErrAdd = Request.Form.Item(alfaChk+1) ' this is the value of the next field in the form
' Checking which type of validation is needed for this field
' according to the next field name if the next field name meets
' the standerd we made for the added hidden fields that we added directly
' after the field we want to check
' for example: myfieldFILL = "myfield" must NOT be empty
' 6 AZLNTHNRQ -> if something is inserted the field must be only A-Z characters and it must not be less than x
' 3 LNTHNRQ -> if something is inserted it must not be less than x characters
' 2 LNTH -> it must not be less than x chars
' 1 FILL -> something must be inserted
' 5 AZNRQ -> if something is inserted the field must be only A-Z
' 4 AZ -> required and it must be only A-Z
' 8 AZ09NRQ -> if something is inserted it must be alfanumeric only (letters and/or numbers)
' 7 AZ09 -> required and it must be alfanumeric only (letters and/or numbers)
' 10 NUMBNRQ -> if something is inserted it must be numeric only
' 9 NUMB -> required and must be numeric only
' 12 DATENRQ -> if something is inserted it must be a date
' 11 DATE -> required and must be a date
' 14 TIMENRQ -> if something is inserted it must be a valid time
' 13 TIME -> required and it must be a valid time
' 19 CNFM -> the two fields must be equal (password field and confirmation <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> )
' 18 URLNRQ -> if something is inserted it must be a valid URL
' 17 URL -> required and it must be a valid URL
' 16 ADRSNRQ -> if something is inserted it must be an email address
' 15 ADRS -> required and must be a valid (in the syntax) email address
' AZLNTHNRQ nota: la stringa non è obbligatoria. Se inserita deve -> essere più di X caratteri e contenere SOLO lettere
' AZ = "dalla A alla Z" , LNTH = "controlla la lunghezza" , NRQ = "Not ReQuired"
' è un caso molto complesso che raramente serve...è più utile verificare se ci sono solo lettere
' ad esempio per nomi e cognomi (in fondo uno/a può anche avere un nome cortissimo <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle> )
' AZLNTHNRQ note: the string is optional. If something is inserted it must be more than X characters and contain only alphabetical chars
' AZ = from A to Z - LNTH = Lenght - NRQ = Not ReQuired
' I know it is a very complex case that will rarely be useful. Of course it's more useful to verify if there are only
' alphabetical chars (example: first or last name ) . One person can have a *very* short name , isn't it? <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>
' After each check, if there's an error it gets concatenated with the others and in the end you get a semicolon separated string
' the values separated are the names of the fields that didn't pass the validation.
' es: name;email;telephone = "the fields name , email and telephone didn't pass the validation"
' This is an ARRAY and in you can use the SPLIT function to get the single fields back and to do something with the field name.
' For example show or hide a region (an error message?) in some part of the page
' The function FindError provided in the tutorial can be used for this purpose
Select Case alfaCase
Case alfaField&"AZLNTHNRQ"
If (((alfaValue <> ""

alfaErrMsg = alfaErrMsg & ";" & alfaField
Else If ((alfaValue <> ""

alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
End If
Case alfaField&"LNTHNRQ"
If (((alfaValue <> ""

alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"LNTH"
If ((Len(alfaValue) < CInt(alfaErrAdd))) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"FILL"
If alfaValue = "" then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZNRQ"
If ((alfaValue <> ""

alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZ"
If (RegExpCheck("^[a-zA-Z]+$",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZ09NRQ"
If ((alfaValue <> ""

alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"AZ09"
If (RegExpCheck("^[a-zA-Z0-9]+$",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"NUMBNRQ"
If (((alfaValue <> ""

alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"NUMB"
If NOT IsNumeric(alfaValue) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"DATENRQ"
If (((alfaValue <> ""

alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"DATE"
If NOT IsDate(alfaValue) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"TIMENRQ"
If ((alfaValue <> ""


alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"TIME"
If (RegExpCheck("^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"CNFM"
If alfaValue <> Request.Form.Item(alfaChk+2) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
alfaChk = alfaChk + 1 ' Skipping the confirmation field from the validation
Else
alfaChk = alfaChk + 1 ' Skipping the confirmation field from the validation
End If
Case alfaField&"URLNRQ"
If ((alfaValue <> ""


alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"URL"
If (RegExpCheck("^http:\/\/\w[\w\-]*(\.[a-zA-Z0-9][\w\-]*)*(\/[\w\.]+)*\/?",alfaValue)) <> "vero" Then
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
Case alfaField&"ADRSNRQ"
If alfaValue <> "" Then
If InStr(alfaValue, " "

alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
alfaAtPos = InStr(alfaValue, "@"

If ((alfaAtPos = 1) OR (alfaAtPos = 0)) then
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
If InStr(alfaAtPos, alfaValue, "."

alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
End If
End If
End If
Case alfaField&"ADRS"
If InStr(alfaValue, " "

'alfaErrMsg = alfaErrMsg & alfaErrAdd & "<BR>"
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
alfaAtPos = InStr(alfaValue, "@"

If ((alfaAtPos = 1) OR (alfaAtPos = 0)) then
'alfaErrMsg = alfaErrMsg & alfaErrAdd & "<BR>"
alfaErrMsg = alfaErrMsg & ";" & alfaField
Else
If InStr(alfaAtPos, alfaValue, "."

'alfaErrMsg = alfaErrMsg & alfaErrAdd & "<BR>"
alfaErrMsg = alfaErrMsg & ";" & alfaField
End If
End If
End If
If alfaErrMsg <> "" Then
alfaErrMsg = Right(alfaErrMsg,(Len(alfaErrMsg)-1))
Else
alfaErrMsg = ""
End If
' if all the cases were not satisfied then the name of the next field
' is not matching with the naming standards we set for the hidden fields
' so the next field is not one of the hidden fields we added
' thus we have to decrease the alfaChk variable by 1 so when the Next
' steps 2 it still includes the next field in the checking
Case Else
alfaChk = alfaChk - 1
End Select
Next
alfaproceed = "true"
If alfaErrMsg <> "" then
alfaproceed = ""
End If
End If ' the if Request.Form.Count end
Else
alfaproceed = ""
End IF
%>
<%
' function FindError
' checks in the AlfaErrMsg string to find the name of the checked fields and if it finds
' a field it returns "True" , otherwise returns "False". So to make an error message appear
' you can put a conditional region in the page that will show only if the condition
' If (FindError(AlfaErrMsg, 'FieldToCheck') = "True"
' where 'FieldToCheck' is the name of the field to show the error message for.
Dim MyVar , MyField , r
Function FindError(MyVar , MyField)
MyArray = Split(MyVar , ";" , -1 , 1)
For r = 0 to UBound(MyArray)
If MyArray(r) = MyField Then
FindError = "True" : Exit Function
Else
FindError = "False"
End If
Next
End Function
%>