form validation in ASP Web site design

Source: Internet
Author: User
Tags exit require trim valid valid email address
Form Validation | design | Form validation General Dynamic Web sites need to collect user information through forms, or to interact with users, although we believe that the vast majority of people who choose to fill out the form will be seriously completed, but also can not avoid boring people disorderly filling out forms, Moreover, even if a normal user in the process of filling out some of the unconscious will be filled with errors, or they forget to fill out certain options. Typically, in order to avoid this situation, the developer will add a validation process to the form, validate the data that the user fills in before the form data is submitted to the server or after the server, and return the request for correction if the error is encountered.
Programmers typically do this in an asp:
1, with JavaScript in the client to verify.
2, with VBScript in the client to verify.
3, with VBScript in the server side to verify.
The above mentioned two different environments, server-side and client, client authentication is actually included in the downloaded page, when the user submits the form, it directly in the downloaded to the local page to invoke script to verify, which can reduce server-side operations. The server-side validation is the submission of the page to the server processing, and another ASP page on the server performs the validation of the form before returning the results to the client. The disadvantage of this is that each time the authentication is to go through the server, consuming a long time. However, using server-side authentication can achieve better verification function.
This article is mainly about ASP in the server-side validation.
Before you learn the following methods, you need to think about what needs to be controlled in the form validation issue. Just like the idea of software engineering, analyze what you want to verify in the first place.
1, require the user's input must be in Chinese (English or digital).
2, require the user's input must be a valid email address.
3, the user entered the data to carry out a variety of different restrictions.
4, limit the amount of data entered by the user.
5 、....
In fact, we will also encounter many different problems in the design of the website, we need to define some rules and restrictions.
Here we take an example to illustrate the validation method.
1, verify the input of the number
Suppose a text box

<form name= "Form1" method= "Post" action= "" >
<input type= "text" name= "TextField" >
</form>
' requires the user to enter a number
If not IsNumeric (Request.Form ("TextField")) Then
Response.Write "Re-fill"
End If
' Require limiting the number of digits, such as you want the user to enter the OICQ number
' This example restricts the user's input to only 4 to 10 digits.
If Len (Request.Form ("TextField")) >10 or Len (Request.Form ("TextField")) <4 Then
Response.Write "Re-fill"
End If
Of course, the top with Request.Form and request is the same, whatever you write.
2, verify the user entered the e-mail address
' Reference a generic detection function to illustrate
' Because the inspection program is longer, define it as a function to call the
function IsValidEmail (email)
Dim names, Name, I, C
' Check for valid syntax in ' to email address.
IsValidEmail = True
names = Split (email, "@")
If UBound (names) <> 1 Then
IsValidEmail = False
Exit function
End If
For each name in names
If Len (name) <= 0 Then
IsValidEmail = False
Exit function
End If
For i = 1 to Len (name)
c = Lcase (Mid (name, I, 1))
If InStr ("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric (c) Then
IsValidEmail = False
Exit function
End If
Next
If left (name, 1) = "." or Right (name, 1) = "." Then
IsValidEmail = False
Exit function
End If
Next
If INSTR (names (1), ".") <= 0 Then
IsValidEmail = False
Exit function
End If
i = Len (names (1))-InStrRev (names (1), ".")
If I <> 2 and I <> 3 then
IsValidEmail = False
Exit function
End If
If INSTR (email, "...") > 0 Then
IsValidEmail = False
End If

End Function
' The above function you should all read, of course you can modify this code so that even if the user input XXX@CCC.DDD is the wrong email address, because DDD is not a valid domain name.
' When quoted, you can write this
If IsValidEmail ("TextField") =false Then
Response.Write "Re-fill"
End If
3, the verification is empty the table element
Some information is required for the user to fill out, so it is not allowed to be empty, so when the user input is empty, you need to be prompted.
' The processing of an empty unit
If Request.Form ("TextField") = "" Then
Response.Write "Fill out as empty"
End If
4, to determine the user input is not a date
First understand Date value format 2002-11-19
' directly determine whether a value is a date
If not IsDate (Request.Form ("TextField")) Then
Response.Write "Date Fill Error"
End If
And we design the site often use three drop-down box to achieve the year, month, day three different options, how to connect the three values, and verify it?
First of all, of course, to create three Drop-down boxes, respectively named Table Single-name for Date,month,year, and then the background of the processing, because previously we in the foreground to get the date,month,year these three passed over the value, So we have to connect them together to synthesize a variable to validate and store the database. Connecting to a standard date format expression can write this:
Birthday=trim (Request.Form ("Year")) & "-" &trim (Request.Form ("month")) & "-" &trim (Request.Form (" Date "))
Verify that the input you have converted is a valid date you can use the IsDate function:
If not IsDate (birthday) Then
Response.Write "Error"
End If
5, do not allow users to enter some special characters
Here we assume that the values passed are content, and we do not allow the values to be entered = and%
If INSTR (Request ("content"), "=") >0 or Instr (Request ("content"), "%") >0
Then
Response.Write "cannot enter = and%"
End If



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.