• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Validation of Zip Code - Server Side

I have inherited a server side script for Classic ASP and have to add the additional functionality to this script to allow for Validation of a Zip Code....I am a .net programmer...and am a little stuck on the syntax on this one....just need to validate a US zip code.....not sure if this will entitle some type of pattern matching..but half the battle is reconciling the existing function.  Please help:)  Entire function below minus any zip code functionality.


'AddToSaveArray2
' call addToSaveArray(array("value","table","field","dataType"),"oldValue","nameStr","min","max")
'  New save routine with server-side validation. Takes an array of 4 values (as original 
'  routine), Plus, can check to see if value has changed and is within a certain range.
'  To Do:  Pass error message back to page. Range checking. Other datatypes.
 
sub addToSaveArray2(dataArray, oldval, nameStr, min, max)
 
	if namestr = "" OR isEmpty(nameStr) then   nameStr = dataArray(2)
	If trim(dataArray(0)) <> trim(oldVal) OR (isNull(dataArray(0)) AND oldVal <> "") Then
		'--------------------------------------------------------
		Select Case trim(lcase(dataArray(3)))
			Case "text"
				dataArray(0) = trim(dataArray(0))
				if NOT isNumeric(max) OR max = "" then
					'ignore - max value not set
				elseif  len(dataArray(0)) > cInt(max) then
					dataArray(0) = left(dataArray(0),max)
					message = message & "Text in '"  & nameStr & "' shortened to max length " & max & ". "
				end if	
				'---------------------------------
				if NOT isNumeric(min) OR min = "" then 
					'ignore - max value not set
				elseif  len(trim(dataArray(0))) < cInt(min) AND trim(dataArray(0)) <> "" then
					message = message & "Text in '"  & nameStr & "' must be at least " & min & " characters long. "
					exit sub		' Value not saved.
				end if	
			Case "boolean"
				if dataArray(0) then	' is true-  do nothing
					dataArray(0) = 1
				elseif lcase(trim(dataArray(0))) <> "true"  then
					dataArray(0) = 0
				end if
				if  dataArray(0) = cBool(isBlank(oldval,0)) then
					exit sub ' no change in value
				end if
			Case "date"
				if trim(dataArray(0)) = "" then 
					dataArray(0) = NULL
				elseif not isDate(dataArray(0)) Then 		
					message = message & "Field '"  & nameStr & "' is not a valid date. " 
					dataArray(0) = NULL
				elseif 	dateDiff("d",dataArray(0),"1/1/1900" ) > 0  then
					'catch out-of-range dates
					message = message & "Field '"  & nameStr & "' must be after 1/1/1900. " 
					dataArray(0) = NULL
				elseif 	 dateDiff("d",dataArray(0),"1/1/2030" ) < 0  then
					message = message & "Field '"  & nameStr & "' must be before 1/1/2030. " 
					dataArray(0) = NULL
				else
					dataArray(0) = cDate(dataArray(0))
				end if
			Case "numeric","numeric(-)"
				dim testX : testX = trim(dataArray(0))
				'rwv "TestX1", testX
				if trim(testX) = "" OR isEmpty(testX) then 
					testX = NULL 
					dataArray(0) = NULL
				end if
				'rwv "TestX2", testX	
				'rwv "data", dataArray(0)
				if trim(lcase(dataArray(3))) = "numeric(-)" AND NOT isNull(testX)  then
					testX = replace(testX,"(","")
					testX = replace(testX,"-","")
					testX = replace(testX,")","")
				end if
				if NOT isNumeric(testX) AND NOT isNull(testX) Then 
					'rwv "TestX3", testX
					message = message & "Field '"  & nameStr & "' must be numeric. " 
					exit sub
				else
					if NOT isNumeric(min) OR min = "" then
						'ignore - min value not set
					elseif  cDbl(nullNumber(dataArray(0),0)) < cDbl(min) then
						'dataArray(0) = cDbl(min)
						message = message & "Cannot save '"  & nameStr & "' because it is below the minimum allowable value of " & min & ". "
						exit sub
					end if
					if NOT isNumeric(max) OR max = ""  then
						'ignore - min value not set
					elseif  cDbl(NullNumber(dataArray(0),0)) > cDbl(max) then
						'dataArray(0) = cDbl(max)
						message = message & "Cannot save '" & nameStr & "' because it is above the maximum allowable value of " & max & ". "
						exit sub
					end if
				end if 
				'---------------------------------------------------------------------------
				'dataArray(0) = testX		' Don't use.
				'---------------------------------------------------------------------------
			Case "permissions" 
				 If lCase(oldVal) <> "chips2" AND trim(oldVal) <> "" Then
					If UserExecSecurity < 5  AND UserSchedSecurity < 7 AND UserMktgSecurity < 7 AND UserRecruitSecurity < 7 Then
						message = message & "You do not have the correct permissions to change '"  & nameStr & "'. " 
						exit sub
					End If
				End If
			Case "acctg5" 
				 If lCase(oldVal) <> "chips2" AND trim(oldVal) <> "" Then
					If UserExecSecurity < 7 AND  UserAcctgSecurity < 5 Then
						message = message & "You do not have the correct permissions to change '"  & nameStr & "'. " 
						exit sub
					End If
				End If	
			Case "recruit7" 
				 If lCase(oldVal) <> "chips2" AND trim(oldVal) <> "" Then
					If UserExecSecurity < 7 AND  UserRecruitSecurity < 7 Then
						message = message & "You do not have the correct permissions to change '"  & nameStr & "'. " 
						exit sub
					End If
				End If
		End Select

