Two lines of defense, a function:
How can you ensure that the client and server side have the same functionality? Validation of form fields flashed before our eyes. People copy your HTML to another script, and then change the client's form field validation--it's not a difficult thing to do. The workaround is to place the validation of the form field on the server side. But that also means that because of a small error from the user, the server will have to return a string of error messages. So why don't we have both? Not only that, we can also use the same JavaScript function on both the client and server side to ensure complete consistency.
Take a look at this short paragraph and pay special attention to the Checkmyzip () function.
Copy Code code as follows:
<% @LANGUAGE = "JavaScript"%>
<%
No ASP here, just a regular HTML Page
%>
<HTML>
<script language= "JavaScript" >
<!--Hide
function Checkmyzip (ZipCode)
{
var myregularexpression=/(^\d{5}$) | (^\d{5}-\d{4}$)/
if (myregularexpression.test (zipCode) = = True)
{
return Nothingiswrong ();
}
Else
{
return Somethingiswrong ();
}
}
function Nothingiswrong ()
{
Doing nothing
return True
}
function Somethingiswrong ()
{
Alert ("Something is wrong with the ZIP code for you provided.")
Document.zipCodeForm.zipCodeText.focus ()
return false;
}
Stop hiding-->
</SCRIPT>
<strong>type a valid U.S. Postal ZIP code into the box, and submit it.</strong>
<form name= "Zipcodeform" action= "script05a.asp" method= "Post"
Onsubmit= "return Checkmyzip (Document.zipCodeForm.zipCodeText.value)" >
<input type= "Text" name= "Zipcodetext" ><BR>
<BR>
<input type= "Submit" value= "Submit" >
</FORM>
</HTML>
What we see in this lesson is one of the biggest rewards for writing ASP scripts in JavaScript. Look at the script below, and then pay attention to the Checkmyzip () function again.
Copy Code code as follows:
<% @LANGUAGE = "JavaScript"%>
<%
function Checkmyzip (ZipCode)
{
var myregularexpression=/(^\d{5}$) | (^\d{5}-\d{4}$)/
if (myregularexpression.test (zipCode) = = True)
{
return Nothingiswrong ();
}
Else
{
return Somethingiswrong ();
}
}
function Nothingiswrong ()
{
Doing nothing
return True
}
function Somethingiswrong ()
{
return false;
}
var zipcode=new String (Request.Form ("Zipcodetext"))
if (Checkmyzip (zipCode) ==true)
{
Response.Write ("Response.Write ("The ZIP code you provided ... ")
Response.Write ("<font color=\" red\ ">")
Response.Write (ZipCode + "</FONT> is good.\r")
Response.Write ("}
Else
{
Response.Write ("Response.Write ("The ZIP code you provided ... ")
Response.Write ("<font color=\" red\ ">")
Response.Write (ZipCode + "</FONT> has a problem.\r")
Response.Write ("}
%>
This is not the perfect one, but it contains the main points we are going to teach. The functions of the client and server stern data are exactly the same. The support function is the same as hungry, but the change is obvious. Just a joke, let's take a look at the following script. It does not have client-side validation.
Copy Code code as follows:
<% @LANGUAGE = "JavaScript"%>
<%
No ASP here, just a regular HTML Page
%>
<HTML>
<strong>type a ZIP code (with no client side validation)
into the box submit it.</strong>
<form name= "Zipcodeform" action= "script05a.asp" method= "Post" >
<input type= "Text" name= "Zipcodetext" ><BR>
<BR>
<input type= "Submit" value= "Submit" >
</FORM>
</HTML>
The first section:
This is the section of the first part of the course plan. There have been the use of VBScript to write an ASP friend does not need to have further research on this site. They can now use the tricks of their client script to convert any function (subroutine), any page, or any application into JavaScript.
Other friends need to stay and continue our journey in part two.
Original and example address of this section: http://aspjavascript.com/lesson05.asp
Original Author: James Clark translation: Huahua reprint Please specify