Home Web Front-end JS Tutorial js method to implement form serialization to determine null value

js method to implement form serialization to determine null value

Feb 03, 2018 pm 01:15 PM
javascript Serialization form

When I was learning javaweb, I did the exercise of adding the form information on the page to the database. When submitting a form, you need to ensure that each input box, radio button, check box, etc. is not empty. At the beginning, you get the values ​​of the controls one by one to determine whether they are empty. Later, you learned about form serialization. Just use serialization to determine the null value.

//form form page


<form id="basicInfo" action="EmployeeServlet?flag=addEmployeeInfo" method="post">
  <p class="formbody">
    <p class="formtitle"><span>基本信息</span></p>
    <ul class="forminfo">
      <li>
        <label>员工账号</label>
        <input id="account" name="account" type="text" class="dfinput" readOnly="true" />
        <i>设置员工姓名和入职时间后,系统自动生成,无法更改</i>
      </li>
      <li>
        <label>员工姓名</label>
        <input name="name" type="text" class="dfinput" id="name" />
        <i>姓名不能为数字</i>
      </li>
      <li>
        <label>密码</label>
        <input name="password" type="password" class="dfinput" id="pwd" placeholder="空值或空白则默认密码为123456" />
        <i></i>
      </li>
      <li>
        <label>性别</label>
        <cite>
          <input name="gender" type="radio" value="男" checked="checked" />男    
          <input name="gender" type="radio" value="女" />女
        </cite>
      </li>
      <li>
        <label>生日</label>
        <input id="birth" name="birthday" type="text" class="dfinput" placeholder="格式:yyyy-mm-dd"/>
        <i id = "birthTip"></i>
      </li>
      <li>
        <label>手机号码</label>
        <input id="phone" name="telephone" type="text" class="dfinput" />
        <i id = "phoneTip"></i>
      </li>
      <li>
        <label>邮箱</label>
        <input id="email" name="email" type="text" class="dfinput" />
        <i id = "emailTip"></i>
      </li>
      <li>
        <label>地址</label>
        <input name="address" type="text" class="dfinput" />
        <i id = "addressTip"></i>
      </li>
      <li>
        <label>所属部门</label>
        <select id="dept" class="dfinput" name="dept">
          
        </select>
      </li>
      <li>
        <label>入职时间</label>
        <input id="entrytime" name="entrytime" type="text" class="dfinput" placeholder="格式:yyyy-mm-dd" />
        <i id = "timeTip"></i>
      </li>
      <li>
        <label>员工状态</label>
        <select id="empstate" class="dfinput" name="empstate">
        </select>
      </li>
      <li>
        <label>直属上级</label>
        <input id="superior" name="superior" type="text" class="dfinput" placeholder="直属上级员工id" />
        <i id = "superTip"></i>
      </li>
      <li>
        <label>职务</label>
        <select id="dutyid" class="dfinput" name="dutyid">
        </select>
      </li>
      <li>
        <label>角色权限</label>
        <select id="authority" class="dfinput" name="authority">
        </select>
      </li>
      <li>
        <label> </label>
        <input id="savebtn" type="submit" class="btn" value="确认保存"/>
      </li>
    </ul>
  </p>
</form>
Copy after login

Triggered when a submission is written in the external link js file The event


$(&#39;form&#39;).submit(function(){
//当密码为空的时候自动赋值
  var pwd = $(&#39;#pwd&#39;).val();
  if(pwd==null || pwd == &#39;&#39;){
    $(&#39;#pwd&#39;).val(&#39;123456&#39;);
  }
  var data = $(&#39;form&#39;).serialize();
//表单序列化后返回一个字符串 如:account=123&password=1234&sex=&dept=2
  var array = data.split(&#39;&&#39;);
//把字符串按&号分隔成数组 得到 {account=123,password=1234,sex=,dept=2} 字符串数组
  for(var i = 0;i < array.length; i++){
    var kwarr = array[i].split(&#39;=&#39;);
//循环将数组中的每个子元素字符串用=号分隔成数组 {account,123} {sex,} 然后判断索引为1的子元素是否存在或为‘&#39; 从而达到了表单判空的目的
    if(kwarr[1]===null || kwarr[1] ===&#39;&#39;){
      alert(&#39;除密码外不能存在空值&#39;);
      return false;
    }
  }
});
Copy after login

The above is the entire content of the example of JS form serialization to determine the null value brought by the editor. I hope you like it~

Related recommendations:

About PHP data serialization testing

Detailed explanation of PHP serialization and deserialization principles

jQuery form serialization example code example sharing

The above is the detailed content of js method to implement form serialization to determine null value. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Tips for using Laravel form classes: ways to improve efficiency Tips for using Laravel form classes: ways to improve efficiency Mar 11, 2024 pm 12:51 PM

Forms are an integral part of writing a website or application. Laravel, as a popular PHP framework, provides rich and powerful form classes, making form processing easier and more efficient. This article will introduce some tips on using Laravel form classes to help you improve development efficiency. The following explains in detail through specific code examples. Creating a form To create a form in Laravel, you first need to write the corresponding HTML form in the view. When working with forms, you can use Laravel

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How does Java serialization affect performance? How does Java serialization affect performance? Apr 16, 2024 pm 06:36 PM

The impact of serialization on Java performance: The serialization process relies on reflection, which will significantly affect performance. Serialization requires the creation of a byte stream to store object data, resulting in memory allocation and processing costs. Serializing large objects consumes a lot of memory and time. Serialized objects increase load when transmitted over the network.

See all articles