Go basic Learning Record-write Web application-Web development Input Validation (IV)

Source: Internet
Author: User

Continue to share the previous articles, under this share--input validation

In order to keep the project can be learned, I will be here to share the code accumulated under, on GitHub, want to learn as soon as possible, you can directly clone my code, write code does not get started, are equal to the useless, light to see, for me, I am not able to learn.

Project Address

https://github.com/durban89/wiki_blogtag: 1.0.4

Some students may not understand, how to only give these, completely do not understand AH. I'm going to have to use the command, and follow the operation, it should be solved.

git clone https://github.com/durban89/wiki_blog /local/pathcd /local/pathgit fetch origingit checkout 1.0.4

I think that's clear enough. Ok!

Continue to share the logic of input validation.

One of the most important principles in web development is that you cannot trust any content in a client user form.
You must validate all incoming data before you use it.
Many websites are affected by this problem, which is both simple and critical.
There are two ways to validate commonly used form data.
The first is the front-end JavaScript authentication, the second is the backend server authentication.
This is the last time to share, share the server-side validation in Web development part fourth

radio button

If we want to know if the open state of the article is open or closed, we can use the radio button, open as 1, close to 2.
However, does the program throw an exception when a 3 value is sent when the article is submitted? Sure, but if you deal with it.
We need to use the same method as in the drop-down list to make sure our radio buttons return only the expected values and operate as follows
Add the following code to the edit.html

<div class="checkbox">  <label for="category">文章状态</label></div><div class="radio">  <label>    <input type="radio" name="openStatus" id="openStatus1" value="1" checked> 打开  </label>  <label>    <input type="radio" name="openStatus" id="openStatus2" value="2"> 关闭  </label></div>

Use the following code in the Articlesave method to validate the input:

if !helpers.ValidateInArray(openStatusSlice, openStatus) {    fmt.Println("openStatus not in openStatusSlice")    http.Redirect(w, r, "/view/"+title, http.StatusFound)    return}

and add the receive and output logic as follows

openStatus := r.FormValue("openStatus")fmt.Println("openStatus: ", openStatus)

It's the same logic as the last time we shared the judgment "classification."
Recompile and run the project, and if you choose "Open", the following output will be received at the terminal after submission

openStatus:  1

check box

Suppose there are some user-interested check boxes, and you don't need the irrelevant values here.
You can verify the following:

In this case, the cleanup is slightly different from the Verify button and check box input, because here we get a slice from the check box. The instance operations are as follows:
Add the following code to the edit.html

<div class="checkbox">  <label for="category">频道</label></div><div class="checkbox">  <label>    <input type="checkbox" name="channel" value="news"> 新闻  </label>  <label>    <input type="checkbox" name="channel" value="technology"> 科技  </label>  <label>    <input type="checkbox" name="channel" value="other"> 其他  </label></div>

Use the following code in the Articlesave method to validate the input:
The logic to add the receive and output is as follows

channel := r.FormValue("channel")fmt.Println("channel: ", channel)fmt.Println("channels: ", r.Form["channel"])

Recompile and run the project, if you select "News", "technology", after submission will receive the following output in the terminal

channel:  newschannels:  [news technology]

As you can see from the output, use R. The value obtained by Formvalue is not the value we get after we commit, but R. Form["Channel" is in line with our requirements, so in future development, the way to get the value of the check box should be noted.

Let's add the following code to validate the obtained value
Add a helper function first

// ValidateSliceIntersection 交集func ValidateSliceIntersection(sourceSlice []string, targetSlice []string) []string {    var slice3 []string    for _, slice1 := range sourceSlice {        for _, slice2 := range targetSlice {            if slice1 == slice2 {                slice3 = append(slice3, slice2)            }        }    }    return slice3}

Called in the method

chennelSlice := []string{"news", "technology", "other"}a := helpers.ValidateSliceIntersection(channel, chennelSlice)if len(a) == 0 {    fmt.Println("channel is empty")    http.Redirect(w, r, "/view/"+title, http.StatusFound)    return}

Recompile and run the project, and if any channel options are not selected, the following output will be received at the terminal after submission

channels:  []channel is empty

If you are interested, try choosing "News", "technology" and try again.

Share here today, if you have a better way please leave a comment below or add group communication

Project Update Address

https://github.com/durban89/typescript_demo.gittag: 1.0.5

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.