Open in new window

0
Robb Hill
Asked:
Robb Hill
  • 7
  • 6
1 Solution
 
silemoneCommented:
well take into account that a true zip will not be 00000, but i think your office is pretty much wanting to make sure that numbers are entered...
simplest answer would be to use regex   [0-9]{5}
0
 
silemoneCommented:
i don't see the code for the zip...where is the zip stored?  a rs?
0
 
Robb HillSenior .Net DeveloperAuthor Commented:
i havent written the code for zip....this procedure is a classic asp validation control which validates by processsing the elements of the array.  So just like it is validating if a field is text or not...I think is how this code would work on a zip.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
silemoneCommented:
i was just looking at vbscript and apparently you could just do something like this:


If Zip Like "#####" Or Zip Like "#####-####" then
end If
0
 
Robb HillSenior .Net DeveloperAuthor Commented:
does that indicate placement or numeric value....also I will be implementing other international zips as well...so I think regex is the way I have to go for the flexibility....BUT
my real question here is how to implement a regex function into the Classic ASP script above.....

So part of this question...is understanding the existing function and the second is how to use regex in classic asp...I know how to do this in .net and jscript.
0
 
silemoneCommented:
well if i was you, I would first find out what that dataArray is...do it hold attributes to one field at a time?  It says it holds a table?  is that a rs table?  So first you need to find out what exactly is inside the dataArray and what type of objects they are...there aren't many comments by whoever wrote the code so, you're going to have to find these things out by maybe testing and printing them out...
0
 
Robb HillSenior .Net DeveloperAuthor Commented:
Basically I am just making a new case statement called zip....which will test the 0 element of dataarray.  That element of the array would be the value of the zip code in question.  I would want to test it against these two regex forms:

So long as one or both is true then the value can pass the test.

usZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
caZip = new RegExp(/(^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$)|(^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$)/);
0
 
silemoneCommented:
maybe this will help

<%


Set RegularExpressionObject = New RegExp

With RegularExpressionObject
.Pattern = "(^\d{5}$)|(^\d{5}-\d{4}$)"
.IgnoreCase = True
.Global = True
End With

Set expressionmatch = RegularExpressionObject.Execute(StringToSearch)

If expressionmatch.Count <= 0 Then
     'alert somehow that invalid...    
End If



stripHTMLtags = RegularExpressionObject.Replace(HTMLstring, "")
Set RegularExpressionObject = nothing

End Function
%>
0
 
Robb HillSenior .Net DeveloperAuthor Commented:
so in the above example my string to search would be = to dataArray(0)
0
 
Robb HillSenior .Net DeveloperAuthor Commented:
my solution thus far is as follows:
Case "zip"
 
                Set RegularExpressionObject = New RegExp
 
                With RegularExpressionObject
                .Pattern = "(^\d{5}$)|(^\d{5}-\d{4}$)"
                .IgnoreCase = True
                .Global = True
                End With
                StringToSearch = dataArray(0)
                Set expressionmatch = RegularExpressionObject.Execute(StringToSearch)
 
                If expressionmatch.Count <= 0 Then
                     'alert somehow that invalid...     
                End If
 
                stripHTMLtags = RegularExpressionObject.Replace(HTMLstring, "")
                Set RegularExpressionObject = nothing

Open in new window

0
 
silemoneCommented:
yes...your String Search would be dataArray(0)....
0
 
Robb HillSenior .Net DeveloperAuthor Commented:
Case "zip"

                Set RegularExpressionObject = New RegExp

                With RegularExpressionObject
                .Pattern = "(^\d{5}$)|(^\d{5}-\d{4}$)|(^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$)|(^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$)"
                .IgnoreCase = True
                .Global = True
                End With
                StringToSearch = dataArray(0)
                Set expressionmatch = RegularExpressionObject.Execute(StringToSearch)

                If expressionmatch.Count <= 0 Then
                     message = message & "Field '"  & nameStr & "'must be in US or Canadian Zip format. ex: either '00000','00000-0000' or 'V3A3N1','V3A 3N1','V3A.3N1' ."  
                     exit sub ' no change in value
                End If

                stripHTMLtags = RegularExpressionObject.Replace(HTMLstring, "")
                Set RegularExpressionObject = nothing
0
 
silemoneCommented:
good job!!!


cheers...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now