Two lines of defense, one function:
How can you ensure that the client and server have the same functions? The verification of the form field flashes before our eyes. Someone copying your html to another script and then changing the form domain verification of the client - this is not a difficult task. The solution before your eyes is to place the verification of the form field on the server side. But that means that because of a small error by the user, a string of error messages must be returned to the server. So, why don’t we have both? Not only that, we can also use the same javascript function on the client and server side to ensure the complete consistency of the two.
Take a look at the following paragraph, please pay special attention to the checkMyZip() function.
The code copy is as follows:
<%@LANGUAGE="JavaScript"%>
<%
//NoASPHere, justaregularHTMLPage
%>
<HTML>
<SCRIPTLANGUAGE="JavaScript">
<!--Hide
functioncheckMyZip(zipCode)
{
varmyRegularExpression=/(^/d{5}$)|(^/d{5}-/d{4}$)/
if(myRegularExpression.test(zipCode)==true)
{
return nothingIsWrong();
}
else
{
return something like that IsWrong();
}
}
function nothingIsWrong()
{
//Donothing
returntrue
}
function something like that IsWrong()
{
alert("Somethingiswrongwiththezipcodeyouuprovided.")
document.zipCodeForm.zipCodeText.focus()
returnfalse;
}
//StopHiding-->
</SCRIPT>
<STRONG>TypeavalidU.S.Postalzipcodeintothebox,andsubmitit.</STRONG>
<FORMNAME="zipCodeForm"ACTION="script05a.asp"METHOD="Post"
onSubmit="returncheckMyZip(document.zipCodeForm.zipCodeText.value)">
<INPUTTYPE="Text"NAME="zipCodeText"><BR>
<BR>
<INPUTTYPE="Submit"VALUE="Submit">
</FORM>
</HTML>
What we see in this lesson is the biggest reward for writing asp scripts using javascript. Look at the script below, and then pay attention to the checkMyZip() function again.
The code copy is as follows:
<%@LANGUAGE="JavaScript"%>
<%
functioncheckMyZip(zipCode)
{
varmyRegularExpression=/(^/d{5}$)|(^/d{5}-/d{4}$)/